Java BufferedImage Equal equals(BufferedImage img1, BufferedImage img2)

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

Description

Returns true if the 2 images are equal, false otherwise

License

Open Source License

Declaration

public static boolean equals(BufferedImage img1, BufferedImage img2) 

Method Source Code

//package com.java2s;
/*//from  w ww  .j  a v  a2  s  . c om
  Copyright (c) 2006 Harri Kaimio
      
  This file is part of Photovault.
    
  Photovault is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  Photovault is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with Photovault; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

import java.awt.image.*;

public class Main {
    /**
       Returns true if the 2 images are equal, false otherwise
    */
    public static boolean equals(BufferedImage img1, BufferedImage img2) {
        if (img1.getWidth() != img2.getWidth()) {
            return false;
        }
        if (img1.getHeight() != img2.getHeight()) {
            return false;
        }
        System.err.println("Equal size");

        int[] data1 = img1.getRGB(0, 0, img1.getWidth(), img1.getHeight(), null, 0, img2.getWidth());
        int[] data2 = img2.getRGB(0, 0, img2.getWidth(), img2.getHeight(), null, 0, img2.getWidth());

        // Compare the images pixel by pixel
        int diffCount = 0;
        for (int n = 0; n < data1.length; n++) {
            if (data1[n] != data2[n]) {
                diffCount++;
            }
        }
        return (diffCount < 10) ? true : false;
    }
}

Related

  1. bufferedImagesEqual(final BufferedImage img1, final BufferedImage img2)
  2. equals(BufferedImage a, BufferedImage b)
  3. equals(final BufferedImage image1, final BufferedImage image2)
  4. imagesAreEqual(BufferedImage image1, BufferedImage image2)
  5. imagesAreEqual(String referenceImagePath, String capturedImagePath, int x, int y, int w, int h)
  6. imagesEqual(File file1, File file2)