简体   繁体   English

为什么我的BufferedImage来自字节数组返回null?

[英]Why is my BufferedImage from a byte array returning null?

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. 现在这段代码适用于PNG或JPG图像,但是当我将它用于BMP图像时,它会返回一个异常,表示bImageFromConvert为null。 Can anyone please help me know why it does that? 谁能帮助我知道它为什么这样做? Thanks everyone. 感谢大家。

The answer is in the Javadoc : 答案在Javadoc中

Returns a BufferedImage as the result of decoding a supplied ImageInputStream with an ImageReader chosen automatically from among those currently registered. 返回一个BufferedImage,作为解码提供的ImageInputStream的结果,ImageReader自动从当前注册的ImageReader中选择。 If no registered ImageReader claims to be able to read the stream, null is returned. 如果没有已注册的ImageReader声称能够读取流,则返回null。

This previous post on SO is more detailed. 此前关于SO的帖子更加详细。

The method javax.imageio.ImageIO.getImageReadersBySuffix() may be useful for you. 方法javax.imageio.ImageIO.getImageReadersBySuffix()可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM