如何在java中扫描特定颜色/图像的屏幕? [英] How to scan the screen for a specific color/image in java?

查看:162
本文介绍了如何在java中扫描特定颜色/图像的屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to scan the screen for a specific image/color and return an x and y coordinate for where that color occurs.

我知道这可能包括

推荐答案

如果您截取屏幕截图,请使用Robot类别截取屏幕截图,但不知道如何适当地扫描该图像。与Robot类,你得到一个类BuffereImage的对象。然后你for循环的宽度和高度(getWidth(),getHeight())。使用getRGB()方法,您可以提取像素的RGB值。如果匹配,您可以将其存储在aan集合或数组中。

If you take a screenshot with the Robot class, you get an object of the class BuffereImage. Then you for loop the width and height (getWidth(), getHeight()). With the getRGB() method you can extract the RGB value of the pixel. If it matches, you can store it in aan collection or array.

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));
        }
    }
}

...

这篇关于如何在java中扫描特定颜色/图像的屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆