How to create a java.awt.Image from image data?
Question
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?
Solution
import java.awt.*;
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(imageData,imageOffset,imageLength);
OTHER TIPS
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.
Have a look at java.awt.image.MemoryImageSource.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow