验证码处理

算法 2016-01-26 16:19

找了一份不错的源码,学习一下验证码识别

第一步当然是批量下载验证码了

        static void Main(string[] args)
        { 
            string downloadPath = Path.Combine(Config.Root, @"yzm\58"); 
            for (int index = 0; index < 200; index  )
            { 
                Random r = new Random(); 
                string url = "https://passport.58.com/validatecode?temp=123ijv4f6wg&t="   r.Next(1, 9999);
                SaveCodeImageFromUrl(url, downloadPath   "\\"   index   ".jpg"); 
            }
        }


        public static void SaveCodeImageFromUrl(string url, string path)
        {

            WebRequest re = WebRequest.Create(url);
            WebResponse rp = re.GetResponse();
            Stream s = rp.GetResponseStream();
            Image img = Image.FromStream(s);
            s.Close();
            s.Dispose();
            img.Save(path);
        }

第二步,灰度处理

        static void Main(string[] args)
        { 
            string downloadPath = Path.Combine(Config.Root, @"yzm\58\");

            var files=Directory.GetFiles(downloadPath "old");
            foreach (var file in files)
            {
                Image img = Image.FromFile(file);
                Image img_grey = (Image)img.Clone();

                string imgOriginal = Image2Num((Bitmap)img_grey);


                if (!Directory.Exists("experiment\\"))
                {
                    Directory.CreateDirectory("experiment\\");
                }
                //imgurl = pborigin.ImageLocation;
                WriteToFile(imgOriginal, "experiment\\"   Path.GetFileNameWithoutExtension(file)   "_original.txt");
                 
                img_grey = ImageProcess.Gray((Bitmap)img_grey, 2);
                string imgGreied = Image2Num((Bitmap)img_grey);
                WriteToFile(imgGreied, "experiment\\"   Path.GetFileNameWithoutExtension(file)   "_greied.txt");

                img_grey.Save(Path.Combine(Config.Root, @"yzm\58\huidu\")   Path.GetFileName(file));

            }
             


        }

        /// <summary>
        /// 将数据写入文件
        /// </summary>
        /// <param name="data"></param>
        /// <param name="path"></param>
        public static void WriteToFile(string data, string path)
        {
            File.WriteAllText(path, data);
        }

        /// <summary>
        /// 图片转换成像素数值
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        #region 图片转换成像素数值
        public static string Image2Num(Bitmap img)
        {

            StringBuilder sb = new StringBuilder();
            string imgPixel = "";
            int height = img.Height;
            int width = img.Width;
            BitmapData bdata = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            int bytelength = width * height * 4;
            byte[] bytes = new byte[bytelength];
            Marshal.Copy(bdata.Scan0, bytes, 0, bytelength);
            int row = 1;
            for (int i = 0; i < bytelength; i  = 4)
            {
                imgPixel = bytes[i]   ","   bytes[i   1]   ","   bytes[i   2];
                if (imgPixel.Length < 11)
                {
                    for (int j = 0; j < 11 - imgPixel.Length; j  )
                    {
                        imgPixel  = " ";
                    }
                }
                imgPixel  = "\t";
                sb.Append(imgPixel);
                if (row % width == 0)
                {
                    sb.Append("\r\n");
                }
                row  ;
            }
            img.UnlockBits(bdata);
            return sb.ToString();
        }
        #endregion



Leave a Reply