converting BufferedImage to PImage
Answered
- Need more info
- Answered
- Working on it
in
Programming Questions
•
3 years ago
a while back, toxi posted a great method for asynchronous media loading:
http://processinghacks.com/hacks:dataloadqueue
unfortunately, this code is broken with java 1.6.
PImage will no longer take a BufferedImage, despite the fact that it extends java.awt.Image. this works for me if i force my Mac to run Java 1.5
here's where the error occurs:
in DataBuffer{}:
public PImage getAsImage() {
try {
ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
BufferedImage bimg = ImageIO.read(bis);
return new PImage(bimg);
}
catch(Exception e) {
System.err.println("Can't create image from buffer");
e.printStackTrace();
}
return null;
}
(bytes is simply the raw byte array)
and here's the console output:
java.lang.ClassCastException: [I cannot be cast to [B
I'm not sure exactly how to convert BufferedImage back to java.awt.Image. there is some code here, but its from 2002 and i'm not sure how relevant it is: http://www.java2s.com/Code/Java/2D-Graphics-GUI/ConvertjavaawtimageBufferedImagetojavaawtImage.htm
Another option might be to completely replace toxi's code with MediaTracker, has anyone had sucess with this in Processing?
unfortunately, this code is broken with java 1.6.
PImage will no longer take a BufferedImage, despite the fact that it extends java.awt.Image. this works for me if i force my Mac to run Java 1.5
here's where the error occurs:
in DataBuffer{}:
public PImage getAsImage() {
try {
ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
BufferedImage bimg = ImageIO.read(bis);
return new PImage(bimg);
}
catch(Exception e) {
System.err.println("Can't create image from buffer");
e.printStackTrace();
}
return null;
}
(bytes is simply the raw byte array)
and here's the console output:
java.lang.ClassCastException: [I cannot be cast to [B
at sun.awt.image.
ByteInterleavedRaster.
getDataElements(
ByteInterleavedRaster.java:
351)
at processing.core.PImage.<init>( PImage.java:233)
at jb_PIMP_sketch1f$DataBuffer. getAsImage(jb_PIMP_sketch1f.
java:1532)
at jb_PIMP_sketch1f$ QueueListener.queueComplete(
jb_PIMP_sketch1f.java:1482)
at jb_PIMP_sketch1f$ CachedDataLoadQueue.run(jb_
PIMP_sketch1f.java:1614)
at processing.core.PImage.<init>(
at jb_PIMP_sketch1f$DataBuffer.
at jb_PIMP_sketch1f$
at jb_PIMP_sketch1f$
I'm not sure exactly how to convert BufferedImage back to java.awt.Image. there is some code here, but its from 2002 and i'm not sure how relevant it is: http://www.java2s.com/Code/Java/2D-Graphics-GUI/ConvertjavaawtimageBufferedImagetojavaawtImage.htm
Another option might be to completely replace toxi's code with MediaTracker, has anyone had sucess with this in Processing?
1