Java Byte Array to BufferedImage bytesToImage(byte[] imageData)

Here you can find the source of bytesToImage(byte[] imageData)

Description

Converts a byte array into an image.

License

Open Source License

Parameter

Parameter Description
imageData The byte array of the data to convert.

Exception

Parameter Description

Return

Image The image instance created from the byte array

Declaration

public static BufferedImage bytesToImage(byte[] imageData) throws IOException 

Method Source Code


//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;
    }
}

Related

  1. byteArrayToImage(byte[] imagebytes)
  2. byteArrayToImage(byte[] imageData)
  3. bytesToBufferedImage(final byte[] imageData)
  4. bytesToImage(byte[] buf)
  5. bytesToImage(byte[] imageData)
  6. byteToBufferedImage(byte[] buff)
  7. byteToBufferedImage(byte[] imgBytes)