Skip to content

ngoanh2n/image-comparator

Repository files navigation

Java Maven GitHub Actions CircleCI

Table of Contents

Declaration

Gradle

Add dependency to build.gradle.

implementation("com.github.ngoanh2n:image-comparator:1.5.0")

Maven

Add dependency to pom.xml.

<dependency>
    <groupId>com.github.ngoanh2n</groupId>
    <artifactId>image-comparator</artifactId>
    <version>1.5.0</version>
</dependency>

Usage

Comparison

  1. Compare 2 images
    ImageComparisonOptions options = ImageComparisonOptions
            .builder()
            .allowedDeviation(0.05)         // Default to 0.0
            .diffColor(Color.CYAN)          // Default to Color.RED
            .build();
    ImageComparisonResult result = ImageComparator.compare(expectedImage, actualImage, options);
  2. Compare 2 image directories
    Path expectedImageDir = Paths.get("data/expected");
    Path actualImageDir = Paths.get("data/actual");
            
    ImageComparisonOptions options = ImageComparisonOptions
            .builder()
            .allowedDeviation(0.05)         // Default to 0.0
            .diffColor(Color.CYAN)          // Default to Color.RED
            .build();
    ImageBulkComparisonResult result = ImageComparator.compare(expectedImageDir, actualImageDir, options);

Result

ImageComparisonResult is the result of ImageComparator.compare(expectedImage, actualImage, options).

boolean isDifferent = ImageComparisonResult.isDifferent();
int diffSize = ImageComparisonResult.getDiffSize();
BufferedImage diffImage = ImageComparisonResult.getDiffImage();
double allowedDeviation = ImageComparisonResult.getAllowedDeviation();
double currentDeviation = ImageComparisonResult.getCurrentDeviation();

ImageBulkComparisonResult is the result of ImageComparator.compare(expectedImageDir, actualImageDir, options).

boolean hasDiff = ImageBulkComparisonResult.hasDiff();
int diffTotal = ImageBulkComparisonResult.getDiffTotal();
List<ImageComparisonResult> diffResults = ImageBulkComparisonResult.getDiffResults();

Visitor

ImageComparisonVisitor for walking through ImageComparator.

  • ImageComparisonVisitor.comparisonStarted(ImageComparisonOptions, BufferedImage, BufferedImage)
  • ImageComparisonVisitor.comparisonFinished(ImageComparisonOptions, BufferedImage, BufferedImage, ImageComparisonResult)

Output

ImageComparisonOutput for writing comparison output files to specified location.
An implementation of ImageComparisonVisitor.

  • The output is always created at build/ngoanh2n/img/{yyyyMMdd.HHmmss.SSS} by default
  • Use ImageComparisonResultOptions to adjust the output behaviors. And set to ImageComparisonOptions
    ImageComparisonResultOptions resultOptions = ImageComparisonResultOptions
           .builder()
           .writeOutputs(false)                       // Default to true
           //.location(Paths.get("build/custom"))     // Default to build/ngoanh2n/img
           .build();
    ImageComparisonOptions options = ImageComparisonOptions
           .builder()
           .resultOptions(resultOptions)              // Default to ImageComparisonResultOptions.defaults()
           .build();

Allure Report

When using Allure as a report framework, should use com.github.ngoanh2n:image-comparator-allure.
image-comparator-allure README.