3

I have a byte array which size is 640*480*3 and its byte order is r,g,b. I'm trying to convert it into Image. The following code doesn't work:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));

with the excepton

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
at javax.imageio.ImageIO.write(ImageIO.java:1520)

I also tried this code:

ImageIcon imageIcon = new ImageIcon(data);
Image img = imageIcon.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_3BYTE_BGR); //Exception

but unsuccessfully:

Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0

How can I receive the Image from this array?

1 Answer 1

2

A plain byte array is not a gnerally recognized image format. You have to code the conversion yourself. Luckily its not very hard to do:

int w = 640;
int h = 480;
BufferedImage i = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int y=0; y<h; ++y) {
    for (int x=0; x<w; ++x) {
        // calculate index of pixel
        // depends on exact organization of image
        // sample assumes linear storage with r, g, b pixel order
        int index = (y * w * 3) + (x * 3);
        // combine to RGB format
        int rgb = ((data[index++] & 0xFF) << 16) |
                  ((data[index++] & 0xFF) <<  8) |
                  ((data[index++] & 0xFF)      ) |
                  0xFF000000;
        i.setRGB(x, y, rgb);
    }
}

The exact formula for pixel index depends on how you organized the data in the array - which you didn't really specify precisely. The prinicple is always the same though, combine the R, G, B value into an RGB (ARGB to be precise) value and put it in the BufferedImage using setRGB() method.

3
  • A plain byte array is not a gnerally recognized image format. what is the source of this info and why it would be that ? And the second case actually reveals that he is having issue with the image itself, he intends to read
    – Sage
    Commented Dec 5, 2013 at 20:48
  • @Sage The source for the info is common sense. Something not specified can't be a generally recognized image format, or can it? There is a gazillion ways to transform an image into a byte[] (and the OP described it briefly 640*480*3 - that leaves no space for any meta data, so it can't be a generic format). At least that much is obvious to me. And the trouble "reading" the image is a natural result of the format not being recognized. All the exceptions result directly or indirectly from an unrecognizable image format. Take a look at the Javadocs for IconImage.
    – Durandal
    Commented Dec 6, 2013 at 17:06
  • thank you i missed the 640*480*3 mentioning. Actually His second exception got me distracted and confused me.
    – Sage
    Commented Dec 6, 2013 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.