在我的 Java 程序中,我需要分析给定坐标中像素的颜色。由于我需要经常这样做,因此我首先捕获屏幕的一部分,然后获取像素颜色。我正在这样做:
BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,100,100));
...
pxcolor = GetPixelColor(bi,x,y);
...
ImageIO.write(bi, "bmp", new File("myScreenShot.bmp"));
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我创建了一张纯粉色图片(RGB(255,0,255))。问题是,即使像素是纯粉色,该函数也会返回类似 RGB(250,61,223) 的内容,并在那里测试变量红色、绿色和蓝色。此外,保存的文件(myScreenShot.bmp)看起来也很不同。
我究竟做错了什么。它可能与 …