Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract image's width, height, color and type from byte array

Tags:

java

image

I have an image in the format of byte[] array in my Java code. I want the following information extracted from that array. How can I do it as fast as possible.

  • Width
  • Height
  • Color (black & white, color or transparent? If color, what is the main color?)
  • Type (Is the image PNG, GIF, JPEG, etc.)
like image 401
Alireza Noori Avatar asked Jun 19 '13 10:06

Alireza Noori


People also ask

How can I get image from byte array?

To convert a byte array to an image. Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter).

How can you find out the width and height of an image object in Python?

open() is used to open the image and then . width and . height property of Image are used to get the height and width of the image.

How is an image stored in a byte array?

Any image is merely a sequence of bytes structured in accordance with whatever underlying format used to represent it, eg color data, layers, dimensions, etc. The significance of any byte(s) you see during debugging is entirely dependent upon the native format of the image, eg PNG, TIFF, JPEG, BMP, etc.

How can I get bytes of an image?

Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.


2 Answers

Use ImageIO to read as buffered image and then get relevant things which you want. See java doc at http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html.

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;


public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // assuming that picture is your byte array
        byte[] picture = new byte[30];

        InputStream in = new ByteArrayInputStream(picture);

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

    }

}
like image 99
abhinav Avatar answered Sep 24 '22 04:09

abhinav


To get the image type from the byte array, you can do something like:

byte[] picture = new byte[30];
ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(picture));

Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
while (readers.hasNext()) {
    ImageReader read = readers.next();
    System.out.println("format name = " + read.getFormatName());
}

Here is the output I have for different files:

format name = png
format name = JPEG
format name = gif

It was inspired from:

Convert Byte Array to image in Java - without knowing the type

like image 41
adou600 Avatar answered Sep 20 '22 04:09

adou600