diff --git a/README.md b/README.md index b592bed..ae29971 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ Published on Maven Central and jCenter Java Library that compares 2 images with Article about growing `image-comparison` on habr: [How did the test task become a production library](https://habr.com/ru/post/475482/) -## Configurations +## Configuration +All these configurations can be updated based on your needs. | *Property* | *Description* | | --- | --- | | `threshold` | The threshold which means the max distance between non-equal pixels. Could be changed according size and requirements to the image. | @@ -68,84 +69,53 @@ Can be found in [RELEASE_NOTES](RELEASE_NOTES.md). com.github.romankh3 image-comparison - 4.3.0 + 4.3.1 ``` #### Gradle ```groovy -compile 'com.github.romankh3:image-comparison:4.3.0' +compile 'com.github.romankh3:image-comparison:4.3.1' ``` #### To compare two images programmatically +##### Default way to compare two images looks like: ```java -class Example { - public static void main( String[] args ) { - // load the images to be compared - BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); - BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); - - // where to save the result (leave null if you want to see the result in the UI) - File resultDestination = new File( "result.png" ); - - //Create ImageComparison object for it. - ImageComparison imageComparison = new ImageComparison( expectedImage, actualImage, resultDestination ); - - //Can be used another constructor for it, without destination. - new ImageComparison("expected.png", "actual.png"); - //or - new ImageComparison(expectedImage, actualImage); - - //Also can be configured BEFORE comparing next properties: - - //Threshold - it's the max distance between non-equal pixels. By default it's 5. - imageComparison.setThreshold(10); - imageComparison.getThreshold(); - - //RectangleListWidth - Width of the line that is drawn in the rectangle. By default it's 1. - imageComparison.setRectangleLineWidth(5); - imageComparison.getRectangleLineWidth(); - - //DifferenceRectangleFilling - Fill the inside the difference rectangles with a transparent fill. By default it's false and 20.0% opacity. - imageComparison.setDifferenceRectangleFilling(true, 30.0); - imageComparison.isFillDifferenceRectangles(); - imageComparison.getPercentOpacityDifferenceRectangles(); - - //ExcludedRectangleFilling - Fill the inside the excluded rectangles with a transparent fill. By default it's false and 20.0% opacity. - imageComparison.setExcludedRectangleFilling(true, 30.0); - imageComparison.isFillExcludedRectangles(); - imageComparison.getPercentOpacityExcludedRectangles(); - - //Destination. Before comparing also can be added destination file for result image. - imageComparison.setDestination(resultDestination); - imageComparison.getDestination(); - - //MaximalRectangleCount - It means that would get first x biggest rectangles for drawing. - // by default all the rectangles would be drawn. - imageComparison.setMaximalRectangleCount(10); - imageComparison.getMaximalRectangleCount(); - - //MinimalRectangleSize - The number of the minimal rectangle size. Count as (width x height). - // by default it's 1. - imageComparison.setMinimalRectangleSize(100); - imageComparison.getMinimalRectangleSize(); - - //Change the level of the pixel tolerance: - imageComparison.setPixelToleranceLevel(0.2); - imageComparison.getPixelToleranceLevel(); - - //After configuring the ImageComparison object, can be executed compare() method: - ImageComparisonResult imageComparisonResult = imageComparison.compareImages(); - - //Can be found ComparisonState. - ImageComparisonState imageComparisonState = imageComparisonResult.getImageComparisonState(); - - //And Result Image - BufferedImage resultImage = imageComparisonResult.getResult(); - - //Image can be saved after comparison, using ImageComparisonUtil. - ImageComparisonUtil.saveImage(resultDestination, resultImage); - } -} + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); + + //Create ImageComparison object and compare the images. + ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).compareImages(); + + //Check the result + assertEquals(ImageComparisonState.MATCH, imageComparisonResult.getImageComparisonState()); +``` + +##### Save result image +To save result image, can be used two ways: +1. add a file to save to constructor. ImageComparison will save the result image in this case. +```java + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); + + // where to save the result (leave null if you want to see the result in the UI) + File resultDestination = new File( "result.png" ); + + //Create ImageComparison object with result destination and compare the images. + ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage, resultDestination).compareImages(); +``` +2. execute ImageComparisonUtil.saveImage static method +```java + //load images to be compared: + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); + + //Create ImageComparison object with result destination and compare the images. + ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).compareImages(); + + //Image can be saved after comparison, using ImageComparisonUtil. + ImageComparisonUtil.saveImage(resultDestination, resultImage); ``` ## Demo diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 806d486..045b416 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,8 @@ # Release Notes +## 4.3.1 +* Fixed bug #201 - problem with comparing totally different pictures. + ## 4.3.0 * Include rectangles in ImageComparisonResult. * The resolved bug with 0.0 in differencePercent diff --git a/build.gradle b/build.gradle index f612b13..9178e96 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group 'com.github.romankh3' -version '4.3.0' +version '4.3.1' description 'Library that compares 2 images with the same sizes and shows the differences visually by drawing rectangles. ' + 'Some parts of the image can be excluded from the comparison. Can be used for automation qa tests.' sourceCompatibility = 1.8 diff --git a/pom.xml b/pom.xml index ce8f537..429ddd6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.romankh3 image-comparison - 4.3.0 + 4.3.1 jar Image Comparison diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java index 1985ff1..6e1320e 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -288,7 +288,7 @@ private List populateRectangles() { counter++; } - return mergeRectangles(rectangles); + return mergeRectangles(mergeRectangles(rectangles)); } /** @@ -346,10 +346,13 @@ private void updateRectangleCreation(Rectangle rectangle, int x, int y) { private List mergeRectangles(List rectangles) { int position = 0; while (position < rectangles.size()) { + if (rectangles.get(position).equals(Rectangle.createZero())) { + position++; + } for (int i = 1 + position; i < rectangles.size(); i++) { Rectangle r1 = rectangles.get(position); Rectangle r2 = rectangles.get(i); - if (r1.equals(Rectangle.createZero())) { + if (r2.equals(Rectangle.createZero())) { continue; } if (r1.isOverlapping(r2)) { diff --git a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java index 6454f73..44c73fe 100644 --- a/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java +++ b/src/test/java/com/github/romankh3/image/comparison/ImageComparisonUnitTest.java @@ -153,6 +153,21 @@ public void shouldReproduceBug17() { assertNotNull(imageComparisonResult.getResult()); } + @DisplayName("Should reproduce bug 201") + @Test + public void shouldReproduceBug201() { + //given + BufferedImage expectedResultImage = readImageFromResources("result#201.png"); + + //when + ImageComparisonResult imageComparisonResult = + new ImageComparison("expected#201.png", "actual#201.png").compareImages(); + + //then + assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState()); + assertImagesEqual(expectedResultImage, imageComparisonResult.getResult()); + } + @DisplayName("Should reproduce bug 21") @Test public void shouldReproduceBug21() { diff --git a/src/test/resources/actual#201.png b/src/test/resources/actual#201.png new file mode 100644 index 0000000..2cf8123 Binary files /dev/null and b/src/test/resources/actual#201.png differ diff --git a/src/test/resources/expected#201.png b/src/test/resources/expected#201.png new file mode 100644 index 0000000..f1ad0c2 Binary files /dev/null and b/src/test/resources/expected#201.png differ diff --git a/src/test/resources/result#201.png b/src/test/resources/result#201.png new file mode 100644 index 0000000..e8e4fcc Binary files /dev/null and b/src/test/resources/result#201.png differ