将int [] []的RGB保存为图像文件

Cat*_*t H 0 java rgb bufferedimage image

我有图像的RBG值的数组int [] [],我需要将它存储到jpg文件.我试着这样做:

 BufferedImage image = ImageIO.read(new ByteArrayInputStream(result));
 ImageIO.write(image, "jpg", new File("/path/", "snap.jpg"));
Run Code Online (Sandbox Code Playgroud)

但我有一个int [] []不是byte []数组.如何将int [] []转换为byte []而不会丢失值?

rua*_*akh 5

即使除了int[][]vs. 的问题byte[],你的表达ImageIO.read(new ByteArrayInputStream(result));也没有意义,因为它期望result是图像文件的内容(不仅仅是像素值,而是一些识别图像的所有标题,填充等等)文件格式).

我想你想要的是:

final int height = result.length;
final int width = result[0].length;
final BufferedImage image =
    new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
        bufferedImage.setRGB(x, y, result[y][x]);
    }
}
Run Code Online (Sandbox Code Playgroud)