Compare image to actual screen

后端 未结 4 1220
萌比男神i
萌比男神i 2021-01-16 23:03

I\'d like to make my Java program compare the actual screen with a picture (screenshot).

I don\'t know if it\'s possible, but I have seen it in Jitbit (a macro recor

相关标签:
4条回答
  • 2021-01-16 23:23

    Have a look at Sikuli project. Their automation engine is based on image comparison.

    I guess, internally they are still using OpenCV for calculating image similarity, but there are plenty of OpenCV Java bindings like this, which allow to do so from Java.

    Project source code is located here: https://github.com/sikuli/sikuli

    0 讨论(0)
  • 2021-01-16 23:34

    You can do this in two steps:

    1. Create a screenshot using awt.Robot

      BufferedImage image = new Robot().createScreenCapture(new Rctangle(Toolkit.getDefaultToolkit().getScreenSize()));
      ImageIO.write(image, "png", new File("/screenshot.png"));
      
    2. Compare the screenshots using something like that: How to check if two images are similar or not using openCV in java?

    0 讨论(0)
  • 2021-01-16 23:39

    Ok then, so I found an answer after a few days.

    This method takes the screenshot:

    public static void takeScreenshot() {
            try {
                BufferedImage image = new Robot().createScreenCapture(new Rectangle(490,490,30,30));
    /* this two first parameters are the initial X and Y coordinates. And the last ones are the increment of each axis*/
                ImageIO.write(image, "png", new File("C:\\Example\\Folder\\capture.png"));
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HeadlessException e) {
                e.printStackTrace();
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    

    And this other one will compare the images

    public static String compareImage() throws Exception {
    
    // savedImage is the image we want to look for in the new screenshot.
    // Both must have the same width and height
    
        String c1 = "savedImage";
        String c2 = "capture";
    
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(c1
                + ".png"));
        BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(
                c2 + ".png"));
        int i, j;
        int k = 1;
        while (((i = in.read()) != -1) && ((j = in1.read()) != -1)) {
            if (i != j) {
                k = 0;
                break;
            }
        }
    
        in.close();
        in1.close();
        if (k == 1) {
    
            System.out.println("Ok...");
            return "Ok";            
    
        } else {
            System.out.println("Fail ...");
            return "Fail";
        }
    }
    
    0 讨论(0)
  • 2021-01-16 23:46

    You may try aShot: documentation link

    1) aShot can ignore areas you mark with special color.

    2) aShot can provide image which display difference between images.

    private void compareTowImages(BufferedImage expectedImage, BufferedImage actualImage) {
        ImageDiffer imageDiffer = new ImageDiffer();
        ImageDiff diff = imageDiffer
                .withDiffMarkupPolicy(new PointsMarkupPolicy()
                        .withDiffColor(Color.YELLOW))
                .withIgnoredColor(Color.MAGENTA)
                .makeDiff(expectedImage, actualImage);
    
        // areImagesDifferent will be true if images are different, false - images the same
        boolean areImagesDifferent = diff.hasDiff();
        if (areImagesDifferent) {
            // code in case of failure 
        } else {
            // Code in case of success
        }
    }
    

    To save image with differences:

    private void saveImage(BufferedImage image, String imageName) {
        // Path where you are going to save image
        String outputFilePath = String.format("target/%s.png", imageName);
        File outputFile = new File(outputFilePath);
        try {
            ImageIO.write(image, "png", outputFile);
        } catch (IOException e) {
            // Some code in case of failure
        }
    }
    
    0 讨论(0)
提交回复
热议问题