我正尝试在Mac OS X中创建类似于DigitalColor Meter的应用程序。 (注意:DigitalColor Meter是Mas OS的一个小应用程序,用于选取鼠标光标下的像素颜色)。为什么AWTRobot getPixelColor()会给出不同的结果?
为了在Java中实现它,我尝试过使用AWTRobot getPixelColor(),但getPixelColor()似乎效率不高。下面是我的代码:
public class AWT_Robot {
public static void main(String[] args) {
int mouseX = 0;
int mouseY = 0;
try {
Robot robot = new Robot();
while(true){
robot.delay(1000);
mouseX = MouseInfo.getPointerInfo().getLocation().x;
mouseY = MouseInfo.getPointerInfo().getLocation().x;
Color color = robot.getPixelColor(mouseX, mouseY);
System.out.println("x: "+mouseX+" y: "+mouseY+" RGB: ("+color.getRed()+", "+color.getGreen()+", "+color.getBlue()+")");
}
} catch (AWTException e) {
e.printStackTrace();
}
}
}
当我徘徊鼠标滑过红色图像(RGB:243,0,0),它具有不同的RGB如下打印:
x: 313 y: 313 RGB: (239, 0, 0)
x: 313 y: 313 RGB: (239, 0, 0)
x: 294 y: 294 RGB: (239, 0, 0)
x: 186 y: 186 RGB: (79, 116, 163)
x: 104 y: 104 RGB: (67, 104, 154)
x: 116 y: 116 RGB: (79, 117, 164)
x: 159 y: 159 RGB: (68, 105, 155)
1)有什么事情成为这个问题背后的原因?
2)还有什么其他的方式来实现Java应用程序(DigitalColor Meter)吗?
我在下面的链接中发现了类似的问题,但他们都没有看到我期待的答案。
java robot.getPixelColor(x,y) question
awtrobot reads incorrect colors
How does Robot's getPixelColor(int x, int y) method work?
Ooops ..愚蠢的错误...:P – Angom