How to scan the screen for a specific color/image in java?

后端 未结 1 974
面向向阳花
面向向阳花 2021-01-24 03:41

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

I know that this will probably include taking a screen

相关标签:
1条回答
  • 2021-01-24 04:33

    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));
            }
        }
    }
    
    ...
    
    0 讨论(0)
提交回复
热议问题