We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › create image from byteArray byte[]
Page Index Toggle Pages: 1
create image from byteArray byte[] (Read 4528 times)
create image from byteArray byte[]
Jul 11th, 2008, 9:52am
 
hi
im trying to create an awt.image using awt.toolkit
this is my code

import java.awt.Toolkit;
Image fJPEG( byte[] jpegBytes )
{
 Image result = null;


 try
 {
   result = Toolkit.getDefaultToolkit().createImage(jpegBytes);
 }
 catch ( Exception e )
 {
 }


 return result;
}

and drawing with:

void draw(){
 println(frameRate);
 arraycopy(b, c);
 injectHDT(c,dhtHeaders[0]+21+int(random(hdt[0].length-1)),byte(int(random(254))));
 
 bimg=fJPEG(c);


img = new PImage( (java.awt.Image)bimg );
image(img, 0, 0);

}
but im getting this error"

Uncaught error fetching image:

java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.
java:999)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:317)
at processing.core.PGraphicsJava2D$ImageCache.update(PGraphicsJava2D.java:691)
at processing.core.PGraphicsJava2D.imageImpl(PGraphicsJava2D.java:650)
at processing.core.PGraphics.image(PGraphics.java:2124)
at processing.core.PApplet.image(PApplet.java:7573)
at Temporary_3515_9041.draw(Temporary_3515_9041.java:85)
at processing.core.PApplet.handleDisplay(PApplet.java:1465)
at processing.core.PGraphics.requestDisplay(PGraphics.java:690)
at processing.core.PApplet.run(PApplet.java:1562)
at java.lang.Thread.run(Thread.java:613)

java.lang.ArrayIndexOutOfBoundsException: 1
at java.awt.image.PixelGrabber.setPixels(PixelGrabber.java:595)
at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120)

Re: create image from byteArray byte[]
Reply #1 - Jul 11th, 2008, 1:20pm
 
I am not sure to understand what you are trying to do, since you show only part of the code. But are you sure you can't do that with Processing only? It can load images, access to the pixels, and what's not.

Anyway, it looks like the jpegBytes you give aren't valid (must be the content of a Jpeg file). Since you silently catch any exception, you can't see if it have been rejected, but I bet the Image fields width and height just remain at default value, probably -1.
Re: create image from byteArray byte[]
Reply #2 - Jul 11th, 2008, 6:45pm
 
im modifing the huffman tables of the jpg data and drawing the results to scree. it can be done in processing. in fact. i ve used other method to acomplish and it works. the problem is the framerate. the jpegdata is good with the other way.
but fails with this one.
this is the other function im using:

BufferedImage fromJPEG( byte[] jpegBytes )
{
 BufferedImage result = null;

 ByteArrayInputStream bis= new ByteArrayInputStream( jpegBytes );

 try
 {
   result = ImageIO.read( bis );
 }
 catch ( Exception e )
 {
 }
 finally
 {
   try
   {
     bis.close();
   }
   catch ( Exception e )
   {
   }
 }

 return result;
}

with this one i get 6 fps.
Re: create image from byteArray byte[]
Reply #3 - Jul 12th, 2008, 11:55am
 
Thanks for satisfying my curiosity...
And this can be used in other ways, like decoding a Base64 image from a Mime e-mail message, for example.

I tried and had the same issue... Actually, -1 in height and width means the image isn't loaded: "If the height is not yet known, this method returns -1 and the specified ImageObserver object is notified later."

After lot of experiments and searches on the Net, I found a solution:

import java.awt.Toolkit;

Image GetFromJPEG(byte[] jpegBytes)
{
 Image jpegImage = null;
 println("Jpeg data length: " + jpegBytes.length);
 
 try
 {
   jpegImage = Toolkit.getDefaultToolkit().createImage(jpegBytes);
 }
 catch (Exception e)
 {
   println("Problem creating image: " + e.toString() + ": " + e.getMessage());
 }
 // Image isn't loaded yet, we have to wait for end of processing of the data
 // Stupid way, I suppose I should use an ImageObserver...
 float waitTime = 0;
 while (jpegImage.getHeight(null) == -1)
 {
   delay(25); // Don't hog CPU!
   waitTime += 0.025;
 }
 println("After " + int(waitTime) + "s, I get an image of width " +
     jpegImage.getWidth(null) + " and height " + jpegImage.getHeight(null));
 
 return jpegImage;
}

byte[] bytes = null;
Image img = null;

void setup()
{
 noLoop();
 try
 {
   InputStream in = new FileInputStream("D:/_PhiLhoSoft/Processing/Johnson.jpg");
   int l = in.available();
   println("L: " + l);
   bytes = new byte[l];
   in.read(bytes);
 }
 catch (Exception e)
 {
   println("Problem reading image: " + e.toString() + ": " + e.getMessage());
 }
 // Initial load
 img = GetFromJPEG(bytes);
 println(img.getClass().toString());
//  sun.awt.image.ToolkitImage tki = (sun.awt.image.ToolkitImage) img;
//  BufferedImage bi = tki.getBufferedImage();
//  println(bi.getClass().toString());
 size(img.getWidth(null), img.getHeight(null));
}

void draw()
{
//  println(frameRate);  
//  arraycopy(b, c);
//  injectHDT(c,dhtHeaders[0]+21+int(random(hdt[0].length-1)),byte(int(random(254))));

 PImage pimg = new PImage(img);
 image(pimg, 0, 0);
}

Notice the image is actually a sun.awt.image.ToolkitImage, a proprietary class which is deprecated... but still used, apparently, by the JVM. The problem is that I haven't found any API description of this class.

I solved that load delay issue by a simple loop, a cleaner way would be, probably, to make a class implementing the ImageObserver interface and wait for this class to be notified of the load.
In Processing, I chose the simpler way! Wink

[EDIT] Improving a bit the code...
Page Index Toggle Pages: 1