简体   繁体   中英

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?

import java.awt.*;

Toolkit toolkit = Toolkit.getDefaultToolkit();

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

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.

看看java.awt.image.MemoryImageSource。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM