Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to compare 2 images using java

Posted on 2009-07-13
Medium Priority
Last Modified: 2013-11-23
How to compare two images are same or not using java.
Question by:Vineel_Davuluri
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Accepted Solution

objects earned 2000 total points
ID: 24838336
compare the two source byte arrays

LVL 92

Assisted Solution

objects earned 2000 total points
ID: 24838350
LVL 20

Expert Comment

ID: 24838458
You need to convert the file into a byte array and then compare those byte arrays.

Expert Comment

ID: 24838815
You'll need to import the following libraries:

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

String file1 = "pic1.png";
String file2 = "pic2.png";
Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
Image image2 = Toolkit.getDefaultToolkit().getImage(file2);
	PixelGrabber grab1 =
	new PixelGrabber(image1, 0, 0, -1, -1, false);
	PixelGrabber grab2 =
	new PixelGrabber(image2, 0, 0, -1, -1, false);
	int[] data1 = null;
	if (grab1.grabPixels()) {
		int width = grab1.getWidth();
		int height = grab1.getHeight();
		data1 = new int[width * height];
		data1 = (int[]) grab1.getPixels();
	int[] data2 = null;
	if (grab2.grabPixels()) {
		int width = grab2.getWidth();
		int height = grab2.getHeight();
		data2 = new int[width * height];
		data2 = (int[]) grab2.getPixels();
	System.out.println("Are they equal?: " + java.util.Arrays.equals(data1, data2));
catch (InterruptedException e1) {

Open in new window

LVL 17

Expert Comment

ID: 24841958
I would use this code
import java.awt.image.*;
import javax.imageio.*;
class Test
	public boolean imagesEqual(File f1,File f2)
		BufferedImage i1,i2;
			i1 =;
			i2 =;
		catch(Exception e){ e.printStackTrace(); return false; }
		if(i1.getWidth() != i2.getWidth() || i1.getHeight() != i2.getHeight())
			return false;
		for(int x = 0; x < i1.getWidth(); x++)
			for(int y = 0; y < i1.getWidth(); y++)
				if(i1.getRGB(x,y) != i2.getRGB(x,y))
					return false;
		return true;

Open in new window


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question