我使用此代码将位数组转换为bufferedimage,但它给出了错误
ByteArrayInputStream bais = new ByteArrayInputStream(arr);
BufferedImage finale=new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
finale=ImageIO.read(bais);
File outputfile = new File("C:\\Users\\Divya Prakash\\Desktop\\image32.jpg");
ImageIO.write(finale, "jpg", outputfile);
错误
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
at javax.imageio.ImageIO.write(ImageIO.java:1520)
at imgpros.Imgpros.main(Imgpros.java:113)
Java Result: 1
如何删除错误?
答案 0 :(得分:1)
正如一条评论所示,ImageIO可能无法读取ByteInputStream
。或者,您的阵列可能无法转换为ByteInputStream
。所以这返回null。试试:
System.out.println(bais==null);
如果打印true
,您可以:
a)寻找将数组转换为BufferedImage
为此,我建议您为BufferedImage
定义宽度和高度:
finale=new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
然后,用数组中的点填充它:
for (int x = 0 ; x < width ; x=x+1) {
for (int y = 0 ; y < height ; y=y+1) {
finale.setRGB(x,y,arr[y*w+x]);
}
}
如果是一个int数组...
b)检查ImageIO是否可以处理您的ByteInputStream
。
InputStream bais = new ByteArrayInputStream(arr);
希望它可以帮到你!