如何将二维整数数组转换为灰度 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 值,您只设置了低字节,它可能是蓝色,这就是为什么它显示为全蓝色。
尝试:
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);
}
}
关于java - 将 0-256 范围内的二维 int 数组转换为灰度 png?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5436931/