将0-256范围内的int的二维数组转换为灰度png?

2024-05-20

如何将 2D 整数数组转换为灰度 png。现在我有这个:

    BufferedImage theImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    for(int y = 0; y<100; y++){
        for(int x = 0; x<100; x++){
            theImage.setRGB(x, y, image[y][x]);
        }
    }
    File outputfile = new File("saved.bmp");
    ImageIO.write(theImage, "png", outputfile);

但图像显示为蓝色。我怎样才能使它成为灰度。

image[][] 包含 0-256 范围内的整数。


图像显示为蓝色,因为setRGB期待一个 RGB 值,您只设置低字节,这可能是蓝色,这就是为什么它显示为全蓝色。

Try:

BufferedImage theImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
for(int y = 0; y<100; y++){
    for(int x = 0; x<100; x++){
        int value = image[y][x] << 16 | image[y][x] << 8 | image[y][x];
        theImage.setRGB(x, y, value);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将0-256范围内的int的二维数组转换为灰度png? 的相关文章

随机推荐