Robot.createScreenCapture 正在改变颜色
这是我编写的用于截屏的类:
public class imagemanipulation {
Dimension screenResolution;
Rectangle screenRectangle;
Robot robot;
imagemanipulation() {
try {
screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
screenRectangle = new Rectangle(screenResolution);
robot = new Robot();
} catch (AWTException ex) {
Logger.getLogger(imagemanipulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
public BufferedImage newScreenshot() {
BufferedImage image = robot.createScreenCapture(screenRectangle);
return image;
}
}
我正在做的是在从上面截取的屏幕截图以及它到另一个屏幕的一部分的坐标上使用 BufferedImagewhatever.getRGB(x,y)
图像,然后返回找到匹配项的 x 和 y 位置。这对于黑白图像非常有用,但不适用于彩色图像。我遇到的问题是 createScreenCapture
正在更改屏幕截图的 RGB 颜色,因此当我将使用 createScreenCapture
拍摄的图像与另一个图像进行比较时,从来不工作。
我认为 createScreenCapture 必须减少屏幕截图中的颜色数量才能提高效率,但是有什么方法可以阻止它这样做吗?
Here's the class I wrote to take screenshots:
public class imagemanipulation {
Dimension screenResolution;
Rectangle screenRectangle;
Robot robot;
imagemanipulation() {
try {
screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
screenRectangle = new Rectangle(screenResolution);
robot = new Robot();
} catch (AWTException ex) {
Logger.getLogger(imagemanipulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
public BufferedImage newScreenshot() {
BufferedImage image = robot.createScreenCapture(screenRectangle);
return image;
}
}
What I am doing is using BufferedImagewhatever.getRGB(x,y)
on both a screenshot of the screen taken from the above, and coordinates of it to part of another image, then returning the x and y position where a match is found. This works great for black and white images but not colored. The problem I'm having is that createScreenCapture
is changing the RGB colors of the screenshot from what they were originally, so when I compare an image taken with createScreenCapture
to another image it never works.
I think createScreenCapture
must be reducing the amount of colors in the screenshots it takes as to be more efficient, but is there any way I can stop it from doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为参考,此示例允许用户单击并拖动到桌面上的某一点,显示周围屏幕的放大图像。捕获后,当鼠标在捕获的图像上移动时,颜色会显示在工具提示中。我已经使用我的平台上的实用程序验证了结果是正确的。您可以将其与您的程序和结果进行比较。
For reference, this example allows one to click and drag to a point on the desktop, displaying an enlarged image of the surrounding screen. Once captured, the color is displayed in a tool tip as the mouse moves over the captured image. I have verified that the result is correct using a utility on my platform. You might compare it to your program and result.