移动梦网2005版图形码识别vb.net2003源代码

因为涉及到商业软件,所以要等过期以后才发布出来。

2005年4月初,识别入门的时候写的,代码不好,凑合着看吧。

要点:
1.去背景:这个图形码的背景色和文字色是有规律的,所以很好去背静
2.分割随即位置的字符,方法:http://blog.csdn.net/Qqwwee_Com/archive/2006/04/14/662505.aspx

using System;
using System.Net;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

namespace CrMonterNet
...{
    /**//// <summary>
    /// CrMonterNetImg 的摘要说明。
    /// </summary>
    public class CrMonterNetImg
    ...{
        /**//// <summary>
        /// Cookie集合,使用这个cookie集合去获取验证码
        /// </summary>
        private CookieCollection Cookie=null;

        /**//// <summary>
        /// 验证码的地址
        /// </summary>
        private string ImgUrl="";

        /**//// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StrUrl">验证码地址</param>
        /// <param name="Cookies">cookie集合</param>
        public CrMonterNetImg(string StrUrl, CookieCollection Cookies)
        ...{
            this.ImgUrl = StrUrl;
            this.Cookie = Cookies;
        }



        private bool _debug=false;
        /**//// <summary>
        /// 设置是否为debug,如果是debug的话,就把当前图片保存到c:.bmp
        /// </summary>
        public bool debug
        ...{
            set
            ...{
                _debug=value;
            }
            get
            ...{
                return _debug;
            }
        }



        /**//// <summary>
        /// 识别主函数
        /// </summary>
        /// <returns></returns>
        public string proc()
        ...{
            //图形
            Bitmap bmp=null;
            //保存深浅2种颜色
            Color[] cc = new Color[2];
            int[] left = new int[5];//左边的点
            int[] right = new int[5];//右边的点
            int[] top = new int[5];//顶点
            int[] bottom = new int[5];//底点
            string str = "";
            try
            ...{
                int i=0;
                // '抓取图片
                while (i != 4)
                ...{//一直抓到图形中有4个独立的字符

                    bmp = this.crImg();
                    if(debug)
                    ...{
                        bmp.Save("c:\1.bmp");
                    }
                    cc = this.GetDeepColor(bmp);//得到深浅颜色
                    this.FormatImg(bmp, cc);//格式话图形,去掉背景
                    Array array1 = this.GetV(bmp);//得到x轴上有黑色点的坐标
                    i = this.chkImg(array1, ref left, ref right);//分析字符个数
                }
                //这里分析单个字符的上下左右点,这样就可以确定范围了
                this.getTB(bmp, left , right, ref top, ref bottom);

                //识别出来
                str = this.crImg(bmp, left, right, top, bottom);

                //释放资源
                bmp.Dispose();
            }
            catch (Exception exception2)
            ...{
                //ProjectData.SetProjectError(exception2);
                Exception exception1 = exception2;
                //ProjectData.ClearProjectError();
            }
            finally
            ...{//释放资源
                if (bmp != null)
                ...{
                    bmp.Dispose();
                }
            }
            return str;
        }


        其他辅助函数#region 其他辅助函数
        /**//// <summary>
        /// 加载验证码
        /// </summary>
        /// <returns>验证码图形</returns>
        private Bitmap crImg()
        ...{
            Bitmap bmp=null;
            //响应
            HttpWebResponse response=null;
            try
            ...{
                //请求
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.ImgUrl);

                //这里附加cookie
                request.CookieContainer = new CookieContainer();
                if (this.Cookie != null)
                ...{
                    request.CookieContainer.Add(this.Cookie);
                }
                //得到响应
                response = (HttpWebResponse) request.GetResponse();
                //从响应流直接创建图片
                bmp = new Bitmap(response.GetResponseStream());
            }
            catch
            ...{
            }
            finally
            ...{//清理资源
                if (response != null)
                ...{
                    response.Close();
                }
            }
            return bmp;
        }

 
        /**//// <summary>
        /// "得到垂直投影",就是得到字符在y轴上有的点
        /// </summary>
        /// <param name="img">图形</param>
        /// <returns>返回这些点所在的array</returns>
        private Array GetV(Bitmap img)
        ...{
            string str = "";
     
            for (int i = 0; i <= img.Width - 1; i++)
            ...{//在图形的x轴上进行循环
                 
                //然后在y轴上找黑点
                for (int h = 0; h <= img.Height - 1; h++)
                ...{
                    Color c = img.GetPixel(i, h);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{//如果找到一个黑点的话,就把这个x轴的坐标给记忆下来。
                        str = str + "," + i.ToString();
                        break;
                    }
                }
            }
            if (str.Length > 0)
            ...{//因为上面采用连接的方式,所以第一个字符可能是“,”,这里去掉
                str = str.Substring(1);
            }
            return str.Split(new char[] ...{ ',' });
        }



        /**//// <summary>
        /// 查找单个字符中图形中最上和最下的黑点。
        /// </summary>
        /// <param name="Img">图形</param>
        /// <param name="IntS">四个字符的左边点</param>
        /// <param name="IntE">四个字符的右边点</param>
        /// <param name="T">传出参数:保存四个字符顶点</param>
        /// <param name="B">传出参数:保存四个字符底点</param>
        /// <returns></returns>
        private object getTB(Bitmap Img, int[] IntS, int[] IntE, ref int[] T, ref int[] B)
        ...{
            //用来计数,最多四个数字。
            int i = 0;
            while (true)
            ...{//一个循环处理一个图形
     
                for (int w = IntS[i]; w <= IntE[i]; w++)
                ...{//对其中的一个字符从左到右开始找
                    int h = 0;
                    do
                    ...{//然后在上面指定的范围内查找最上黑点和最下黑点的位置
                        Color c1 = Img.GetPixel(w, h);
                        if (c1.ToArgb() == Color.Black.ToArgb())
                        ...{
                            if (T[i] == 0)
                            ...{
                                T[i] = h;
                            }
                            if (h < T[i])
                            ...{
                                T[i] = h;
                            }
                            if (h > B[i])
                            ...{
                                B[i] = h;
                            }
                        }
                        h++;
                    }
                    while (h <= 0x13);
                }


                i++;
                if (i > 3)
                ...{
                    return null;
                }
            }

 


        }

        /**//// <summary>
        /// 得到深色、浅色--深色的RGB刚好相差30
        /// </summary>
        /// <param name="Img"></param>
        /// <returns></returns>
        private Color[] GetDeepColor(Bitmap Img)
        ...{
            Color c=Color.White;//深色 
            Color c1=Color.White;//浅色
            Color[] cc = new Color[3];
            if (Img == null)
            ...{//检查图片是否有效
                return null;
            }
         
            for (int w = 0; w <= Img.Width - 1; w++)
            ...{//从做到右开始找。
                int h = 4;
                do
                ...{//经过观察,y方向4-6坐标的三行绝对可以找到这2种颜色

                    Color temp = Img.GetPixel(w, h);// 当前色
                    if (temp.R < 0xff)
                    ...{//'有颜色的时候才在处理
                        if (c.R > 0)
                        ...{
                            //用找到的颜色和当前颜色比较
                            //'如果找到的颜色比当前颜色要小
                            // '那么 c就是深色
                            // 'c1就是浅色
                            if (c.R < temp.R)
                            ...{
                                c1 = temp;
                            }
                            if (c.R > temp.R)
                            ...{
                                c1 = c;
                                c = temp;
                            }
                        }
                        else
                        ...{
                            c = temp;
                        }
                        if (c1.ToArgb() > 0)
                        ...{
                            break;
                        }
                    }
                    h++;
                }
                while (h <= 6);
            }
            cc[0] = c;
            cc[1] = c1;
            return cc;
        }

        /**//// <summary>
        /// 去背景,单色显示
        /// </summary>
        /// <param name="Img"></param>
        /// <param name="Cc"></param>
        /// <returns></returns>
        private object FormatImg(Bitmap Img, Color[] Cc)
        ...{
            object obj=null;
     
            //循环处理每一点
            for (int w = 0; w <= Img.Width - 1; w++)
            ...{             
                for (int h = 0; h <=  Img.Height - 1; h++)
                ...{
                    //如果当前颜色是深色的话,就转化成黑色
                    //否则就转化成白色
                    Color c = Img.GetPixel(w, h);
                    if (c.ToArgb() == Cc[1].ToArgb())
                    ...{
                        Img.SetPixel(w, h, Color.White);
                    }
                    if (c.ToArgb() == Cc[0].ToArgb())
                    ...{
                        Img.SetPixel(w, h, Color.Black);
                    }
                }
            }
            return obj;
        }


        /**//// <summary>
        /// 识别图形的字符
        /// </summary>
        /// <param name="Img">图形</param>
        /// <param name="IntS">计算出来的左边点</param>
        /// <param name="Inte">计算出来的上面点</param>
        /// <param name="T">计算出来的上面点</param>
        /// <param name="B">计算出来的顶点</param>
        /// <returns>字符</returns>
        private string crImg(Bitmap Img, int[] IntS, int[] Inte, int[] T, int[] B)
        ...{
            string str = "";
            int i = 0;
            do
            ...{//依次处理4个字符

                //获取当前范围:四个构造函数分别是x,y ,宽和高,已经可以确定字符的范围了。
                Rectangle r = new Rectangle(IntS[i], T[i], (Inte[i] - IntS[i]) + 1, (B[i] - T[i]) + 1);
                //裁减出来的图形
                Bitmap bmp = Img.Clone(r, Img.PixelFormat);
                //识别并保存出来
                str = str + this.crImg(bmp);
                //释放当前图片资源
                bmp.Dispose();

                //处理下一个。
                i++;
            }
            while (i <= 3);
            return str;
        }

 


    

        /**//// <summary>
        /// 检测当前的图片是否符合社识别条件,通过分析字符数量
        /// </summary>
        /// <param name="Va"></param>
        /// <param name="IntS">ref:用来保存四个字符的左边点</param>
        /// <param name="IntE">ref:用来保存四个字符的右边点</param>
        /// <returns>字符数量</returns>
        private int chkImg(Array Va, ref int[] intS, ref int[] intE)
        ...{
            int IntRang=0; //一共找到多少段

            string[] str = (string[]) Va;
            intS = new int[] ...{ -1, -1, -1, -1, -1, -1, -1 };  //保存段的开始数字,多申请几个,避免出问题
            intE = new int[] ...{ -1, -1, -1, -1, -1, -1, -1 };  //保存段的结束数字,多申请几个,避免出问题
 
            for (int i = 0; i <= str.Length - 2; i++)
            ...{
                int intCur = int.Parse(str[i]);//当前坐标
                int IntPrew = int.Parse(str[i + 1]);//下一个坐标
                if ((IntPrew - intCur) == 1)
                ...{//如果2个数字相差一,表示这个数字还是连续的

                    if (intS[IntRang] == -1)
                    ...{//如果已经找到结尾了
                        intS[IntRang] = intCur;
                    }
                    if (i == (str.Length - 2))
                    ...{ //如果到了倒数第二数字,最后一个数字就是结束了
                        intE[IntRang] = IntPrew;
                    }
                }
                else
                ...{
                    intE[IntRang] = intCur;
                    IntRang++;
                }
            }
            return (IntRang + 1);
        }

 
        /**//// <summary>
        /// 识别小图片中的数字
        /// </summary>
        /// <param name="Img">小图形</param>
        /// <returns>识别出来的字符</returns>
        private string crImg(Bitmap Img)
        ...{
            if (Img != null)
            ...{//检查图片
                Color c;


                if ((Img.Width == 6) & (Img.Height == 9))
                ...{//宽6 高9的一定是1
                    return "1";
                }


                if ((Img.Width == 9) & (Img.Height == 12))
                ...{//宽9 高12的一定是4
                    return "4";
                }

                if ((Img.Width == 8) & (Img.Height == 9))
                ...{//如果宽是8 高是9的话

                    //找特殊点
                    Color c2 = Img.GetPixel(0, 1);
                    if (c2.ToArgb() == Color.Black.ToArgb())
                    ...{//如果0,1是黑点的话,就是2,否则就是0
                        return "2";
                    }
                    return "0";
                }

                if ((Img.Width == 8) & (Img.Height == 11))
                ...{//宽8  高11
                    c = Img.GetPixel(2, 0);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{//也同样找特殊点来区分2.0
                        return "8";
                    }
                    return "6";
                }
                if ((Img.Width == 8) & (Img.Height == 12))
                ...{//同上的方法区分3.5.7.9
                    //都可以使用特殊点来区分
                    c = Img.GetPixel(5, 2);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{
                        return "3";
                    }
                    c = Img.GetPixel(2, 4);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{
                        return "5";
                    }
                    c = Img.GetPixel(0, 0);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{
                        return "7";
                    }
                    c = Img.GetPixel(0, 5);
                    if (c.ToArgb() == Color.Black.ToArgb())
                    ...{
                        return "9";
                    }
                }
            }
            return " ";
        }
        #endregion

    }
}

转载于:https://www.cnblogs.com/xoyu/archive/2008/12/14/1354605.html

转 移动梦网2005版图形码识别c#.net2003源代码相关推荐

  1. TCP流量控制和拥塞控制 最新网狐荣耀版源码下载

    搭建准备 1.网狐荣耀版源码下载: 下载 2.visual studio 2015 下载 3.下载jdk1.8 安装步骤: 一.安装visual studio 2015,在百度就能搜索到下载地址,在教 ...

  2. 召唤神龙-万宁版源码,游戏直播用不挂(麦田资源网)

    <!DOCTYPE html> <html><head><meta charset="utf-8"><title>召唤神 ...

  3. 500vip网全套源码下载 带精美手机版源码

    500vip网全套源码下载 带精美手机版源码 源码说明:         今天给大家分享的源码这个源码是"500vip网全套源码下载 带精美手机版源码" ,以下所有截图均为本站源码 ...

  4. 下载附带.php,PHP开发的优客365网址导航商业精华版1.1.6版本源码带WAP手机版附带三款模板和四款插件-资源下载随便下源码网...

    PHP开发的优客365网址导航商业精华版1.1.6版本源码,带WAP手机版,附带三款模板和四款插件 p优客365网站分类导航系统是个跨平台的开源软件,基于PHPMYSQL开发构建的开源网站分类目录管理 ...

  5. 隔空看脸网恋照妖镜源码-邀请视频通话版,带搭建说明

    隔空看脸网恋照妖镜源码-邀请视频通话版,带搭建说明 源码下载地址:点击下载照妖镜源码 如果不想下载源码搭建:可以直接用现成的 [点击直接使用]隔空看脸网恋照妖镜 文件名-隔空看脸网恋照妖镜源码 编程语 ...

  6. android wear 神奇宝贝,宝可梦Home安卓版下载_宝可梦Home手机app官方版(Pokemon Home) v1.0.3-安族游戏网...

    宝可梦Home安卓版(Pokemon Home)是基于云技术所打造的一款云端服务app,该应用能够连接不同的设备,并且通能够同步不同设备的宝可梦进行宝可梦数据交换,同时移动端也能够和ns端利用此应用来 ...

  7. 倾城Q码 倾城梦网 掌上投洽会 掌上6 18首页

    http://wap.chinafair.org.cn/chinese/index.asp 掌上投洽会首页 技术支持 倾城梦网 http://code.qcmw.com/Wap/ http://cod ...

  8. 全新开发周易起名网PHP网站源码+后台修复版

    正文 已经修复后台无法登录的BUG,最新还不错的周易起名网PHP完整源码,下方是这个程序的功能介绍: 1.支持宝宝在线起名 2.支持周易取名 3.带在线下单起名 4.支持一对一起名 5.支持手机wap ...

  9. Java_JSP电子政务网(论文+源码)

    摘 要 当前阶段,伴随着社会信息技术的快速发展,使得电子政务能够成为我国政府职能部门进行办公管理的一个重要内容,其中就包括了提升办公效率完善服务质量以及实现信息共享等.但是,虽然当前我国政府的信息化事 ...

最新文章

  1. android studio 前言中不允许有内容_Android Studio 中 System Trace 的新增功能
  2. n个数分为m堆有多少种分法(青岛理工邀请赛)动态规划
  3. 前端学习(2237):react实现疫情数据
  4. linux 如何安装Python3.5
  5. 【DS】线段树HDU-1166
  6. Altium AD20删除机械层MECH
  7. 很好的一款监控web请求的工具,fiddler.
  8. 内容范围:正态分布,泊松分布,多项分布,二项分布,伯努利分布
  9. 电脑太慢了最简单的办法怎么弄_最简单快速的方法恢复电脑误删文件-数据恢复常见问题...
  10. 微信小程序,模板+按钮+绑定事件
  11. app上线发布流程_完整iOS APP发布App Store上架流程
  12. CPU天梯图2022
  13. 【顺序栈】32 顺序栈ADT模板简单应用算法设计:火车调度
  14. 树莓派wiringPi输出PMW
  15. 为什么upupoo显示服务器维护中,首先,电脑能正常上网,有的网站可以上传图片,但就是不能显示上传图片的功能按钮,网页打开都正常。...
  16. 美国通胀大幅下降?可衰退却成为投资者2023年最担心的问题
  17. 简单好玩经典有趣的微信小游戏
  18. php做微信支付和支付宝支付的方法
  19. Python实战——自动生成情人节对女朋友表白玫瑰花、爱心,以及用Python画一颗樱花树,愿祖国繁花与共!
  20. 浏览器缓存机制(强缓存和协商缓存)

热门文章

  1. Linux内核之 printk 打印
  2. [Linux] Samba服务器配置 安全级别为share
  3. 日本煤炉Mercari运营详细教程:煤炉店铺运营没有你想的那么简单
  4. java excel 数组_Java将Excel解析为数组集合
  5. android 显示电池电量
  6. html 折线边框,HTML中的边框属性
  7. 一些简单原生js demo
  8. 学习资料:新东方老师的“超级背单词法”
  9. tomcat内存溢出自动重启
  10. 27个iOS开发小技巧