Kevin
Kevin

Reputation:

How to create a java.awt.Image from image data?

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?

Upvotes: 2

Views: 9832

Answers (3)

Stefan Tannenbaum
Stefan Tannenbaum

Reputation: 276

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.

Upvotes: 4

Paul Tomblin
Paul Tomblin

Reputation: 182782

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

Upvotes: 0

Dmitry Khalatov
Dmitry Khalatov

Reputation: 4369

import java.awt.*;

Toolkit toolkit = Toolkit.getDefaultToolkit();

Image image = toolkit.createImage(imageData,imageOffset,imageLength);

Upvotes: 3

Related Questions