package com.fingerprint;

import java.io.*; 
import java.util.Base64;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; 
import java.awt.image.*;

public class JavaMatcher
{
	static
    {
		System.loadLibrary("JavaMatcher");
    }
	
	public native int MatchTemplates(byte[] refdata,int refsize, byte[] matdata,int matsize);
    
    
    
    public String readTxtFile(String path) 
    {
        BufferedReader br;
        String read = "";
        String readStr = "";
        try {
            File file = new File(path);
            FileReader fileread = new FileReader(file);
            br = new BufferedReader(fileread);
            while ((read = br.readLine()) != null) {
                readStr = readStr + read;
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        System.out.println("###readStr:" + readStr);
        return readStr;
    } 
    
	 public byte[] readBinFile(String fileName) 
	 {
		 RandomAccessFile randomFile = null;
	     try {
	            randomFile = new RandomAccessFile(fileName, "r");
	            long fileLength = randomFile.length();
	            byte[] bytes = new byte[(int) fileLength];
	            randomFile.read(bytes);
	            return bytes;
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (randomFile != null) {
	                try {
	                    randomFile.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	     return null;
    }
    
    public void TestString()
    {
    	String src=new String();
   	 	String dst=new String();   	 	
   	 	src=readTxtFile("./ref.txt");
   	 	dst=readTxtFile("./mat.txt");
   	 	
   	 	byte[] dat1=Base64.getDecoder().decode(src);
   	 	byte[] dat2=Base64.getDecoder().decode(dst);
   	 			
   	 	if(MatchTemplate(dat1,dat2)>=80)
   	 	{
   	 		System.out.println("Match TRUE");
   	 	}
   	 	else
   	 	{
   	 		System.out.println("Math FALSE");
   	 	}   	 	
    }
	
    void Test(){
		byte[] dat1=readBinFile("./ref.dat");
		byte[] dat2=readBinFile("./mat.dat");
		if(MatchTemplate(dat1,dat2)>=80){
		//if(MatchTemplates(dat1,256,dat2)>=80){
			System.out.println("Match TRUE");
		}else{
			System.out.println("Math FALSE");
		}
	}
	
    static JavaMatcher jc = new JavaMatcher();
    
    public static void main(String[] args)throws   IOException 
    {
    	 System.out.println("Fingerprint SDK Java Demo");
    	 jc.Test();
    	 jc.TestString();
    }
}
