【问题标题】:How to convert a raw data array into a BufferedImage如何将原始数据数组转换为 BufferedImage
【发布时间】:2013-06-26 01:56:44
【问题描述】:

我在 java 中有一个 tiff 图像的 int[2048][2048] 原始数据数组。我想将该数组转换回 BufferedImage。我应该如何进行?

【问题讨论】:

  • 现在是原始数据还是 TIFF?理清模棱两可的问题以获得好的答案。

标签: java


【解决方案1】:

就这么简单:

BufferedImage image = ImageIO.read(new ByteArrayInputStream(array));

【讨论】:

  • ByteArrayInputStream 接受一个字节数组作为参数,但我拥有的是一个 int[][] 数组
【解决方案2】:

当您说原始数据时,这并没有说明数据的格式,我假设它将采用最常见的格式:ARGB。将其转换为 BufferedImage 然后可以通过使用 BufferedImage.setRGB() 方法简单地传输每个像素来完成:

public static BufferedImage toBufferedImage(int[][] rawRGB) {
    int h = rawRGB.length;
    int w = rawRGB[0].length;
    BufferedImage i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    for (int y=0; y<h; ++y) {
        for (int x=0; x<w; ++x) {
            int argb = rawRGB[y][x];
            i.setRGB(x, y, argb);
        }
    }
    return i;
}

你明白了,只需将逐个像素复制到 BufferedImage 中。

如果图像似乎在一维或两个维度上镜像,则需要在 setRGB 调用中分别使用 (w-x-1, h-y-1) 进行校正。 如果图像似乎是旋转的,则使用 w 和 h 交换并设置 RGB(y, x, argb) 创建图像。 如果颜色出现反转/乱码,请找出 argb 值中的通道顺序并相应地对其进行移位以使通道顺序为 A、R、G、B。像素也可能处于完全不同的颜色模型中 - 您需要找到找出它们所在的颜色模型并将每个像素转换为 ARGB。

您从获取数组的方法应该提供一些文档,说明数据是如何组织的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 2013-10-26
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多