【问题标题】:How to scan the screen for a specific color/image in java?如何在java中扫描屏幕以获取特定颜色/图像?
【发布时间】:2015-02-10 17:08:19
【问题描述】:

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

我知道这可能包括使用 Robot 类截取屏幕截图,但不知道如何正确扫描该图像。

【问题讨论】:

标签: java colors


【解决方案1】:

如果你用 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));
        }
    }
}

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2014-04-08
    • 2018-09-07
    • 1970-01-01
    • 2013-07-05
    • 2011-09-22
    相关资源
    最近更新 更多