如何在java中扫描屏幕以获得特定的颜色/图像



我需要在屏幕上扫描特定的图像/颜色,并返回该颜色出现的位置的x和y坐标。

我知道这可能包括使用Robot类进行屏幕截图,但不知道如何适当地扫描图像。

如果使用Robot类进行屏幕截图,则会得到BuffereImage类的对象。然后循环宽度和高度(getWidth(),getHeight())。使用getRGB()方法,可以提取像素的RGB值。如果匹配,您可以将其存储在一个集合或数组中。

BufferedImage img = ...
int matchColor = Color.RED.getRGB();
int h = img.getHeight();
int w = img.getWidth();
Set<Point> points = new HashSet<Point>();
for(int i = 0 ; i < w ; i++) {
    for(int j = 0 ; j < h ; j++) {
        if(img.getRGB(i, j) == matchColor) {
            points.add(new Point(i, j));
        }
    }
}
...

最新更新