首页 > 其他 > 详细

关于验证码识别2

时间:2014-08-14 16:31:19      阅读:333      评论:0      收藏:0      [点我收藏+]

 最近写了几个网站的验证码图片自动识别程序,尽管每个网站的验证码图片都不相同,识别的方法有所差别。但写得多了,也总结出不少相同之处。今天抽空封装出一个基础类来,发现可以很好地重复利用,编写不同的验证码识别程序,效率提高了不少。好东东不能独享,现放出来供大家共同研究,请网友们妥善用之。
        封装后的类使用很简单,针对不同的验证码,相应继承修改某些方法,即可简单几句代码就可以实现图片识别了:
            GrayByPixels(); //灰度处理
            GetPicValidByValue(128, 4); //得到有效空间
            Bitmap[] pics = GetSplitPics(4, 1);     //分割
            string code = GetSingleBmpCode(pics[i], 128);   //得到代码串
          

            具体使用,请参见我做的例子:
                 投票程序示例.exe      投票程序源码
       (例子说明:使用进程投票,可自动清除Alert弹出窗口,可自动换IP,ADSL用户自行修改Restart.bat中第三行内容)

bubuko.com,布布扣using System;
bubuko.com,布布扣using System.Collections.Generic;
bubuko.com,布布扣using System.Text;
bubuko.com,布布扣using System.Collections;
bubuko.com,布布扣using System.Drawing;
bubuko.com,布布扣using System.Drawing.Imaging;
bubuko.com,布布扣using System.Runtime.InteropServices;
bubuko.com,布布扣
bubuko.com,布布扣namespace BallotAiying2
bubuko.com,布布扣{
bubuko.com,布布扣    class UnCodebase
bubuko.com,布布扣    {
bubuko.com,布布扣        public Bitmap bmpobj;
bubuko.com,布布扣        public UnCodebase(Bitmap pic)
bubuko.com,布布扣        {
bubuko.com,布布扣            bmpobj = new Bitmap(pic);    //转换为Format32bppRgb
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 根据RGB,计算灰度值
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="posClr">Color值</param>
bubuko.com,布布扣        /// <returns>灰度值,整型</returns>
bubuko.com,布布扣        private int GetGrayNumColor(System.Drawing.Color posClr)
bubuko.com,布布扣        {
bubuko.com,布布扣            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 灰度转换,逐点方式
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void GrayByPixels()
bubuko.com,布布扣        {
bubuko.com,布布扣            for (int i = 0; i < bmpobj.Height; i++)
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < bmpobj.Width; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
bubuko.com,布布扣                    bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
bubuko.com,布布扣                }
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 去图形边框
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="borderWidth"></param>
bubuko.com,布布扣        public void ClearPicBorder(int borderWidth)
bubuko.com,布布扣        {
bubuko.com,布布扣            for (int i = 0; i < bmpobj.Height; i++)
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < bmpobj.Width; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
bubuko.com,布布扣                        bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
bubuko.com,布布扣                }
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 灰度转换,逐行方式
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void GrayByLine()
bubuko.com,布布扣        {
bubuko.com,布布扣            Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
bubuko.com,布布扣            BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
bubuko.com,布布扣            //    bmpData.PixelFormat = PixelFormat.Format24bppRgb;
bubuko.com,布布扣            IntPtr scan0 = bmpData.Scan0;
bubuko.com,布布扣            int len = bmpobj.Width * bmpobj.Height;
bubuko.com,布布扣            int[] pixels = new int[len];
bubuko.com,布布扣            Marshal.Copy(scan0, pixels, 0, len);
bubuko.com,布布扣
bubuko.com,布布扣            //对图片进行处理
bubuko.com,布布扣            int GrayValue = 0;
bubuko.com,布布扣            for (int i = 0; i < len; i++)
bubuko.com,布布扣            {
bubuko.com,布布扣                GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
bubuko.com,布布扣                pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb();      //Color转byte
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            bmpobj.UnlockBits(bmpData);
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 得到有效图形并调整为可平均分割的大小
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="dgGrayValue">灰度背景分界值</param>
bubuko.com,布布扣        /// <param name="CharsCount">有效字符数</param>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        public void GetPicValidByValue(int dgGrayValue, int CharsCount)
bubuko.com,布布扣        {
bubuko.com,布布扣            int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
bubuko.com,布布扣            int posx2 = 0; int posy2 = 0;
bubuko.com,布布扣            for (int i = 0; i < bmpobj.Height; i++)      //找有效区
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < bmpobj.Width; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    int pixelValue = bmpobj.GetPixel(j, i).R;
bubuko.com,布布扣                    if (pixelValue < dgGrayValue)     //根据灰度值
bubuko.com,布布扣                    {
bubuko.com,布布扣                        if (posx1 > j) posx1 = j;
bubuko.com,布布扣                        if (posy1 > i) posy1 = i;
bubuko.com,布布扣
bubuko.com,布布扣                        if (posx2 < j) posx2 = j;
bubuko.com,布布扣                        if (posy2 < i) posy2 = i;
bubuko.com,布布扣                    };
bubuko.com,布布扣                };
bubuko.com,布布扣            };
bubuko.com,布布扣            // 确保能整除
bubuko.com,布布扣            int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;   //可整除的差额数
bubuko.com,布布扣            if (Span < CharsCount)
bubuko.com,布布扣            {
bubuko.com,布布扣                int leftSpan = Span / 2;    //分配到左边的空列 ,如span为单数,则右边比左边大1
bubuko.com,布布扣                if (posx1 > leftSpan)
bubuko.com,布布扣                    posx1 = posx1 - leftSpan;
bubuko.com,布布扣                if (posx2 + Span - leftSpan < bmpobj.Width)
bubuko.com,布布扣                    posx2 = posx2 + Span - leftSpan;
bubuko.com,布布扣            }
bubuko.com,布布扣            //复制新图
bubuko.com,布布扣            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bubuko.com,布布扣            bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
bubuko.com,布布扣        }
bubuko.com,布布扣        
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 得到有效图形,图形为类变量
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="dgGrayValue">灰度背景分界值</param>
bubuko.com,布布扣        /// <param name="CharsCount">有效字符数</param>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        public void GetPicValidByValue(int dgGrayValue)
bubuko.com,布布扣        {
bubuko.com,布布扣            int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
bubuko.com,布布扣            int posx2 = 0; int posy2 = 0;
bubuko.com,布布扣            for (int i = 0; i < bmpobj.Height; i++)      //找有效区
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < bmpobj.Width; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    int pixelValue = bmpobj.GetPixel(j, i).R;
bubuko.com,布布扣                    if (pixelValue < dgGrayValue)     //根据灰度值
bubuko.com,布布扣                    {
bubuko.com,布布扣                        if (posx1 > j) posx1 = j;
bubuko.com,布布扣                        if (posy1 > i) posy1 = i;
bubuko.com,布布扣
bubuko.com,布布扣                        if (posx2 < j) posx2 = j;
bubuko.com,布布扣                        if (posy2 < i) posy2 = i;
bubuko.com,布布扣                    };
bubuko.com,布布扣                };
bubuko.com,布布扣            };
bubuko.com,布布扣            //复制新图
bubuko.com,布布扣            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bubuko.com,布布扣            bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 得到有效图形,图形由外面传入
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="dgGrayValue">灰度背景分界值</param>
bubuko.com,布布扣        /// <param name="CharsCount">有效字符数</param>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
bubuko.com,布布扣        {
bubuko.com,布布扣            int posx1 = singlepic.Width; int posy1 = singlepic.Height;
bubuko.com,布布扣            int posx2 = 0; int posy2 = 0;
bubuko.com,布布扣            for (int i = 0; i < singlepic.Height; i++)      //找有效区
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < singlepic.Width; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    int pixelValue = singlepic.GetPixel(j, i).R;
bubuko.com,布布扣                    if (pixelValue < dgGrayValue)     //根据灰度值
bubuko.com,布布扣                    {
bubuko.com,布布扣                        if (posx1 > j) posx1 = j;
bubuko.com,布布扣                        if (posy1 > i) posy1 = i;
bubuko.com,布布扣
bubuko.com,布布扣                        if (posx2 < j) posx2 = j;
bubuko.com,布布扣                        if (posy2 < i) posy2 = i;
bubuko.com,布布扣                    };
bubuko.com,布布扣                };
bubuko.com,布布扣            };
bubuko.com,布布扣            //复制新图
bubuko.com,布布扣            Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bubuko.com,布布扣            return singlepic.Clone(cloneRect, singlepic.PixelFormat);
bubuko.com,布布扣        }
bubuko.com,布布扣        
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 平均分割图片
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="RowNum">水平上分割数</param>
bubuko.com,布布扣        /// <param name="ColNum">垂直上分割数</param>
bubuko.com,布布扣        /// <returns>分割好的图片数组</returns>
bubuko.com,布布扣        public Bitmap [] GetSplitPics(int RowNum,int ColNum)
bubuko.com,布布扣        {
bubuko.com,布布扣            if (RowNum == 0 || ColNum == 0)
bubuko.com,布布扣                return null;
bubuko.com,布布扣            int singW = bmpobj.Width / RowNum;
bubuko.com,布布扣            int singH = bmpobj.Height / ColNum;
bubuko.com,布布扣            Bitmap [] PicArray=new Bitmap[RowNum*ColNum];
bubuko.com,布布扣
bubuko.com,布布扣            Rectangle cloneRect;
bubuko.com,布布扣            for (int i = 0; i < ColNum; i++)      //找有效区
bubuko.com,布布扣            {
bubuko.com,布布扣                for (int j = 0; j < RowNum; j++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    cloneRect = new Rectangle(j*singW, i*singH, singW , singH);
bubuko.com,布布扣                    PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图
bubuko.com,布布扣                }
bubuko.com,布布扣            }
bubuko.com,布布扣            return PicArray;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="singlepic">灰度图</param>
bubuko.com,布布扣        /// <param name="dgGrayValue">背前景灰色界限</param>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
bubuko.com,布布扣        {
bubuko.com,布布扣            Color piexl;
bubuko.com,布布扣            string code = "";
bubuko.com,布布扣            for (int posy = 0; posy < singlepic.Height; posy++)
bubuko.com,布布扣                for (int posx = 0; posx < singlepic.Width; posx++)
bubuko.com,布布扣                {
bubuko.com,布布扣                    piexl = singlepic.GetPixel(posx, posy);
bubuko.com,布布扣                    if (piexl.R < dgGrayValue)    // Color.Black )
bubuko.com,布布扣                        code = code + "1";
bubuko.com,布布扣                    else
bubuko.com,布布扣                        code = code + "0";
bubuko.com,布布扣                }
bubuko.com,布布扣            return code;
bubuko.com,布布扣        }
bubuko.com,布布扣    }
bubuko.com,布布扣}
bubuko.com,布布扣

关于验证码识别2,布布扣,bubuko.com

关于验证码识别2

原文:http://www.cnblogs.com/Bonker/p/3912590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!