将0-256范围内的int的二维数组转换为灰度png?
如何将 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 范围内的整数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
图像显示为蓝色,因为
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.Try:
我从未尝试过,但实际上 BufferedImage 即使在灰度模式下也应该被实例化:
I never tried but actually BufferedImage should be instantiated even in grey scale mode: