Skip to content

Commit

Permalink
4.3.1-SNAPSHOT (#198)
Browse files Browse the repository at this point in the history
4.3.1: Fix bug #201
  • Loading branch information
Roman Beskrovnyi committed Mar 13, 2021
1 parent e494e45 commit 1d87642
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 75 deletions.
112 changes: 41 additions & 71 deletions README.md
Expand Up @@ -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. |
Expand Down Expand Up @@ -68,84 +69,53 @@ Can be found in [RELEASE_NOTES](RELEASE_NOTES.md).
<dependency>
<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>4.3.0</version>
<version>4.3.1</version>
</dependency>
```
#### 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
Expand Down
3 changes: 3 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>4.3.0</version>
<version>4.3.1</version>
<packaging>jar</packaging>

<name>Image Comparison</name>
Expand Down
Expand Up @@ -288,7 +288,7 @@ private List<Rectangle> populateRectangles() {
counter++;
}

return mergeRectangles(rectangles);
return mergeRectangles(mergeRectangles(rectangles));
}

/**
Expand Down Expand Up @@ -346,10 +346,13 @@ private void updateRectangleCreation(Rectangle rectangle, int x, int y) {
private List<Rectangle> mergeRectangles(List<Rectangle> 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)) {
Expand Down
Expand Up @@ -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() {
Expand Down
Binary file added src/test/resources/actual#201.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/expected#201.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/result#201.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1d87642

Please sign in to comment.