Java Utililty Methods BufferedImage Compare

List of utility methods to do BufferedImage Compare

Description

The list of methods to do BufferedImage Compare are organized into topic(s).

Method

booleancompare(BufferedImage a, BufferedImage b)
Method used to compare two given images
int aData = 0, bData = 0;
for (int xPos = 0; xPos < a.getWidth(); xPos++) {
    for (int yPos = 0; yPos < a.getHeight(); yPos++) {
        aData = a.getRGB(xPos, yPos);
for (int xPos = 0; xPos < b.getWidth(); xPos++) {
    for (int yPos = 0; yPos < b.getHeight(); yPos++) {
...
booleancompare(BufferedImage p1, BufferedImage p2)
compare
if (p1 == null) {
    return p2 == null;
} else if (p2 == null) {
    return false;
Raster r1 = p1.getRaster();
Raster r2 = p2.getRaster();
return compare(r1, r2);
...
booleancompareBufferedImages(BufferedImage bufferedImage1, BufferedImage bufferedImage2)
compare Buffered Images
if (bufferedImage1.getWidth() == bufferedImage2.getWidth()
        && bufferedImage1.getHeight() == bufferedImage2.getHeight()) {
    for (int x = 0; x < bufferedImage1.getWidth(); x++) {
        for (int y = 0; y < bufferedImage1.getHeight(); y++) {
            if (bufferedImage1.getRGB(x, y) != bufferedImage2.getRGB(x, y))
                return false;
} else {
    return false;
return true;
booleancompareImage(BufferedImage biA, BufferedImage biB)
compare Image
DataBuffer dbA = biA.getData().getDataBuffer();
int sizeA = dbA.getSize();
DataBuffer dbB = biB.getData().getDataBuffer();
int sizeB = dbB.getSize();
if (sizeA == sizeB) {
    for (int i = 0; i < sizeA; i++) {
        if (dbA.getElem(i) != dbB.getElem(i)) {
            return false;
...
doublecompareImage(BufferedImage img1, BufferedImage img2)
Assume that img1, img2 sizes are equals
Raster data1 = img1.getRaster();
Raster data2 = img2.getRaster();
int width = data1.getWidth();
int height = data2.getHeight();
double[] p1 = new double[3];
double[] p2 = new double[3];
double result = 0;
for (int x = 0; x < width; x++) {
...
doublecompareImage(BufferedImage img1, BufferedImage img2)
compare Image
int height = Math.max(img1.getHeight(), img2.getHeight());
int width = Math.max(img1.getWidth(), img2.getWidth());
long matching = 0;
long pixels = 0;
for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
        int col1 = -1;
        if (x < img1.getWidth() && y < img1.getHeight()) {
...
doublecompareImages(BufferedImage imageOne, BufferedImage imageTwo)
compare Images
int useWidth = imageOne.getWidth() > imageTwo.getWidth() ? imageTwo.getWidth() : imageOne.getWidth();
int useHeight = imageOne.getHeight() > imageTwo.getHeight() ? imageTwo.getHeight() : imageOne.getHeight();
int maxWidth = imageOne.getWidth() < imageTwo.getWidth() ? imageTwo.getWidth() : imageOne.getWidth();
int maxHeight = imageOne.getHeight() < imageTwo.getHeight() ? imageTwo.getHeight() : imageOne.getHeight();
int differenceCount = 0;
for (int x = 0; x < useWidth; x++) {
    for (int y = 0; y < useHeight; y++) {
        if (imageOne.getRGB(x, y) != imageTwo.getRGB(x, y))
...
booleancompareImages(BufferedImage imgA, BufferedImage imgB)
compare Images
if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) {
    int width = imgA.getWidth();
    int height = imgA.getHeight();
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
                return false;
} else {
    return false;
return true;
booleancompareImages(final BufferedImage firstImage, final BufferedImage secondImage)
compare Images
if (firstImage.getHeight() != secondImage.getHeight() || firstImage.getWidth() != secondImage.getWidth()) {
    return false;
for (int x = 0; x < firstImage.getWidth(); ++x) {
    for (int y = 0; y < firstImage.getHeight(); ++y) {
        if (firstImage.getRGB(x, y) != secondImage.getRGB(x, y)) {
            return false;
return true;
Point[]compareImages(final BufferedImage img1, final BufferedImage img2)
Exact pixel by pixel compare.
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 (img1.getRGB(x, y) != img2.getRGB(x, y)) {
            pixels.add(new Point(x, y));
...