VerifiDecoder.java
上传用户:qingzhou
上传日期:2013-04-21
资源大小:733k
文件大小:9k
- package gxhpjx;
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import magick.ImageInfo;
- import magick.MagickImage;
- import magick.MagickException;
- import magick.QuantizeInfo;
- import magick.ColorspaceType;
- import magick.MagickApiException;
- import magick.PixelPacket;
- import magick.DrawInfo;
- import magick.ResolutionType;
- import magick.ProfileInfo;
- import magick.MontageInfo;
- import magick.Magick;
- import magick.MagickInfo;
- import java.io.File;
- import java.io.IOException;
- import java.io.FileOutputStream;
- import java.net.URI;
- import java.util.Calendar;
- import javax.imageio.*;
- import java.net.URL;
- import java.awt.image.*;
- /**
- * For decoding the verification codes at the website http://www.chsi.com.cn
- * Only for your reference and please do NOT use it for any illegal or
- * commerce activity.
- *
- * @author Dodo zengl@ics.ict.ac.cn Dec 14,2005
- */
- public class VerifiDecoder {
-
- private final int pixel_diff = 50000; //象素差 default to 50000
- private final int class_threshold = 70;//分类阈值 default to 70
- private final int code_length = 4; //验证码长度 default to 4
-
- /*
- * Param1: String templatePath 模板库路径
- * Param2: String inputPath 输入路径
- * Param3: String inputFileName 输入文件名称
- * Param4: int code[] 存储匹配结果
- *
- * return: true if and only if decoding is successful, false otherwise.
- */
- public boolean decode(String templatePath,String inputPath,String inputFileName,int code[]){
- try {
- System.out.println("输入路径为:"+inputPath + inputFileName);
- ImageInfo inputInfo = new ImageInfo(inputPath + inputFileName);
-
- //切分坐标系
- Rectangle rect[] = { new Rectangle(1, 1, 20, 17),new Rectangle(20, 1, 20, 17),
- new Rectangle(38, 1, 15, 17), new Rectangle(54, 1, 15, 17)
- };
- PixelPacket templetPPacket = null; //切分后的模板象素包
- PixelPacket inputPPacket = null; //切分后的待匹配象素包
-
- Calendar startTime = Calendar.getInstance();//计时
- int m = 1;
- for(; m<=code_length; m++){
-
- MagickImage inputImage = new MagickImage(inputInfo);
- inputImage = inputImage.cropImage(rect[m-1]);//切分待匹配矩形
-
- for (int n=0; n<=9; n++){
- String templateFileName = m + "_" + n + ".jpg";//模板图片文件
- ImageInfo templetInfo = new ImageInfo(templatePath + templateFileName);
-
- MagickImage templetImage = new MagickImage(templetInfo);
- templetImage = templetImage.cropImage(rect[m-1]);//切分模板矩形
-
- int i,j=0,k=0;
- for (i=1; i <= 59; i++) {
- for (j=1; j<=14; j++) {//default to i<=59 j<=14
- templetPPacket = templetImage.getOnePixel(i,j);//获取模板x,y象素包
- inputPPacket = inputImage.getOnePixel(i,j);
-
- if (Math.abs(templetPPacket.getRed()-inputPPacket.getRed())>=pixel_diff
- & Math.abs(templetPPacket.getGreen()-inputPPacket.getGreen())>=pixel_diff
- & Math.abs(templetPPacket.getBlue()-inputPPacket.getBlue())>=pixel_diff
- ){
- k++;//统计相异象素包
- }
- }
- }
- if (k < class_threshold) {//匹配成功
- code[m-1] = n;//存储匹配结果
- System.out.println("模板文件:"+templatePath+templateFileName + "n待匹配文件:"+inputPath+inputFileName);
- System.out.println("行:i=" + i + " 列:j=" + j + " 相异点个数:k=" + k);
- System.out.println("模板第" + m + "位: " + n + " 输入:" + n + "n");
- break;//跳出循环,取消该位上的匹配
- }
- }
- }
- Calendar endTime = Calendar.getInstance();//计时
- System.out.print("--------------------------------------n验证码识别结果:");
- if (m>code_length){
- for (int i=0; i<code_length; i++){
- System.out.print(code[i]);
- }
- System.out.print(" [成功] ");
- System.out.println(" 用时:"+ (endTime.getTimeInMillis() - startTime.getTimeInMillis()) + " 毫秒n");
- return true;
- }else {
- System.out.println(" [失败] n");
- }
- }
- catch (MagickApiException ex) {
- System.err.println("MagickException: " + ex + ": " + ex.getReason()
- + ", " + ex.getDescription());
- ex.printStackTrace();
- }
- catch (MagickException ex) {
- System.err.println("MagickException: " + ex);
- ex.printStackTrace();
- }
- return false;
- }
-
- /*
- * 提供Facade方法封装decoding过程
- *
- * */
-
- public String facade(String templatePath, String inputPath, String inputFileName, URL url)throws Exception{
-
- int code[] = {0,0,0,0}; //匹配结果
- BufferedImage image = ImageIO.read(url);
- ImageIO.write(image,"JPEG",new File(inputPath+inputFileName));//从网站上读取输入验证码,保存至本地
-
- VerifiDecoder vd = new VerifiDecoder();
- vd.decode(templatePath,inputPath,inputFileName,code);
-
- String res = "";
- for (int i = 0; i<code.length; i++){
- res = res + code[i];
- }
- return res;
- }
-
- /**
- * 重载facade方法,直接访问本地已获取的验证码图片
- *
- * */
-
- public String facade(String templatePath, String inputPath, String inputFileName)throws Exception{
-
- int code[] = {0,0,0,0}; //匹配结果
-
- VerifiDecoder vd = new VerifiDecoder();
- vd.decode(templatePath,inputPath,inputFileName,code);
-
- String res = "";
- for (int i = 0; i<code.length; i++){
- res = res + code[i];
- }
- return res;
- }
-
- /*
- * 查找Base目录下及其下一级子目录下的特定文件
- *
- * Param1: String baseDir 根目录
- * Param2: String fileInCache 要在缓冲中查找的目标文件名匹配关键字
- * Param3: String newFileName 转移后的目标文件名及路径
- * return: String[] 由于IE缓存目录Contend.IE5目录下的几个目录是随机生成的,所以
- * 需要搜寻验证码所在的某个子目录,并把该子目录及图片名称以字符串数组形式返回
- * */
- public String[] findFile(String baseDir, String fileInCache, String newPathFile){
- String[] res = {"",""};
- try{
- File file = new File(baseDir);
- File inputFile = new File(newPathFile);
- if (file.exists()) {
- int i,k = 1;
- //while (k<=1){//轮循直到要查找的文件在IE缓存中出现
- System.out.print("*");
- for (i = 0; i< file.list().length; i++){
- int j;
- if (file.listFiles()[i].isDirectory()){ //子目录
- //System.out.println("查询目录:"+file.listFiles()[i]);
- for (j = 0; j < file.listFiles()[i].list().length; j++){
- File subfile = file.listFiles()[i].listFiles()[j];//子子目录
- if (subfile.isFile()&& subfile.getName().startsWith(fileInCache)){
- res[0] = subfile.getParent().substring(subfile.getParent().lastIndexOf("\")+1);
- res[1] = subfile.getName();//缓冲中的文件名
- System.out.println("n===== OK! 找到第[" + k + "]张 "+ subfile.getName()+" 位于:"+ res[0] + " ===== ");
- if (inputFile.exists()){
- if (!inputFile.delete())
- System.out.println("目标目录中的旧图片删除失败!");
- }
- if (subfile.renameTo(inputFile))//转移缓存中的所有验证图片
- System.out.println("转移成功!n");
- else System.out.println("转移失败!n");
- k++;
- }
- }
- }
- //}
- }
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return res;
- }
-
- public static void main(String[] args) throws Exception{
- String templatePath = "D:\trash\template\";
- String inputPath = templatePath;
- String inputFileName = "ValidatorIMG.jpg";//待匹配图片文件:ValidatorIMG
- int code[] = {0,0,0,0}; //匹配结果
- URL url = new URL("http://www.chsi.com.cn/ValidatorIMG.JPG?ID=" + Math.random()*10000);
- BufferedImage image = ImageIO.read(url);
- ImageIO.write(image,"JPEG",new File(templatePath+inputFileName));//从网站上读取输入验证码,保存至本地
-
- VerifiDecoder vd = new VerifiDecoder();
- vd.decode(templatePath,inputPath,inputFileName,code);
- }
- }