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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With