Thursday, February 3, 2011

Java code: How to crop images and compare them

Java code: How to crop images and compare them

Dear Reader,
I have written a complete code to crop images and compare them for identity. Basically such type of application is
used in Security scenarios where Image verification/matching is involved. Ex: Finger Print matching, Palm reader matching in 
companies where we have security system installed.

These APIs are fully functional and I have tested in my system.
There are two Java classes:

1) CropImageFile.java  (For Cropping the Source Image and generating Duplicate image).
2) CompareImageFiles.java (For Verifying the Source Image and Duplicate image).

Please go through the codes carefully, I have clearly written the File Paths, where it is getting stored and 
manually moved in other directory for matching purpose. You can tweak the code to suit your purpose.

Steps to work in Security System:
1) First Save Finger Print or Palm images of Authentic employees in a Authentic directory/Folder.

2) Crop them using our tool, as We need a portion of the images only. Full image is not necessary. Assume
a size of 400*200 dimension.

3) Verification step: Let a Stranger put his Finger/Palm at device and crop the recorded image. 
Use our tool to verify the image from files that are stored in the Authentic Directory (where we have stored 
authentic images). You can use a loop to compare with all the files. I have given code for only one file match.

Codes:
//CropImageFile.java
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
public class CropImageFile {
public static void main(String[] args) {
cropImage();
}
public static void cropImage() {
byte[] bytesOut = null;
try {
int x1 = 0, y1 = 0, cw = 0, ch = 0;
String str[] = new String[]{"10","10","400","200"};

x1 = str[0].equals("") ? 50 : Integer.parseInt(str[0]);
y1 = str[1].equals("") ? 50 : Integer.parseInt(str[1]);
cw = str[2].equals("") ? 50 : Integer.parseInt(str[2]);
ch = str[3].equals("") ? 50 : Integer.parseInt(str[3]);

String oldFileName="D:\\Ears\\Old_Another_File.JPG";
FileInputStream oldFile = new FileInputStream(oldFileName);

File newFile = new File("D:\\Ears\\New_Another_File.JPG");
FileOutputStream fout = new FileOutputStream(newFile);

BufferedImage bimage = ImageIO.read(oldFile);
BufferedImage bi = null;
bi = bimage.getSubimage(x1, y1, cw, ch);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String format = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
if (bi != null) {
ImageIO.write(bi, format, baos);
}
bytesOut = baos.toByteArray();
fout.write(bytesOut);
System.out.println("New file with cropped dimension is created.");
fout.close();
}
catch(Exception exp){
exp.printStackTrace();
}
}
}


//CompareImageFiles.java
import java.io.*;
import java.util.Arrays;

public class CompareImageFiles {
private static int BUFFSIZE=2000;
private static byte buff1[]=new byte[2000];
private static byte buff2[]=new byte[2000];

public static void main(String[] args) throws Exception {
String fileName1="D:\\Ears\\To_Compare\\New1.png";
String fileName2 = "D:\\Ears\\To_Compare\\New2.png";
String fileName3 = "D:\\Ears\\To_Compare\\New3.png";

System.out.println("Comparison result of both files : "+fileContentsEquals(fileName1,fileName2));
System.out.println("Comparison result of both files : "+fileContentsEquals(fileName1,fileName3));

fileName1="D:\\Ears\\To_Compare\\New_Another_File1.JPG";
fileName2 = "D:\\Ears\\To_Compare\\New_Another_File2.JPG";
fileName3 = "D:\\Ears\\To_Compare\\New_Another_File3.JPG";
System.out.println("Comparison result of both files : "+fileContentsEquals(fileName1,fileName2));
System.out.println("Comparison result of both files : "+fileContentsEquals(fileName2,fileName3));

}

public static boolean inputStreamEquals(InputStream is1, InputStream is2) {
if(is1 == is2) return true;
if(is1 == null && is2 == null) {
return true;
}
if(is1 == null || is2 == null) {
return false;
}
try {
int read1 = -1;
int read2 = -1;
do {
int offset1 = 0;
while (offset1 < BUFFSIZE && (read1 = is1.read(buff1, offset1, BUFFSIZE-offset1)) >= 0) {
offset1 += read1;
}

int offset2 = 0;
while (offset2 < BUFFSIZE && (read2 = is2.read(buff2, offset2, BUFFSIZE-offset2)) >= 0) {
offset2 += read2;
}
if(offset1 != offset2) {
return false;
}
if(offset1 != BUFFSIZE) {
Arrays.fill(buff1, offset1, BUFFSIZE, (byte)0);
Arrays.fill(buff2, offset2, BUFFSIZE, (byte)0);
}
if(!Arrays.equals(buff1, buff2)){
return false;
}
} while(read1 >= 0 && read2 >= 0);
if(read1 < 0 && read2 < 0) return true;    // both at EOF
return false;

} catch (Exception ei) {
return false;
}
}

public static boolean fileContentsEquals(File file1, File file2) {
InputStream is1 = null;
InputStream is2 = null;
if(file1.length() != file2.length())  {
System.out.println("Either of the file is not available or size differs");
return false;
}
try {
is1 = new FileInputStream(file1);
is2 = new FileInputStream(file2);
return inputStreamEquals(is1, is2);

} catch (Exception ei) {
return false;
} finally {
try {
if(is1 != null) is1.close();
if(is2 != null) is2.close();
} catch (Exception ei2) {}
}
}

public static boolean fileContentsEquals(String fn1, String fn2) {
return fileContentsEquals(new File(fn1), new File(fn2));
}
}

//Both classes have separate main methods, can be used individually also.

1 comment:

  1. Sir i want that web cam take image and check it with image saved in database
    please send me coding of program
    where we match two image either image is in dull condition or rotating or in other position but program check image by his face?
    deepakjindaldj2007@gmail.com

    ReplyDelete