Compare image to actual screen

后端 未结 4 1224
萌比男神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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
        }
    }
    

提交回复
热议问题