缓冲图像和createScreenCapture 产生错误的颜色
在我的 Java 程序中,我需要分析给定坐标中像素的颜色。由于我需要经常这样做,因此我首先捕获屏幕的一部分,然后获取像素颜色。我正在这样做:
BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,100,100));
...
pxcolor = GetPixelColor(bi,x,y);
...
ImageIO.write(bi, "bmp", new File("myScreenShot.bmp"));
GetPixelColor 函数非常明显:
public Color GetPixelColor(BufferedImage b, int x, int y)
{
int pc = b.getRGB(x, y);
Color ccc = new Color(pc,true);
int red = (pc & 0x00ff0000) >> 16; // for testing
int green = (pc & 0x0000ff00) >> 8; // for testing
int blue = pc & 0x000000ff; // for testing
return ccc;
}
出于测试目的,我创建了一个 纯粉色图片 (RGB(255,0,255))。问题是 即使像素是纯粉色,该函数也会返回类似 RGB(250,61,223) 的内容,并在那里测试变量红色、绿色和蓝色。此外,保存的文件(myScreenShot.bmp)看起来完全不同。
我做错了什么。它可能与 ColorModel 有关吗?
UPD:从 bi 获取 DataBuffer 似乎不会产生正确的结果 - 生成的 DataBuffer 的第一个元素等于“-2105371”。我不知道减号从哪里来,但如果我将其转换为十六进制,我会得到类似“FFFFFFFFFFDFFDFE5”的内容。真实像素 RGB 是 (E5,E5,EB),缓冲区已损坏,取而代之的是 RGB(DF,DF,E5)。这已经让我抓狂了。
In my Java program I need to analyze a color of a pixel in given coordinates. Because of the fact that I need to do it often, first I capture a part of the screen, and then get a pixel color. I am doing this with:
BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,100,100));
...
pxcolor = GetPixelColor(bi,x,y);
...
ImageIO.write(bi, "bmp", new File("myScreenShot.bmp"));
The GetPixelColor function is quite obvious:
public Color GetPixelColor(BufferedImage b, int x, int y)
{
int pc = b.getRGB(x, y);
Color ccc = new Color(pc,true);
int red = (pc & 0x00ff0000) >> 16; // for testing
int green = (pc & 0x0000ff00) >> 8; // for testing
int blue = pc & 0x000000ff; // for testing
return ccc;
}
For testing purposes I have created a pure pink picture (RGB(255,0,255)). The problem is that
even if the pixel is pure pink, the function returns something like RGB(250,61,223) as well as testing variables red, green and blue there. Also, the saved file (the myScreenShot.bmp) looks quite different.
What am I doing wrong. Could it be somehow ColorModel related?
UPD: getting the DataBuffer from bi doesn't seems to produce right results - the
first element of produced DataBuffer is equal to "-2105371". I don't know from where came minus sign, but if I transform it to HEX I will get something like "FFFFFFFFFFDFDFE5". The real pixel RGB is (E5,E5,EB), and the buffer is already corrupted, having instead RGB(DF,DF,E5). This drives me nuts already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是由于颜色模型造成的。
根据这个code 它使用
DirectColorModel
(见下文),无论屏幕的颜色深度如何。It is most likely due to the color model.
According to this code it uses a
DirectColorModel
(see below) regardless of your color depth of your screen.