Java Graphics How to - Extract image's width, height, color and type from byte array








Question

We would like to know how to extract image's width, height, color and type from byte array.

Answer

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/*w  w w  .j  a  v  a  2s  . c om*/
import javax.imageio.ImageIO;


public class Main{

    public static void main(String[] args) throws IOException {
        byte[] picture = new byte[2048]; // your image data

        InputStream in = new ByteArrayInputStream(picture);

        BufferedImage buf = ImageIO.read(in);
        ColorModel model = buf.getColorModel();
        int height = buf.getHeight();
        int width = buf.getWidth();
    }

}