gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:08:02 26 4
gpt4 key购买 nike

我有图像 RBG 值的数组 int[ ][ ],我需要将其存储到 jpg 文件中。我尝试这样做:

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

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

最佳答案

即使除了 int[][]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]);
}
}

关于java - 将RGB的int[][]保存为图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24496488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com