版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、using system;using system.net;using system.web;using system.drawing;using system.drawing.imaging;namespace crmonternet. /*/ <summary> / crmonternetimg 的摘要说明。 / </summary>
2、; public class crmonternetimg . /*/ <summary> / cookie集合,使用这个cookie集合去获取验证码 / <
3、/summary> private cookiecollection cookie=null; /*/ <summary> / 验证码的地址 / </
4、summary> private string imgurl="" /*/ <summary> / 构造函数 / </summar
5、y> / <param name="strurl">验证码地址</param> / <param name="cookies">cookie集合</param> public
6、;crmonternetimg(string strurl, cookiecollection cookies) . this.imgurl = strurl; this.
7、cookie = cookies; private bool _debug=false; /*/ <summary> / 设置是否为debug,如果是
8、debug的话,就把当前图片保存到c:.bmp / </summary> public bool debug . set &
9、#160; . _debug=value;
10、; get . return _debug;
11、 /*/ <summary> / 识别主函数 / </summary> / <returns&
12、gt;</returns> public string proc() . /图形 bitmap&
13、#160;bmp=null; /保存深浅2种颜色 color cc = new color2; int left
14、= new int5;/左边的点 int right = new int5;/右边的点 int top = new int5;/顶点
15、 int bottom = new int5;/底点 string str = "" try
16、160; . int i=0; / '抓取图片
17、160; while (i != 4) ./一直抓到图形中有4个独立的字符
18、0; bmp = this.crimg(); if(debug) . &
19、#160; bmp.save("c:1.bmp"); &
20、#160; cc = this.getdeepcolor(bmp);/得到深浅颜色 this.formatimg(bmp, cc)
21、;/格式话图形,去掉背景 array array1 = this.getv(bmp);/得到x轴上有黑色点的坐标
22、160; i = this.chkimg(array1, ref left, ref right);/分析字符个数 /这里分析单个字符的上下左右点,
23、这样就可以确定范围了 this.gettb(bmp, left , right, ref top, ref bottom); /识别出来 &
24、#160; str = this.crimg(bmp, left, right, top, bottom); /释放资源
25、160; bmp.dispose(); catch (exception exception2)
26、0; . /projectdata.setprojecterror(exception2); exception exception1 =
27、160;exception2; /projectdata.clearprojecterror(); finally
28、; ./释放资源 if (bmp != null) .
29、60; bmp.dispose(); &
30、#160; return str; 其他辅助函数#region 其他辅助函数 /*/ <summary>
31、160; / 加载验证码 / </summary> / <returns>验证码图形</returns> private bitmap crimg()
32、 . bitmap bmp=null; /响应 httpwebresponse response
33、=null; try . /请求
34、; httpwebrequest request = (httpwebrequest) webrequest.create(this.imgurl); /这里附加cookie
35、60; request.cookiecontainer = new cookiecontainer(); if (this.cookie != null)
36、60; . request.cookiecontainer.add(this.cookie);
37、60; /得到响应 response = (httpwebresponse) request.getresponse();
38、0; /从响应流直接创建图片 bmp = new bitmap(response.getresponsestream();
39、0; catch . finally
40、60; ./清理资源 if (response != null)
41、60;. response.close();
42、60; return bmp; /*/ <summary> / "得到垂直投影",就是得
43、到字符在y轴上有的点 / </summary> / <param name="img">图形</param> / <returns>返回这些点所在的array</returns>
44、0; private array getv(bitmap img) . string str = ""
45、160; for (int i = 0; i <= img.width - 1; i+) ./在图形的x轴上进行循环
46、; /然后在y轴上找黑点 for (int h = 0; h <= img.height
47、0;- 1; h+) . color c = img.getpixel(i, h);
48、160; if (c.toargb() = color.black.toargb() ./如果找到一个黑点的话,就把这个x轴的坐标
49、给记忆下来。 str = str + "," + i.tostring(); &
50、#160; break; &
51、#160; if (str.length > 0) ./因为上面采用连接的方式,所以第一个字符可能是“,”,这里去掉
52、0; str = str.substring(1); return str.split(new char . ',&
53、#39; ); /*/ <summary> / 查找单个字符中图形中最上和最下的黑点。 / </summary>
54、; / <param name="img">图形</param> / <param name="ints">四个字符的左边点</param> / <param name="inte">四
55、个字符的右边点</param> / <param name="t">传出参数:保存四个字符顶点</param> / <param name="b">传出参数:保存四个字符底点</param>
56、; / <returns></returns> private object gettb(bitmap img, int ints, int inte, ref int t, ref int b) . &
57、#160; /用来计数,最多四个数字。 int i = 0; while (true)
58、 ./一个循环处理一个图形 for (int w = intsi; w <= intei; w+)
59、 ./对其中的一个字符从左到右开始找 int h = 0;
60、 do ./然后在上面指定的范围内查找最上黑点和最下黑点的位置 &
61、#160; color c1 = img.getpixel(w, h); if (c1.toargb() = color.black.toargb()
62、 . if (ti = 0)
63、60; .
64、160; ti = h;
65、60; if (h < ti)
66、0; . ti = h;
67、0; if (h > bi)
68、; .
69、0; bi = h;
70、; h+;
71、160; while (h <= 0x13);
72、160; i+; if (i > 3) &
73、#160; . return null; &
74、#160; /*/ <summary> / 得到深色、浅色-深色的rgb刚好相差30 / </summary>
75、; / <param name="img"></param> / <returns></returns> private color getdeepcolor(bitmap img)
76、0; . color c=color.white;/深色 color c1=color.white;/浅色
77、 color cc = new color3; if (img = null) ./检查图片是否有效
78、160; return null; for (int w = 0; w <
79、;= img.width - 1; w+) ./从做到右开始找。 int h = 4;
80、160; do ./经过观察,y方向4-6坐标的三行绝对可以找到这2种颜色 color te
81、mp = img.getpixel(w, h);/ 当前色 if (temp.r < 0xff)
82、; ./'有颜色的时候才在处理 if (c.r > 0)
83、 . /用找到的颜色和当前颜色比较
84、60; /'如果找到的颜色比当前颜色要小 /
85、 '那么 c就是深色 / 'c1就是浅色
86、 if (c.r < temp.r) .
87、 c1 = temp;
88、0; if (c.r > temp.r)
89、; .
90、0; c1 = c; c = temp;
91、0;
92、0; else .
93、; c = temp;
94、0; if (c1.toargb() > 0)
95、160; . break; &
96、#160; h+;
97、0; while (h <= 6); &
98、#160; cc0 = c; cc1 = c1; return cc;
99、60; /*/ <summary> / 去背景,单色显示 / </summary> / <param name="img"></para
100、m> / <param name="cc"></param> / <returns></returns> private object formatimg(bitmap img,
101、60;color cc) . object obj=null; /循环处理每一点 &
102、#160; for (int w = 0; w <= img.width - 1; w+) .
103、0; for (int h = 0; h <= img.height - 1; h+) .
104、 /如果当前颜色是深色的话,就转化成黑色 /否则就转化成白色
105、160; color c = img.getpixel(w, h); if (c.toargb() = cc1.toargb()
106、; . img.setpixel(w, h, color.white);
107、0; if (c.toargb() = cc0.toargb()
108、160; . img.setpixel(w, h, color.black); &
109、#160; &
110、#160; return obj; /*/ <summary> / 识别图形的字符 / </summary
111、> / <param name="img">图形</param> / <param name="ints">计算出来的左边点</param> / <param
112、;name="inte">计算出来的上面点</param> / <param name="t">计算出来的上面点</param> / <param name="b">计算出来的顶点</param> &
113、#160; / <returns>字符</returns> private string crimg(bitmap img, int ints, int inte, int t, int b) . &
114、#160; string str = "" int i = 0; do
115、 ./依次处理4个字符 /获取当前范围:四个构造函数分别是x,y ,宽和高,已经可以确定字符的范围了。 rectangle r
116、160;= new rectangle(intsi, ti, (intei - intsi) + 1, (bi - ti) + 1); /裁减出来的图形
117、0; bitmap bmp = img.clone(r, img.pixelformat); /识别并保存出来 str
118、160;= str + this.crimg(bmp); /释放当前图片资源 bmp.dispose();
119、 /处理下一个。 i+;
120、160;while (i <= 3); return str; /*/ <summary> &
121、#160; / 检测当前的图片是否符合社识别条件,通过分析字符数量 / </summary> / <param name="va"></param> / <param
122、60;name="ints">ref:用来保存四个字符的左边点</param> / <param name="inte">ref:用来保存四个字符的右边点</param> / <returns>字符数量</returns>
123、60; private int chkimg(array va, ref int ints, ref int inte) . int intrang=0; /一共找到多少段
124、 string str = (string) va; ints = new int . -1, -1, -1, -1, -1, -1, -1 /保存段的开始数字,多申请几个,避免出
125、问题 inte = new int . -1, -1, -1, -1, -1, -1, -1 /保存段的结束数字,多申请几个,避免出问题 for (i
126、nt i = 0; i <= str.length - 2; i+) . int intcur = int.parse(stri);/当前坐标
127、 int intprew = int.parse(stri + 1);/下一个坐标 if (intprew - intcur) =
128、 1) ./如果2个数字相差一,表示这个数字还是连续的 if (intsintrang = -1)
129、; ./如果已经找到结尾了 intsintrang = intcur;
130、; if (i = (str.length - 2)
131、0; . /如果到了倒数第二数字,最后一个数字就是结束了 inteintrang = intp
132、rew;
133、160; else . inteintrang = intcur;
134、160; intrang+;
135、; return (intrang + 1); /*/ <summary> / 识别小图片中的数字 &
136、#160; / </summary> / <param name="img">小图形</param> / <returns>识别出来的字符</returns> private s
137、tring crimg(bitmap img) . if (img != null) ./检查图片 &
138、#160; color c; if (img.width = 6) & (img.height = 9)
139、160; ./宽6 高9的一定是1 return "1"
140、160; if (img.width = 9) & (img.height = 12) ./宽9 高12的一定是4
141、60; return "4"
142、160; if (img.width = 8) & (img.height = 9) ./如果宽是8 高是9的话
143、0; /找特殊点 color c2 = img.getpixel(0, 1);
144、60; if (c2.toargb() = color.black.toargb() ./如果0,1是黑点的话,就是2,否则就是0
145、 return "2"
146、; return "0" if (img.width = 8) & (img.height = 11)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年岑溪市公开招聘专任教师321名(梧州学院专场)备考题库及答案详解(新)
- 广东省建筑工程集团控股有限公司2026届校园招聘备考题库完整答案详解
- 厦门大学哲学系2025年工程、实验系列专业技术中初级职务人员招聘备考题库及完整答案详解一套
- 2025-2026学年颜色艺术教案
- 2025-2026学年小班涂色教案草莓
- 2025-2026学年钓鱼视频教学设计模板
- 2026春牛津译林版英语八年级下册Unit 1 Study skill Task(同步课件)
- 2025-2026学年蒙古语歌曲教学设计英语
- 2025-2026学年语言律动教案中班
- 烟台文化旅游职业学院《医用生物材料B》2024-2025学年第二学期期末试卷
- 2026年江苏信息职业技术学院单招综合素质考试题库及参考答案详解一套
- 成都市金牛区2025年社区网格工作人员考试题库及答案
- 部编七年级-语文文言文练习及答案
- 46566-2025温室气体管理体系管理手册及全套程序文件
- 2025年剑桥商务英语(BEC)初级考试真题及答案
- 茶叶健康的秘密武器-探究茶叶的营养价值与健康影响
- 电工单招实操考试题库及答案
- 施工现场消防应急预案方案
- 分期汽车不过户协议书
- 环境卫生管理知识培训课件
- 2025-2030互联网+中医服务模式创新与用户接受度分析
评论
0/150
提交评论