在BufferedImage的屏幕上查找位置?

时间:2012-04-07 11:30:42

标签: java image awtrobot

我试图在屏幕上找到某个图像的位置,但我对Java很新,所以我想我可能搞砸了..这就是我试过的:

public Point getLocationOfImage(int[][] pixels){
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Robot robot;
    Point point = new Point(0, 0);;
    boolean success = false;
    try {
        robot = new Robot();
    } catch (AWTException e) {
        robot = null;
        e.printStackTrace();
    }
    Dimension screenDimensions = toolkit.getScreenSize();
    retry:
    for(int i = 0; i < screenDimensions.width; i++){
        for(int i2 = 0; i2 < screenDimensions.height; i2++){
            //check region
            for(int j = 0; j < 30; j++){
                for(int j2 = 0; j2 < 30; j2++){
                    BufferedImage test = robot.createScreenCapture(new Rectangle(i + j, i2 + j2, 30, 30));
                    toDrawG.drawImage(test, 30, 0, null);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(test.getRGB(i + j, i2 + j2) == pixels[j][j2]){
                        continue;}
                    else{
                        break retry;
                    }
                }
            }
        }
    }
    return point;
    }

所以无论如何似乎根本不起作用,而像素[] []来自另一个函数:

public static int[][] getPixels(BufferedImage i){
    int temp[][] = new int[i.getWidth()][i.getHeight()];
    for(int n = 0; n < i.getWidth(); n++){
        for(int n2 = 0; n2 < i.getHeight(); n2++){
            temp[n][n2] = i.getRGB(n, n2);
        }   
    }
    return temp;
}

但就我而言,这个功能运作良好..有什么我想念的吗?我相信这里有人可以在几分钟内发现我的错误..

0 个答案:

没有答案