Convert and display image from byte array

43,037

Solution 1

  • the picture is like .. when You use italics in a word document

Think I finally got what this bullet item meant now.. ;-)

Speculative answer, but here goes:

If the image you write looks "skewed", it's probably due to missing padding for each column as the BMP format specifies (or incorrect width field in the BMP header). I assume then, that the images you get EOF exceptions for, is where the width is not a multiple of 4.

Try to write the BMPs using ImageIO to see if that helps:

private static BufferedImage createRGBImage(byte[] bytes, int width, int height) {
    DataBufferByte buffer = new DataBufferByte(bytes, bytes.length);
    ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, Raster.createInterleavedRaster(buffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null), false, null);
}

...

byte[] bytes = ...; // Your image bytes
OutputStream stream = ...; // Your output

BufferedImage image = createRGBImage(bytes, width, height);

try {
    ImageIO.write(image, "BMP", stream);
}
finally {
    stream.close();
}

Solution 2

Call it by class name, liek ClassName.byteArrayToImage(byte):

public static BufferedImage  byteArrayToImage(byte[] bytes){  
        BufferedImage bufferedImage=null;
        try {
            InputStream inputStream = new ByteArrayInputStream(bytes);
            bufferedImage = ImageIO.read(inputStream);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return bufferedImage;
}
Share:
43,037
bajla
Author by

bajla

Sports and Music lover, Android Developer at work

Updated on July 09, 2022

Comments

  • bajla
    bajla almost 2 years

    I'm making a program, which gets data about an image in byte array from a server. I'm converting this data into 24bit BMP format (whether its jpeg, png, bmp or 8-24-32bpp). First, I'm saving it to my HD, and then I'm loading it into a JLabel's Icon. Works perfectly, though there are some cases in which I get the following exception:

    java.io.EOFException at
    javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:353) at
    com.sun.imageio.plugins.bmp.BMPImageReader.read24Bit(BMPImageReader.java:1188) at
    com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:843) at
    javax.imageio.ImageIO.read(ImageIO.java:1448) at 
    javax.imageio.ImageIO.read(ImageIO.java:1308)
    

    For this line (the second)

    File imgFile = new File("d:/image.bmp");
    BufferedImage image = ImageIO.read(imgFile);
    

    In these cases:

    • the image does not load into the JLabel, but it can be found on my HD
    • the conversion is not proper, because something "slips"
    • the picture is like when you use italics in a word document

    First, i thought maybe the bpp is the problem, then i thought that maybe the pictures are too large, but i have cases it works and cases it doesn't for both suggestions. I'm a little stuck here, and would be glad for ideas.

  • bajla
    bajla almost 11 years
    i don't want to convert image to bytearray .. and what is 'rs' in the first line? guess its an image, but i dont have an image in the first place
  • Mohsin Shaikh
    Mohsin Shaikh almost 11 years
    rs is the ResultSet object.
  • bajla
    bajla almost 11 years
    you are genius, the problematic images have width 618, 2671, 598 ... all the others' width is a multiple of 4.
  • Harald K
    Harald K almost 11 years
    @bajla Cool. I updated the answer now, with code to convert RGB bytes to BufferedImage. You can use that, unless you figured out how to correctly pad your image data (from what I could understand, Philipp C. Heckel's original code did it correctly, but I only skimmed through it).
  • bajla
    bajla almost 11 years
    Sir, this is the absolute winner solution! thanks for the update. I really appreciate it! This way the image displaying is faster, and now it works in all cases! :) :)