问题描述
//像素过滤接口packagepay365;publicinterfaceFilter{voiddoFilter(int[][]data);}//packagepay365;publicabstractclassAbstractFilterimplementsFilter{publicvoiddoFilter(int[][]data){inth=data.length;if(h<=0)return;intw=data[0].length;if(w<=0)return;for(inti=0;i<h;i++){for(intj=0;j<w;j++){data[i][j]=filter(data[i][j]);}}}protectedabstractintfilter(intp);}//过滤csdn验证码的过滤器packagepay365;publicclassCsdnFilterextendsAbstractFilter{protectedintfilter(intp){returnisNotWhite(p)?1:0;}privatebooleanisNotWhite(intp){booleanb=(p&0×0ff)==255&&(p>>8&0×0ff)==255&&(p>>16&0xff)==255;return!b;}}//过滤前景色为白色的过滤器packagepay365;publicclassWhiteFilterextendsAbstractFilter{protectedintfilter(intp){if(isWhite(p)){return1;}else{return0;}}privatebooleanisWhite(intp){return(p&0x0ff)>240&&(p>>8&0x0ff)>240&&(p>>16&0xff)>240;}}//模板创建类packagepay365;importjava.awt.image.BufferedImage;importjava.io.BufferedReader;importjava.io.File;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.URL;importjava.util.HashSet;importjava.util.Iterator;importjava.util.Set;importjavax.imageio.ImageIO;publicclassTemplateCreator{/***@paramargs*/publicstaticvoidmain(String[]args)throwsException{Setset=newHashSet();Stringurl="http://passport.csdn.net/member/ShowExPwd.aspx";StringfilterClazz="pay365.CsdnFilter";if(args.length>=1){url=args[0];}if(args.length>=2){filterClazz=args[1];}FiltercsdnFilter=(Filter)Class.forName(filterClazz).newInstance();for(inti=1;i<10;i++){URLu=newURL(url);BufferedImagebi=ImageIO.read(u);ImageDataia2=newImageData(bi,csdnFilter);ImageData[]ii=ia2.split();for(intx=0;x<ii.length;x++){ImageDataimageArr=ii[x];set.add(imageArr);}//set.addAll(Arrays.asList(ia2.split()));}System.out.println(set.size());for(Iteratoriter=set.iterator();iter.hasNext();){ImageDataele=(ImageData)iter.next();ele.show();System.out.print("char:");Strings=readLine();if(s.length()==1){ele.code=s.charAt(0);}}PrintWriterpw=newPrintWriter(newFile("template.data"));for(Iteratoriter=set.iterator();iter.hasNext();){ImageDataele=(ImageData)iter.next();pw.println(ele.encode());}pw.flush();pw.close();}privatestaticBufferedReaderreader=newBufferedReader(newInputStreamReader(System.in));privatestaticStringreadLine(){try{returnreader.readLine();}catch(Exceptione){e.printStackTrace();return"";}}}//识别类packagepay365;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Iterator;importjavax.imageio.ImageIO;publicclassRecognize2{staticStringurl="http://passport.csdn.net/member/ShowExPwd.aspx";staticFilterfilter;publicstaticvoidmain(String[]args)throwsException{if(args.length>=1){url=args[0];}StringfilterClazz="pay365.CsdnFilter";if(args.length>=2){filterClazz=args[1];}filter=(Filter)Class.forName(filterClazz).newInstance();inttotal=10;intcount=0;for(inti=0;i<total;i++){booleanb=recognize(i);if(b)count++;}System.out.println("rate:"+(count*1.0/total*100)+"%100");}/***@throwsIOException*/privatestaticbooleanrecognize(intnum)throwsIOException{BufferedImagebi=ImageIO.read(newURL(url));ImageIO.write(bi,"png",newFile(num+".png"));ImageDataia2=newImageData(bi,filter);ImageData[]ii=ia2.split();ArrayListlist=newArrayList();ImageData[]template=ImageData.decodeFromFile("template.data");HashMapmap=newHashMap();for(inti=0;i<template.length;i++){map.put(template[i],newCharacter(template[i].code));}for(intx=0;x<ii.length;x++){ImageDataimageArr=ii[x];if(imageArr.w>15)continue;Characterc=(Character)map.get(imageArr);if(c!=null){list.add(c);}}Strings="";System.out.print(num+":");for(Iteratoriter=list.iterator();iter.hasNext();){Characterc=(Character)iter.next();s+=c;System.out.print(c);}System.out.println();returns.length()!=0;}}