《2022年2022年简单图形验证码的识 .pdf》由会员分享,可在线阅读,更多相关《2022年2022年简单图形验证码的识 .pdf(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、简单图形验证码的识别作者:未知文章来源: 转载点击数:161 更新时间: 2007-3-3 对 于 简单 的 图形 验证 码 (字 体规 则 ,没 有杂 点 或杂 点 容易 过滤 掉), 用模 板 匹配 的方 式 可以 比 较容 易地 识 别出 来 . 0. 图片 黑 白化 ,用 1 表示 有字 的 像素 ,0 表 示无字 的 像素1. 字块 分隔 ,将图 片 分隔 成只 包 含成 单字 的 最小 块2. 生 成模 板 ,将字 块 与字 符 关联3. 识 别 ,将 新 图片 分块 并 与模 板匹 配 java(jdk1.5)实现 :/ 先运行 TemplateCreator创 建模 板 ,再
2、运行Recognize2识别/ 图 片数 据表 示 ,也 用 来表 示字 块package pay365; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; public class ImageData public int data; public
3、int w; public int h; public char code; public ImageData() public ImageData(BufferedImage bi) this(bi,new WhiteFilter(); public ImageData(BufferedImage bi,Filter filter) h = bi.getHeight(); w = bi.getWidth(); data = new inthw; for (int i = 0; i h; i+) for (int j = 0; j w; j+) int p = bi.getRGB(j, i);
4、 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 11 页 - - - - - - - - - dataij = p; filter.doFilter(data); public ImageData split() ArrayList list = new ArrayList(); ImageIterator ite = new ImageIterator(this); while (ite.hasNext() list.add(ite.next(); return (I
5、mageData) list.toArray(new ImageData0); int skipEmpty(int begin, boolean isX, int value) if (isX) for (int i = begin; i w; i+) for (int j = 0; j h; j+) if (dataji != value) return i; return -1; else for (int i = begin; i h; i+) for (int j = 0; j w; j+) if (dataij != value) return i; return -1; int s
6、kipEntity(int begin, boolean isX, int value) if (isX) for (int i = begin; i w; i+) for (int j = 0; j h; j+) if (dataji = value) break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 11 页 - - - - - - - - - if (j = h - 1) return i; return -1; else for (int i = be
7、gin; i h; i+) for (int j = 0; j w; j+) if (dataij = value) break; if (j = w - 1) return i; return -1; class ImageIterator implements Iterator int x; ImageData ia; ImageData next; public ImageIterator(ImageData ia) this.ia = ia; public boolean hasNext() if (next != null) return true; next = getNext()
8、; return next != null; ImageData getNext() int x1 = skipEmpty(x, true, 0); if (x1 = -1) return null; int x2 = skipEntity(x1, true, 1); if (x2 = -1) x2 = w; x = x2; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 11 页 - - - - - - - - - int y1 = skipEmpty(0, false
9、, 0); if (y1 = -1) return null; int y2 = skipEntity(y1, false, 1); if (y2 = -1) y2 = h; return ia.clone(x1, y1, x2 - x1, y2 - y1); public Object next() ImageData temp = next; next = null; return temp; public void remove() ImageData clone(int x, int y, int w0, int h0) ImageData ia = new ImageData();
10、ia.w = w0; ia.h = h0; ia.data = new intia.hia.w; for (int i = 0; i h0; i+) for (int j = 0; j w0; j+) ia.dataij = datai + yj + x; return ia; public void show() System.out.println(); for (int i = 0; i h; i+) for (int j = 0; j w; j+) System.out.print(dataij = 1 ? 1 : ) + ); System.out.println(); System
11、.out.println(); public int hashCode() int code = w h; int count = 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 11 页 - - - - - - - - - for (int i = 0; i h; i+) for (int j = 0; j w; j+) if (dataij = 1) count+; code = count; return code; public boolean equals
12、(Object obj) if (this = obj) return true; if (obj instanceof ImageData) ImageData o = (ImageData) obj; if (o.h != h) return false; if (o.w != w) return false; for (int i = 0; i h; i+) for (int j = 0; j w; j+) if (o.dataij != dataij) return false; return true; else return false; public static ImageDa
13、ta decodeFromFile(String path) throws IOException BufferedReader reader = new BufferedReader(new FileReader( new File(path); String line; ArrayList list = new ArrayList(); while (line = reader.readLine() != null) ImageData ia = decode(line); if (ia != null) list.add(ia); 名师资料总结 - - -精品资料欢迎下载 - - - -
14、 - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 11 页 - - - - - - - - - return (ImageData) list.toArray(new ImageData0); public static ImageData decode(String s) String ss = s.split(, 4); if (ss.length != 4) return null; if (ss0.length() != 1) return null; ImageData ia = new ImageData(); ia
15、.code = ss0.charAt(0); ia.w = Integer.parseInt(ss1); ia.h = Integer.parseInt(ss2); if (ss3.length() != ia.w * ia.h) return null; ia.data = new intia.hia.w; for (int i = 0; i ia.h; i+) for (int j = 0; j ia.w; j+) if (ss3.charAt(i * ia.w + j) =1) ia.dataij = 1; else ia.dataij = 0; return ia; public St
16、ring encode() StringBuffer sb = new StringBuffer(); sb.append(code).append(,); sb.append(w).append(,); sb.append(h).append(,); for (int i = 0; i h; i+) for (int j = 0; j w; j+) if (dataij = 1) sb.append(1); else sb.append(0); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - -
17、- - 第 6 页,共 11 页 - - - - - - - - - return sb.toString(); / 像 素过 滤接 口package pay365; public interface Filter void doFilter(int data); / package pay365; public abstract class AbstractFilterimplements Filter public void doFilter(int data) int h = data.length; if (h=0) return; int w = data0.length; if (
18、w=0) return ; for(int i=0;i h;i+) for(int j=0;j 8 & 00ff) = 2 55 & (p 16 & 0 xff) = 255; return !b; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 11 页 - - - - - - - - - / 过 滤前 景色 为 白色 的过 滤 器package pay365; public class WhiteFilter extends AbstractFilter protec
19、ted int filter(int p) if (isWhite(p) return 1; else return 0; private boolean isWhite(int p) return (p & 0 x0ff) 240 & (p 8 & 0 x0ff) 240 & (p 16 & 0 xff) 240; / 模 板创 建类package pay365; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.InputStream
20、Reader; import java.io.PrintWriter; import .URL; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.imageio.ImageIO; public class TemplateCreator /* * param args */ public static void main(String args) throws Exception Set set = new HashSet(); String url = http:/
21、 filterClazz = pay365.CsdnFilter; if (args.length=1) url = args0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 11 页 - - - - - - - - - if (args.length=2) filterClazz = args1; Filter csdnFilter = (Filter) Class.forName(filterClazz).newInstance(); for (int i = 1
22、; i 10; i+) URL u = new URL(url); BufferedImage bi = ImageIO.read(u); ImageData ia2 = new ImageData(bi,csdnFilter); ImageData ii = ia2.split(); for (int x = 0; x = 1) url = args0; String filterClazz = pay365.CsdnFilter; if (args.length = 2) filterClazz = args1; filter = (Filter) Class.forName(filter
23、Clazz).newInstance(); int total = 10; int count = 0; for (int i = 0; i total; i+) boolean b = recognize(i); if (b) count+; System.out.println(rate: + (count * 1.0 / total * 100) + %100); /* * throws IOException */ private static boolean recognize(int num) throws IOException BufferedImage bi = ImageI
24、O.read(new URL(url); ImageIO.write(bi,png,new File(num+.png); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 11 页 - - - - - - - - - ImageData ia2 = new ImageData(bi, filter); ImageData ii = ia2.split(); ArrayList list = new ArrayList(); ImageData template = Im
25、ageData.decodeFromFile(template.data); HashMap map = new HashMap(); for (int i = 0; i template.length; i+) map.put(templatei, new Character(templatei.code); for (int x = 0; x 15) continue; Character c = (Character) map.get(imageArr); if (c ! = null) list.add(c); String s = ; System.out.print(num + :); for (Iterator iter = list.iterator(); iter.hasNext();) Character c = (Character) iter.next(); s += c; System.out.print(c); System.out.println(); return s.length() != 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 11 页 - - - - - - - - -