为什么这段代码运行如此缓慢?

Nat*_*han 2 java performance awt awtrobot

我正在尝试使用这些方法在屏幕的矩形区域中查找颜色。但是有时屏幕上有数百万个像素,而我现在仅每秒实现约35次getColor迭代。我的代码中一定有某些东西导致它运行非常缓慢。

我怎么能比这更快地扫描屏幕?理想情况下,我想在不到一秒钟的时间内扫描整个屏幕以获取一种颜色,而不是现在的8个小时:P

这是我的两种方法。

public static int getColor(int x, int y){
    try{
        return(robot.getPixelColor(x, y).getRGB() * -1);
    }catch(Exception e){
        System.out.println("getColor ERROR");
        return 0;
    }
}

//returns first instance of color,
//Begins top left, works way down to bottom right
public static Point findColor(Box searchArea, int color){
    System.out.println("Test");
    if(searchArea.x1 > searchArea.x2){
        int temp = searchArea.x1;
        searchArea.x1 = searchArea.x2;
        searchArea.x2 = temp;
    }
    if(searchArea.y1 > searchArea.y2){
        int temp = searchArea.y1;
        searchArea.y1 = searchArea.y2;
        searchArea.y2 = temp;
    }
    for(int i = searchArea.x1;i <=searchArea.x2; i++){
        for(int j = searchArea.y1;j<=searchArea.y2;j++){
            if(getColor(i, j) == color){
                return new Point(i, j);

            }
            System.out.println(i + " " + j);
        }
    }
    return new Point(-1, -1);
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

Robot#getColor 会非常慢,尤其是在以这种方式使用时。

更好的解决方案是抓取屏幕截图(即使是小块)并处理结果BufferedImage

使用以下示例,我得到了...

Took 0 seconds to scan image
Took 3 seconds to scan screen
Run Code Online (Sandbox Code Playgroud)

对于面积 10x10

示例代码...

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestColorGrab {

    private static Robot robot;

    public static void main(String[] args) {
        try {
            robot = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(TestColorGrab.class.getName()).log(Level.SEVERE, null, ex);
        }
        new TestColorGrab();
    }

    public TestColorGrab() {
        Rectangle bounds = new Rectangle(0, 0, 10, 10);
        long start = System.currentTimeMillis();
        scanImageArea(bounds);
        System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000), TimeUnit.SECONDS) + " seconds to scan image");

        start = System.currentTimeMillis();
        scanRobotArea(bounds);
        System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000) + " seconds to scan screen");
    }

    public static int getColor(int x, int y) {
        try {
            return (robot.getPixelColor(x, y).getRGB() * -1);
        } catch (Exception e) {
            System.out.println("getColor ERROR");
            return 0;
        }
    }

    public static void scanRobotArea(Rectangle searchArea) {
        for (int i = searchArea.x; i < searchArea.x + searchArea.width; i++) {
            for (int j = searchArea.y; j < searchArea.y + searchArea.height; j++) {
                getColor(i, j);
            }
        }
    }

    public static void scanImageArea(Rectangle searchArea) {
        BufferedImage image = robot.createScreenCapture(searchArea);
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                image.getRGB(x, y);
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)