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

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

问题描述

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

$ p $ BufferedImage theImage = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB); (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值,因此图像显示为蓝色,您只需设置低位字节,可能是蓝色,这就是为什么它是全蓝的。



试试:

  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 |图像[Y] [X];
theImage.setRGB(x,y,value);
}
}


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[][] contains ints ranging from 0-256.

解决方案

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.

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

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

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