【发布时间】:2014-10-09 04:21:53
【问题描述】:
// load pixels into an image
this.image = new BufferedImage(this.width,
this.height,
BufferedImage.TYPE_INT_RGB);
// get actual image data for easier pixel loading
byte[] iData = new byte[this.size - 54];
for(int i = 0; i < this.size - 54; i++) {
iData[i] = this.data[i+54];
}
// start from bottom row
for(int y = this.height-1; y >= 0; y--) {
for(int x = 0; x < this.width; x++) {
int index = (this.width*y + x) * 3;
int b = iData[index];
int g = iData[index+1];
int r = iData[index+2];
//System.out.format("R: %s\nG: %s\nB: %s\n\n", r, g, b);
// merge rgb values to single int
int rgb = ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);
// build image from bottom up
this.image.setRGB(x, this.height-1-y, rgb);
}
}
我正在从位图中读取 RGB 值。我的 iData 字节数组是正确的,因为我已经对照十六进制编辑器对其进行了检查。但是,当我运行这个循环时,我的输出图像会变形(见图)。为了解决这个问题,我已经绞尽脑汁好几个小时了,为什么会这样?
输入图像是加拿大国旗。
输出图像:
【问题讨论】:
-
您的输入文件是否有 4 字节对齐的行?某些格式会在每行的末尾添加填充,以使每行的长度为 4 字节的倍数。
-
因为你可能没有考虑到这一点。
-
@Tetramputechture 这意味着你需要使用不同的值来乘以y。
-
你没事,我已经解决了。这一切都源于我认为我不需要调整后的宽度,所以我将它从循环中删除,但我的调试方法不正确,所以我没有看到结果。谢谢,伙计们。