|
| 1 | +package com.github.vedenin.imagecomparison; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import com.github.romankh3.image.comparison.ImageComparison; |
| 6 | +import com.github.romankh3.image.comparison.ImageComparisonUtil; |
| 7 | +import com.github.romankh3.image.comparison.model.ComparisonResult; |
| 8 | +import com.github.romankh3.image.comparison.model.ComparisonState; |
| 9 | + |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.URISyntaxException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Sample of the using Image-Comparison library. |
| 17 | + */ |
| 18 | +public class Sample { |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException, URISyntaxException { |
| 21 | + // load the images to be compared |
| 22 | + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); |
| 23 | + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png"); |
| 24 | + |
| 25 | + // where to save the result (leave null if you want to see the result in the UI) |
| 26 | + File resultDestination = new File("result.png"); |
| 27 | + |
| 28 | + //Create ImageComparison object for it. |
| 29 | + ImageComparison imageComparison = new ImageComparison(expectedImage, actualImage, resultDestination); |
| 30 | + |
| 31 | + //Can be used another constructor for it, without destination. |
| 32 | + new ImageComparison("expected.png", "actual.png"); |
| 33 | + //or |
| 34 | + new ImageComparison(expectedImage, actualImage); |
| 35 | + |
| 36 | + //Also can be configured BEFORE comparing next properties: |
| 37 | + |
| 38 | + //Threshold - it's the max distance between non-equal pixels. By default it's 5. |
| 39 | + imageComparison.setThreshold(10); |
| 40 | + imageComparison.getThreshold(); |
| 41 | + |
| 42 | + //RectangleListWidth - Width of the line that is drawn in the rectangle. By default it's 1. |
| 43 | + imageComparison.setRectangleLineWidth(5); |
| 44 | + imageComparison.getRectangleLineWidth(); |
| 45 | + |
| 46 | + //Destination. Before comparing also can be added destination file for result image. |
| 47 | + imageComparison.setDestination(resultDestination); |
| 48 | + imageComparison.getDestination(); |
| 49 | + |
| 50 | + //MaximalRectangleCount - It means that would get first x biggest rectangles for drawing. |
| 51 | + // by default all the rectangles would be drawn. |
| 52 | + imageComparison.setMaximalRectangleCount(10); |
| 53 | + imageComparison.getMaximalRectangleCount(); |
| 54 | + |
| 55 | + //MinimalRectangleSize - The number of the minimal rectangle size. Count as (width x height). |
| 56 | + // by default it's 1. |
| 57 | + imageComparison.setMinimalRectangleSize(100); |
| 58 | + imageComparison.getMinimalRectangleSize(); |
| 59 | + |
| 60 | + //After configuring the ImageComparison object, can be executed compare() method: |
| 61 | + ComparisonResult comparisonResult = imageComparison.compareImages(); |
| 62 | + |
| 63 | + //Can be found ComparisonState. |
| 64 | + ComparisonState comparisonState = comparisonResult.getComparisonState(); |
| 65 | + |
| 66 | + //And Result Image |
| 67 | + BufferedImage resultImage = comparisonResult.getResult(); |
| 68 | + |
| 69 | + //Image can be saved after comparison, using ImageComparisonUtil. |
| 70 | + ImageComparisonUtil.saveImage(resultDestination, resultImage); |
| 71 | + } |
| 72 | +} |
0 commit comments