Skip to content

Commit 55a7a08

Browse files
committed
ImageCompare utiliti is commited so user can compare jpg or png image and get result image as an output, or use can query match percentage.
1 parent fc55f14 commit 55a7a08

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<modelVersion>4.0.0</modelVersion>
1212
<groupId>com.theartos</groupId>
1313
<artifactId>artos</artifactId>
14-
<version>0.0.13-beta-1</version>
14+
<version>0.0.13-beta-2</version>
1515

1616
<!-- Organisation Info -->
1717
<organization>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*******************************************************************************
2+
* Copyright (C) 2018-2019 Arpit Shah and Artos Contributors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
******************************************************************************/
22+
package com.artos.utils;
23+
24+
import java.awt.image.BufferedImage;
25+
import java.io.File;
26+
import java.io.FileNotFoundException;
27+
import java.io.IOException;
28+
29+
import javax.imageio.ImageIO;
30+
31+
import com.google.common.io.Files;
32+
33+
/**
34+
* This code will compare pixel from base image with other image.
35+
*
36+
* <PRE>
37+
* 1) If both pixel at location (x,y) are same then add same pixel in result image without change. Otherwise it modifies that pixel and add to our result
38+
* image.
39+
* 2) In case of baseline image height or width is larger than other image then it will add red colour for extra pixel which is not available in
40+
* other images.
41+
* 3) Both image file format should be same for comparison. Code will use base image file format to create resultant image file.
42+
* </PRE>
43+
*
44+
*/
45+
public class ImageCompare {
46+
47+
private int percentageMatch = 0;
48+
private File resultImage = null;
49+
50+
/**
51+
* compare image with reference image and return percentage of match.
52+
*
53+
* @param referenceImage = golden sample, reference image
54+
* @param targetImage = image which is required to be tested
55+
* @throws Exception Exception in case image does not exist or extension does not match
56+
*/
57+
public void compare(File referenceImage, File targetImage) throws Exception {
58+
compare(referenceImage, targetImage, null, null);
59+
}
60+
61+
/**
62+
* compare image with reference image and return percentage of match. Image must be of same type. Any mismatch of pixels will be highlighted in
63+
* red colour and result image will be stored at desired location.
64+
*
65+
* @param referenceImage = golden sample, reference image
66+
* @param targetImage = image which is required to be tested
67+
* @param resultDir = result destination directory name
68+
* @param resultImageName = result image name without an extension
69+
* @throws Exception in case image does not exist or extension does not match
70+
*/
71+
public void compare(File referenceImage, File targetImage, File resultDir, String resultImageName) throws Exception {
72+
73+
String fileExtenstion = Files.getFileExtension(referenceImage.getName());
74+
75+
if (!(referenceImage.exists() && targetImage.exists() && referenceImage.isFile() && targetImage.isFile())) {
76+
throw new FileNotFoundException();
77+
}
78+
79+
if (!(Files.getFileExtension(referenceImage.getName()).equalsIgnoreCase(Files.getFileExtension(targetImage.getName())))) {
80+
throw new Exception("File extensions are not the same");
81+
}
82+
83+
if (null != resultDir) {
84+
if (!resultDir.exists() || !resultDir.isDirectory()) {
85+
resultDir.mkdirs();
86+
}
87+
}
88+
89+
long matchCount = 0;
90+
BufferedImage refImage = ImageIO.read(referenceImage);
91+
BufferedImage testImage = ImageIO.read(targetImage);
92+
int height = refImage.getHeight();
93+
int width = refImage.getWidth();
94+
95+
BufferedImage rImage;
96+
if (fileExtenstion.toUpperCase().contains("PNG")) {
97+
rImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
98+
} else {
99+
// Assume jpg, it does not have alpha
100+
rImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
101+
}
102+
for (int y = 0; y < height; y++) {
103+
for (int x = 0; x < width; x++) {
104+
try {
105+
int pixelC = testImage.getRGB(x, y);
106+
int pixelB = refImage.getRGB(x, y);
107+
if (pixelB == pixelC) {
108+
rImage.setRGB(x, y, refImage.getRGB(x, y));
109+
// Add one to increase percentage of match
110+
matchCount++;
111+
} else {
112+
int a = 0xff | refImage.getRGB(x, y) >> 24, r = 0xff & refImage.getRGB(x, y) >> 16, g = 0x00 & refImage.getRGB(x, y) >> 8,
113+
b = 0x00 & refImage.getRGB(x, y);
114+
115+
int modifiedRGB = a << 24 | r << 16 | g << 8 | b;
116+
rImage.setRGB(x, y, modifiedRGB);
117+
}
118+
} catch (Exception e) {
119+
// handled height or width mismatch
120+
rImage.setRGB(x, y, 0x80ff0000);
121+
}
122+
}
123+
}
124+
125+
if (null != resultDir && null != resultImageName) {
126+
setResultImage(new File(resultDir, resultImageName + "." + fileExtenstion));
127+
if (fileExtenstion.toUpperCase().contains("PNG")) {
128+
createPngImage(rImage);
129+
} else {
130+
createJpgImage(rImage);
131+
}
132+
}
133+
134+
long totalPixel = height * width;
135+
setPercentageMatch((int) (matchCount * 100 / totalPixel));
136+
}
137+
138+
private void createPngImage(BufferedImage image) throws IOException {
139+
ImageIO.write(image, "png", getResultImage());
140+
}
141+
142+
private void createJpgImage(BufferedImage image) throws IOException {
143+
ImageIO.write(image, "jpg", getResultImage());
144+
}
145+
146+
/**
147+
* Returns image match percentage. 100 if image matches completely.
148+
*
149+
* @return image match percentage
150+
*/
151+
public int getPercentageMatch() {
152+
return percentageMatch;
153+
}
154+
155+
private void setPercentageMatch(int percentageMatch) {
156+
this.percentageMatch = percentageMatch;
157+
}
158+
159+
/**
160+
* Returns result image file, null if result image is not specified.
161+
*
162+
* @return result image file or null if result image is not specified.
163+
*/
164+
public File getResultImage() {
165+
return resultImage;
166+
}
167+
168+
private void setResultImage(File resultImage) {
169+
this.resultImage = resultImage;
170+
}
171+
}

0 commit comments

Comments
 (0)