【发布时间】:2014-10-28 12:46:45
【问题描述】:
我正在编辑一个 BufferedImage。
更改图片中的像素后,我会检查以确保新值与我预期的一致。但是,它们并没有更改为指定的像素颜色!
我认为这可能与 Alpha 值有关,因此我最近添加了一个从原始像素中提取 Alpha 值的步骤,并确保在创建要插入图像的新颜色时使用该值.
System.out.println(newColors[0] + ", " + newColors[1] + ", " + newColors[2]);
Color oldColor = new Color(image.getRGB(x, y));
Color newColor = new Color(newColors[0], newColors[1], newColors[2], oldColor.getAlpha()); // create a new color from the RGB values.
image.setRGB(x, y, newColor.getRGB());// set the RGB of the pixel in the image.
for (int col : getRGBs(x,y)) {
System.out.println(col);
}
方法getRGBs()返回一个数组,其中
- 索引 0 是红色值
- 索引 1 为绿色
- 索引 2 为蓝色。
输出如下:
206, 207, 207
204
203
203
如您所见,值206, 207, 207 从图像中返回为204, 203, 203 - 事实上,我更改的每个像素都返回为204, 203, 203。
我究竟做错了什么?这没有任何意义。
提前致谢!
【问题讨论】:
-
为什么不在
image.setRGB(x, y, newColor.getRGB());之后使用image.getRGB(x, y)来检查颜色是否正常?如果他们没问题,那么错误就在getRGBs(x,y) -
贴出方法
getRGBs()的代码。可能有什么问题。 -
getRGBs() 就像你说的那样,Ezequiel。这就是问题所在,它返回了我没有放入图像的数字!谢谢。
-
Ortis,getRGBs()就可以了,很简单的方法。我现在想出了一个解决方案,但是谢谢!
标签: java bufferedimage