Skip to content

Commit

Permalink
refactored compareImages() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed May 17, 2019
1 parent 578bd62 commit 26912bf
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
*/
public class ImageComparison {

/**
* Prefix of the name of the result image.
*/
private static final String NAME_PREFIX = "image-comparison";

/**
* Suffix of the name of of the result image.
*/
private static final String NAME_SUFFIX = ".png";

/**
* 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 @@ -108,8 +98,6 @@ public ComparisonResult compareImages() throws IOException {

matrix = populateTheMatrixOfTheDifferences();

groupRegions();

List<Rectangle> rectangles = populateRectangles();

ComparisonResult comparisonResult = new ComparisonResult();
Expand All @@ -127,13 +115,7 @@ public ComparisonResult compareImages() throws IOException {
BufferedImage resultImage = ImageComparisonUtil.deepCopy(image2);
comparisonResult.setResult(resultImage);

Graphics2D graphics = resultImage.createGraphics();
graphics.setColor(RED);

BasicStroke stroke = new BasicStroke(rectangleLineWidth);
graphics.setStroke(stroke);

drawRectangles(rectangles, graphics);
drawRectangles(rectangles, resultImage);

return comparisonResult;
}
Expand Down Expand Up @@ -187,6 +169,7 @@ private boolean isDifferentPixels(int rgb1, int rgb2) {
}

private List<Rectangle> populateRectangles() {
groupRegions();
List<Rectangle> rectangles = new ArrayList<>();
while (counter <= regionCount) {
Rectangle rectangle = createRectangle();
Expand Down Expand Up @@ -262,9 +245,18 @@ private List<Rectangle> mergeRectangles(List<Rectangle> rectangles) {
}

/**
* Draw rectangles on the result image.
* Draw the rectangles based on collection of the rectangles and result image.
*
* @param rectangles the collection of the {@link Rectangle} objects.
* @param resultImage result image, which will being drawn.
*/
private void drawRectangles(List<Rectangle> rectangles, Graphics2D graphics) {
private void drawRectangles(List<Rectangle> rectangles, BufferedImage resultImage) {
Graphics2D graphics = resultImage.createGraphics();
graphics.setColor(RED);

BasicStroke stroke = new BasicStroke(rectangleLineWidth);
graphics.setStroke(stroke);

rectangles.forEach(rectangle -> graphics.drawRect(rectangle.getMinPoint().getY(),
rectangle.getMinPoint().getX(),
rectangle.getWidth(),
Expand Down

0 comments on commit 26912bf

Please sign in to comment.