tags:

views:

1373

answers:

3

How to create a java.awt.Image from image data? Image data is not pure RGB pixel data but encoded in jpeg/png format.

JavaME has a simple api Image.createImage(...) for doing this.

public static Image createImage(byte[] imageData,
                                int imageOffset,
                                int imageLength)

imageData - the array of image data in a supported image format.

Is there anything similar to this available in JavaSE?

+2  A: 
import java.awt.*;

Toolkit toolkit = Toolkit.getDefaultToolkit();

Image image = toolkit.createImage(imageData,imageOffset,imageLength);
Dmitry Khalatov
A: 

Have a look at java.awt.image.MemoryImageSource.

Paul Tomblin
+1  A: 

Use javax.imageio.ImageIO

BufferedImage image = ImageIO.read(new ByteArrayInputStream(myRawData));

Do not use the older functions which return an implementation of Image other than BufferedImage. The Image interface is in fact only a handle that might be loaded by a background thread and give you all kinds of headaches.

Stefan Tannenbaum