使用Buffered Image写入RGB图像
我有一个二维字节数组 I[][],其中每个条目都是 0 或 -1 (即 255)
现在我使用以下代码以 RGB 格式写入图像。
public void writeImage(String outputFileName){
BufferedImage BI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int y = 0;y < height;y++){
for(int x = 0;x < width;x++){
int gray = (int) I[y][x] & 0xFF;
int newRGB = (gray << 16) + (gray << 8) + gray;
BI.setRGB(x, y, newRGB);
}
}
try{
ImageIO.write(BI, "JPG", new File(outputFileName));
} catch(IOException e){
System.err.println("Unable to output results");
}
}
图像写入成功。由于原始数据数组仅包含两个不同的值(0 和 255),因此我预计写入的图像也将具有两个不同的级别。然而,书面图像有 92 个不同的级别。我做错了什么?
I have a 2 dimensional byte array I[][], each entry in which is either 0 or -1 (i.e 255)
Now I am using the following code to write the image in RGB format.
public void writeImage(String outputFileName){
BufferedImage BI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int y = 0;y < height;y++){
for(int x = 0;x < width;x++){
int gray = (int) I[y][x] & 0xFF;
int newRGB = (gray << 16) + (gray << 8) + gray;
BI.setRGB(x, y, newRGB);
}
}
try{
ImageIO.write(BI, "JPG", new File(outputFileName));
} catch(IOException e){
System.err.println("Unable to output results");
}
}
The image is written succesfully. Since the original data array contained only two different values (0 and 255), i expected that the written image will also have two distinct levels. However the written image has 92 different levels. What am I doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有做错任何事,但 JPEG 压缩意味着每个像素中的级别并未准确记录 - 当图像未压缩时,JPEG 压缩伪像会将锐利边缘稍微扩散到其他像素。
You're not doing anything wrong, but JPEG compression means that the levels in each pixel are not exactly recorded -- when the image is uncompressed the JPEG compression artifacts spread sharp edges slightly to other pixels.