C#联通新版验证码识别的实现

以前写了篇 联通充值卡自动充值的实现,最近发现联通官网改版了,随便看了下发现新版的验证码和以前的不同,发了点时间研究了下他的识别码,它现在的验证码如下

C#联通新版验证码识别的实现

现在将识别步骤说下

1,转换灰度图片

2,清除2px的边框

3,分割验证码个数 (4)

4,生成字模库

经过以上步骤,可以得到下面这个效果

C#联通新版验证码识别的实现

下面为部分实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public String GetCheckString(Bitmap bitmap) {
           UnCodebase ud = new UnCodebase(bitmap);
           ud.GrayByPixels();
           ud.ClearPicBorder(2);
           ud.CutMap(14, 15, 0, 0);
           bitmap = ud.bmpobj;
         //  bitmap = ud.ClearNoise(128, 1);
           String chkcode = "";
           Bitmap[] arrmap = ud.SplitImg(bitmap, 4, 1);
           foreach (Bitmap item in arrmap) {
               String str = ud.GetCodebybitmap(item, 128);
               Boolean isEques = false;
               foreach (String strss in code) {
                   String[] a = strss.Split(':');
                   if (str == a[1]) {
                       chkcode += a[0];
                       isEques = true;
                       break;
                   }
               }
               if (!isEques) {
                   String strCurrent = "";
                   double max = 0.0;
                   foreach (String strss in code) {
                       int len1, len2, min, count = 0;
                       String[] a = strss.Split(':');
                       len1 = a[1].Length;
                       len2 = str.Length;
                       min = len1;
                       if (min > len2) {
                           min = len2;
                       }
                       for (int i = 0; i < min; i++) {
                           if (str[i] == a[1][i]) {
                               count++;
                           }
                       }
                       double currrent = (count * 100.0 / min);
                       if (max < currrent) {
                           max = currrent;
                           strCurrent = a[0].Trim();
                       }
                   }
                   chkcode += strCurrent.Trim();
               }
           }
           return chkcode;
       }

通过这些处理后,识别成功率在90+%以上,

下面附上测试地址,代码  100%C#实现,方便asp.net调用,如果是C/C++实现 asp.net 调非托管的有些麻烦,非得放到System32 或是一个绝对路径下麻烦

测试地址

http://www.fox163.com/UniconTest.aspx

--幸福海

博客地址:http://www.cnblogs.com/ningqhai/

上一篇:TeamViewer如何绑定谷歌二次验证码/谷歌身份验证?


下一篇:ettercap局域网内DNS欺骗(隔壁的哥们轻一点 ...)