Here you can find the source of bytesToImage(byte[] imageData)
Parameter | Description |
---|---|
imageData | The byte array of the data to convert. |
Parameter | Description |
---|
public static BufferedImage bytesToImage(byte[] imageData) throws IOException
//package com.java2s; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.IOException; public class Main { /**/* ww w. ja va 2s .com*/ * Converts a byte array into an image. The byte array must include the * image header, so that a decision about format can be made. * * @param imageData The byte array of the data to convert. * @return Image The image instance created from the byte array * @throws java.io.IOException if any. * @see java.awt.Toolkit#createImage(byte[] imagedata) */ public static BufferedImage bytesToImage(byte[] imageData) throws IOException { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image toolkitImage = toolkit.createImage(imageData); int width = toolkitImage.getWidth(null); int height = toolkitImage.getHeight(null); // Deriving a scaled instance, even if it has the same // size, somehow makes drawing of the tiles a lot // faster on various systems (seen on Linux, Windows // and MacOS X). toolkitImage = toolkitImage.getScaledInstance(width, height, Image.SCALE_FAST); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = img.getGraphics(); g.drawImage(toolkitImage, 0, 0, null); g.dispose(); return img; } }