将范围为 0-256 的二维整数数组转换为灰度 png? [英] Convert a 2D array of int ranging from 0-256 into a grayscale png?

查看:23
本文介绍了将范围为 0-256 的二维整数数组转换为灰度 png?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将二维整数数组转换为灰度 png.现在我有这个:

How can I convert a 2D array of ints into a grayscale png. right now I have this:

    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);

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

but the image comes out blue. how can I make it a grayscale.

image[][] 包含从 0 到 256 的整数.

image[][] contains ints ranging from 0-256.

推荐答案

图像显示为蓝色,因为 setRGB 需要一个 RGB 值,您只设置了低字节,可能是蓝色,这就是为什么它全是蓝色的.

The image comes out blue because setRGB is expecting an RGB value, you're only setting the low byte, which is probably Blue, which is why it's coming out all blue.

试试:

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);
    }
}

这篇关于将范围为 0-256 的二维整数数组转换为灰度 png?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆