Loading...
Logo
Processing Forum
I am trying to load flickr images in a new thread using this:


I deleted the caching, because I don't need it, someone also posted the code without caching here: ( p5wiki_async_loader )


Problem is that I get this error: 

java.lang.ClassCastException: [I cannot be cast to [B

Code is in databuffer class, around line 40:  return new PImage(bimg);

What might be the problem?

Images are loaded and I can save them using saveBytes("test.jpg",bytes);

Copy code
  1. public PImage getAsImage() {
  2.         try {

  3.             ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  4.             BufferedImage bimg = ImageIO.read(bis);
  5.             return new PImage(bimg);
  6.         } catch (Exception e) {
  7.           println(e);
  8.             System.err.println("Can't create image from buffer");
  9.         }
  10.         return null;
  11.     }

Replies(2)

If I recall correctly, a similar question have been asked recently.
The semi-cryptic error message means a method is fed with an array of int [I ([ is for array, I is for int) while the method was expecting an array of bytes [B.
The array of integers is created inside the PImage constructor you call: it tries to extract the data from the BufferedImage into the pixels[] array, but fails because of incompatible data format.
Thanks, this makes a bit more sense, so I managed to get this working in a way:

The problem was that raster.getDataElements will use an array type of Transfertype which turned out to be byte[] instead of int[].

getDataElements: 
"Returns the pixel data for the specified rectangle of pixels in a primitive array of type TransferType. For image data supported by the Java 2D API, this will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE"

So instead I am using raster.getPixels(0, 0, width, height, mypixels); which will fill mypixels int[] with our values. Then I can create an image using those pixels.

Also the int[] needs to be width*height*3 because we use RGB, so 8-8-8 bits for RGB values.

I am not sure this problem lies within processings PImage constructor, or the original program on the wiki site...

Is there a better solution? Maybe use an IntArrayInput stream or something..? I am not really into pure Java.

Solution: 

Copy code
  1.  ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  2.       BufferedImage bimg = ImageIO.read(bis);

  3.       int[] mypixels;
  4.       //byte[] mybyte;

  5.       width = bimg.getWidth();
  6.       height = bimg.getHeight();
  7.       mypixels = new int[width * height * 3]; 
  8.       //mybyte = new byte[width * height];

  9.       WritableRaster raster = bimg.getRaster();

  10.       //raster.getDataElements(0, 0, width, height, mybyte );

  11.       raster.getPixels(0, 0, width, height, mypixels);

  12.       PImage img = createImage(width, height, RGB);

  13.       img.loadPixels();

  14.       for (int i = 0; i < (width * height); i++) {

  15.         img.pixels[i] = color(mypixels[ (3*i)], mypixels[(3*i + 1) ], mypixels[(3*i +2) ]);
  16.       }

  17.       img.updatePixels();

  18.       return img;