我有同样的问题,其他人试图应用这个问题的正确答案,我的int数组实际上得到一个OutOfboundException,我修复它添加一个索引,因为数组的长度必须是widht *高* 3在这之后我无法得到的图像,所以我固定它的栅格设置到图像
public static Image getImageFromArray(int[] pixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,pixels);
image.setData(raster);
return image;
}
而且你可以看到图像,如果u显示它的标签上JFrame上这样
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
在imageIcon()上设置图像。 最后的建议,您可以尝试将Bufferedimage.TYPE_INT_ARGB更改为与您从此类型中获得数组的图像相匹配的其他内容,这非常重要,因为我有一个0和-1的数组,因此我使用了此类型BufferedImage.TYPE_3BYTE_BGR