在屏幕上查找给定图像的程序

时间:2019-03-29 05:02:21

标签: java image

我用Java编写了一个小程序,以在屏幕上查找给定的图像并返回其坐标。该程序可以正常编译,但是由于某些原因,它无法找到该图像。

它是这样的:

程序接收缓冲的图像作为输入

图片应该是这样的

image would be something like this

下面的代码截取屏幕截图

screen= new Robot().createScreenCapture(new r 
                     rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

示例屏幕截图:

sample screenshot of screen

(理想情况下,该程序将返回google chrome图标的坐标)

程序尝试通过遍历屏幕中的每个像素来定位图像,并尝试通过Quad for Loop将其与给定图像的像素进行匹配

for(int x = 0; x< screen.getWidth()-image.getWidth();x++){
     for(int y = 0; y< screen.getHeight()-image.getHeight();y++) {                                                   
           w=x,h=y;
           //a new rectangle is "drawn" reset the flag
           for(int a = 0; a< image.getWidth();a++){
              for(int b = 0; b< image.getHeight();b++){
                  //checks that pixel in screen matches the same pixel in the 
                  //image. if yes increment h value. if no change flag to true
                  //and breaks the loop
              }
              //checks the flag if true breaks loop other wise increment w 
              //value value
           }
           //here is the end of the "rectangle trial", if the image checks out 
           //the coordinate (w,h) will be the bottom right pixel of the image in 
           //screen. take that coordinate and return it
     }
}
after checking every pixels return coordinate (-1,-1) so that i know image doesnt exisit in the screen

这个想法是我将屏幕上的每个像素(除了右边缘和下边缘的像素)与图像中的像素进行匹配。 (想象从电源线(0,0)开始为屏幕的每个像素绘制一个图像大小的矩形,如果矩形中的任何像素与图像中相同坐标的像素都不匹配,则for循环将会中断,程序将移至屏幕上的下一个像素)

以下代码是for循环内的内容

for(int x = 0; x< screen.getWidth()-image.getWidth();x++){
            for(int y = 0; y< screen.getHeight()-image.getHeight();y++){//screen sized iteration
                f=x;
                g=y;
                flag=false;
                for(int a = 0; a< image.getWidth();a++){
                    for(int b = 0; b< image.getHeight();b++){//image sized 
                        if(screen.getRGB(f,g) != image.getRGB(a,b)){
                            flag=false;
                            break;
                        }
                        else{
                            g++;
                        }
                    }
                    if(flag){
                        break;
                    }else{
                        f++;
                    }
                }
                if(!flag) {
                    returnCoords=new Coords(f-image.getWidth()/2,g-                
                    image.getHeight()/2);
                    return returnCoords;
                }
            }

        }

        return returnCoords;
    }

程序应该输出图像的中心坐标(如果它是屏幕的一部分)。如果它不是屏幕的一部分,程序将返回(-1.0,-1.0)。但是,无论我输入什么图像(44.0,-50.0)都是返回的坐标

下面是编译后的代码

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

public class ImageScanner {     
    public static void main(String[] args) throws AWTException, IOException{
        BufferedImage image = ImageIO.read(new File("C:\\Users\\Tony\\Desktop\\my projects\\Capture.PNG"));
        Coords x=ImageScanner.scanImage(image);

        Robot robot = new Robot();
        robot.mouseMove((int)(x.getX()),(int)(x.getY()));
    }
    public static class Coords{
        int x;
        int y;
        public Coords(int width,int height){
            x=width;
            y=height;
        }
        public int getX() {
            return this.x;
        }

        public int getY() {
            return this.y;
        }

    }
    public static Coords scanImage(BufferedImage image) throws IOException, HeadlessException, AWTException {

        BufferedImage screen=null;

        screen= new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

        int f=0;
        int g=0;
        int mismatched=0;

        ImageScanner.Coords returnCoords = new ImageScanner.Coords(-1,-1);

        for(int x = 0; x< screen.getWidth()-image.getWidth();x++){
            for(int y = 0; y< screen.getHeight()-image.getHeight();y++){//screen sized iteration
                f=x;
                g=y;
                mismatched=0;

                for(int a = 0; a< image.getWidth();a++){
                    for(int b = 0; b< image.getHeight();b++){//image sized 
                        if(screen.getRGB(f,g) != image.getRGB(a,b)){
                            mismatched++;
                        }
                        else{
                            g++;
                        }
                    }
                    g=y;
                    f++;
                }

                if((mismatched/((float)image.getWidth()*(float)image.getHeight()))<0.2) {
                    returnCoords=new Coords(f-image.getWidth()/2,g+image.getHeight()/2);
                    return returnCoords;
                }
            }

        }

        return returnCoords;
    }

}

编辑:谢谢,程序现在可以正常工作

output i got

0 个答案:

没有答案