基本上,我正在做的是将图像转换为字节数组,对其进行处理,然后在获得字节数组后,将其转换回图像。这是我将字节数组转换为图像的方法。
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();
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码非常适用于 PNG 或 JPG 图像,但是当我将它用于 BMP 图像时,它返回一个异常,表示 bImageFromConvert 为空。谁能帮我知道为什么会这样?谢谢大家。
答案在 Javadoc 中:
返回 BufferedImage 作为使用从当前注册的 ImageReader 中自动选择的 ImageReader 解码提供的 ImageInputStream 的结果。如果没有注册的 ImageReader 声称能够读取该流,则返回 null。
上一篇关于 SO 的文章更加详细。
方法javax.imageio.ImageIO.getImageReadersBySuffix()可能对您有用。
| 归档时间: |
|
| 查看次数: |
3417 次 |
| 最近记录: |