/*
 * CodeHacker.java
 *
 * Created on 2007年6月24日, 下午7:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author vlinux
 */
public class CodeHacker {
    
    private BufferedImage codeImage;
    private int[][] imageDatas;
    private int[] a,b,c,d;
    private String code;
    
    public CodeHacker( String imageFilename ) throws IOException {
        this( ImageIO.read(new File(imageFilename)) );
    }
    
    public CodeHacker( BufferedImage codeImage ) {
        this.codeImage = codeImage;
        readDatas();
        code = ""+getBestCode( getSimple(3,5) )+
                getBestCode( getSimple(3,13) )+
                getBestCode( getSimple(3,21) )+
                getBestCode( getSimple(3,29) );
    }
    
    
    /**
     * 获取解析出的验证码
     */
    public String getCode() {
        return code;
    }
    
    // 从图像中读取数据
    private void readDatas() {
        java.awt.image.WritableRaster wr = codeImage.getRaster();
        imageDatas = new int[codeImage.getHeight()][codeImage.getWidth()];

        int[] sample = new int[4];
        for( int h=0; h<codeImage.getHeight(); h++ ) {
            for( int w=0; w<codeImage.getWidth(); w++ ) {
                wr.getPixel( w, h, sample );
                int sum = 0;
                for( int s: sample ) sum += s;
                imageDatas[h][w] = sum>0?1:0;
            }//for
        }//for
    }
    
    //获取单个数字的图像数组
    private int[] getSimple( int x, int y ) {
        int[] simple = new int[9*6];
        int count = 0;
        for( int h=x; h<x+9; h++ ) {
            for( int w=y; w<y+6; w++ ) {
                simple[count++] = imageDatas[h][w];
            }
        }
        return simple;
    }
    
    //获取最和谐的数字
    private int getBestCode( int[] x ) {
        int bestNumber = -1;
        double bestVal = 0.0;
        for( int index=0; index<10; index++ ) {
            double val = compareSimple( CodeData.getNumber(index),x );
            if( bestVal <= val ) {
                bestVal = val;
                bestNumber = index;
            }
        }
        return bestNumber;
    }
    
    
    //获取两个数字图像数组的和谐百分比
    private double compareSimple( int[] t, int[] x ) {
        int count = 0;
        int val = 0;
        for( int i=0; i<t.length; i++ ) {
            if( 1 == t[i] ) {
                count++;
                if( 1 == x[i] ) {
                    val++;
                }
            }
        }
        return (val+0.0)/count;
    }
    
    
    
    //显示出获取的图像
    /*
    private void showDatas() {
        for( int h=0; h<codeImage.getHeight(); h++ ) {
            for( int w=0; w<codeImage.getWidth(); w++ ) {
                System.out.print( imageDatas[h][w] );
            }
            System.out.println();
        }
    }*/
    
    //显示出获取的干戈数字的图像数组
    /*
    private void showSimple( int[] a ) {
        for( int h=0; h<9; h++ ) {
            for( int w=0; w<6; w++ ) {
                System.out.print( a[h*6+w] );
            }
            System.out.println();
        }
    }*/

    /*public static void main( String... args ) throws IOException {
        CodeHacker codeHacker = new CodeHacker("images/code3.png");
        System.out.println( codeHacker.getCode() );
    }
    */
    
}

