/
ImageDiffView.java
56 lines (46 loc) · 1.59 KB
/
ImageDiffView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package net.avh4.util.imagecomparison;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageDiffView extends JComponent {
private static final long serialVersionUID = 1L;
private final BufferedImage image1;
private final BufferedImage image2;
private boolean showHighlight = true;
public ImageDiffView(final String file1, final String file2)
throws IOException {
image1 = ImageIO.read(new File(file1));
image2 = ImageIO.read(new File(file2));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(image1.getWidth() + image2.getWidth(), Math.max(
image1.getHeight(), image2.getHeight()));
}
@Override
protected void paintComponent(final Graphics g) {
final int w1 = image1.getWidth();
final int w2 = image2.getWidth();
final int h1 = image1.getHeight();
final int h2 = image2.getHeight();
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, w1, 0, null);
if (showHighlight) {
g.setColor(Color.RED);
for (int x = 0; x < Math.min(w1, w2); x++) {
for (int y = 0; y < Math.min(h1, h2); y++) {
if (image1.getRGB(x, y) != image2.getRGB(x, y)) {
g.drawLine(x, y, x, y);
}
}
}
}
}
public void setShowHighlight(final boolean b) {
showHighlight = b;
repaint();
}
}