Java BufferedImage Compare compareImages(final BufferedImage img1, final BufferedImage img2)

Here you can find the source of compareImages(final BufferedImage img1, final BufferedImage img2)

Description

Exact pixel by pixel compare.

License

Open Source License

Parameter

Parameter Description
img1 First image for the comparison
img2 Second image for the comparison

Return

Point[] array that contains the coordinates of pixels that are different

Declaration

protected static Point[] compareImages(final BufferedImage img1, final BufferedImage img2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Main {
    /**//from   ww w  . j  a va 2 s . c o m
     * Exact pixel by pixel compare. Images must have the same size
     * 
     * @param img1 First image for the comparison
     * @param img2 Second image for the comparison
     * @return Point[] array that contains the coordinates of pixels that are different
     */
    protected static Point[] compareImages(final BufferedImage img1, final BufferedImage img2) {
        if (img1.getWidth() != img2.getWidth() || img1.getHeight() != img2.getHeight()) {
            return null;
        }

        final ArrayList<Point> pixels = new ArrayList<>();

        for (int x = 0; x < img1.getWidth(); x++) {
            for (int y = 0; y < img1.getHeight(); y++) {
                // if the RGB values of 2 pixels differ
                if (img1.getRGB(x, y) != img2.getRGB(x, y)) {
                    pixels.add(new Point(x, y));
                }
            }
        }

        return pixels.toArray(new Point[pixels.size()]);
    }
}

Related

  1. compareImage(BufferedImage img1, BufferedImage img2)
  2. compareImage(BufferedImage img1, BufferedImage img2)
  3. compareImages(BufferedImage imageOne, BufferedImage imageTwo)
  4. compareImages(BufferedImage imgA, BufferedImage imgB)
  5. compareImages(final BufferedImage firstImage, final BufferedImage secondImage)
  6. compareImgToFile(BufferedImage img, File file)
  7. imageDifference(BufferedImage imgA, BufferedImage imgB)