基本上,我正在做的是将图像转换为字节数组,处理它,然后在获得字节数组后,将其转换回图像。这是我如何将字节数组转换为图像。
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();
}
现在这段代码适用于PNG或JPG图像,但是当我将它用于BMP图像时,它会返回一个异常,表示bImageFromConvert为null。谁能帮助我知道它为什么这样做?谢谢大家。
答案 0 :(得分:1)
答案是in the Javadoc:
作为解码提供的结果返回BufferedImage ImageInputStream,从中自动选择ImageReader 那些目前注册的。如果没有注册的ImageReader声称 能够读取流,返回null。
此previous post on SO更详细。