This solution helped me. It highlights the differences and has the best performance from the methods I tried. (Assumptions: Images are the same size. This method has not been tested on transparencies.)
Average time to compare PNG images 1600x860 50 times (on one computer):
- JDK7 ~ 178 milliseconds
- JDK8 ~ 139 milliseconds
Does anyone have a better / quick solution?
public static BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) { // convert images to pixel arrays... final int w = img1.getWidth(), h = img1.getHeight(), highlight = Color.MAGENTA.getRGB(); final int[] p1 = img1.getRGB(0, 0, w, h, null, 0, w); final int[] p2 = img2.getRGB(0, 0, w, h, null, 0, w); // compare img1 to img2, pixel by pixel. If different, highlight img1 pixel... for (int i = 0; i < p1.length; i++) { if (p1[i] != p2[i]) { p1[i] = highlight; } } // save img1 pixels to a new BufferedImage, and return it... // (May require TYPE_INT_ARGB) final BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); out.setRGB(0, 0, w, h, p1, 0, w); return out; }
Application:
import javax.imageio.ImageIO; import java.io.File; ImageIO.write( getDifferenceImage( ImageIO.read(new File("a.png")), ImageIO.read(new File("b.png"))), "png", new File("output.png"));
Some inspiration ...
source share