Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my BufferedImage from a byte array returning null?

Tags:

java

image

Basically, what I'm doing is converting an image into a byte array, processing it, then after obtaining the byte array, converting it back to an image. Here is how I convert it the byte array to an image.

InputStream in = new ByteArrayInputStream(result); //result is the byte array
BufferedImage bImageFromConvert;
try {
   bImageFromConvert = ImageIO.read(in);
   ImageIO.write(
      bImageFromConvert, watermark_ext, new File(extracted_name_path));
} catch (Exception e) {
   return e.getMessage();
}

Now this code works perfectly for PNG or JPG images, however when I use it for BMP images, it returns an exception that says bImageFromConvert is null. Can anyone please help me know why it does that? Thanks everyone.

like image 446
Lights Avatar asked Nov 12 '22 10:11

Lights


1 Answers

The answer is in the Javadoc:

Returns a BufferedImage as the result of decoding a supplied ImageInputStream with an ImageReader chosen automatically from among those currently registered. If no registered ImageReader claims to be able to read the stream, null is returned.

This previous post on SO is more detailed.

The method javax.imageio.ImageIO.getImageReadersBySuffix() may be useful for you.

like image 129
Aubin Avatar answered Nov 15 '22 07:11

Aubin