Java BufferedImage Equal imgEquals(File imgRef, File img)

Here you can find the source of imgEquals(File imgRef, File img)

Description

img Equals

License

Open Source License

Declaration

public static boolean imgEquals(File imgRef, File img) throws IOException 

Method Source Code


//package com.java2s;
/*//w w  w  . j ava 2s .c o m
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Revolance-UI-Model
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Copyright (C) 2012 - 2013 RevoLance
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * This program 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 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program 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 this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<File, Map<File, Boolean>> cache = new HashMap<File, Map<File, Boolean>>();

    public static boolean imgEquals(String imgRef, String img) throws IOException {
        File imgRefFile = new File(imgRef);
        File imgFile = new File(img);
        if (imgRefFile.exists() && imgFile.exists()) {
            return imgEquals(imgRefFile, imgFile);
        } else {
            return true;
        }
    }

    public static boolean imgEquals(File imgRef, File img) throws IOException {
        if (isCached(imgRef, img)) {
            return getComparisons(imgRef).get(img).booleanValue();
        } else if (isCached(img, imgRef)) {
            return getComparisons(img).get(imgRef).booleanValue();
        } else {
            BufferedImage refBI = ImageIO.read(imgRef);
            BufferedImage imgBi = ImageIO.read(img);

            int width = imgBi.getWidth();
            int height = imgBi.getHeight();

            if (refBI.getWidth() == width && refBI.getHeight() == height) {
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        int refIntensity = refBI.getRGB(x, y);
                        int imgIntensity = imgBi.getRGB(x, y);
                        if (refIntensity != imgIntensity) {
                            getComparisons(imgRef).put(img, false);
                            return false;
                        }
                    }
                }
            }

            getComparisons(imgRef).put(img, true);
            return true;
        }
    }

    private static boolean isCached(File imgRef, File img) {
        if (cache.containsKey(imgRef)) {
            Map<File, Boolean> comparisons = cache.get(imgRef);
            if (comparisons.containsKey(img)) {
                return true;
            }
        }
        return false;
    }

    private static Map<File, Boolean> getComparisons(File imgRef) {
        Map<File, Boolean> comparisons;
        if (cache.containsKey(imgRef)) {
            comparisons = cache.get(imgRef);
        } else {
            comparisons = new HashMap<File, Boolean>();
            cache.put(imgRef, comparisons);
        }
        return comparisons;
    }
}

Related

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