淘宝验证码破解之轮廓线

现在的验证码已经不是什么花哨的复杂背景了,而趋于背景单一,但字体扭曲,粘连,加上和字体同样颜色的干扰线,所以这个发展趋势就像武功练到一定境界时反而招式简单,却威力巨大。有句话叫做什么来着--返璞归真。

看看淘宝的验证码:checkcode?sessionID=3d8fd93a28e345ea5f1c15860c591a7c&_ts=1263016130038&r=1263016151609&r=1263016154031&r=1263016155796&r=1263016158437&r=1263016165984&r=1263016168984&r=1263016170578&r=1263016175437&r=1263016181078&r=1263016183500&r=1263016185359&r=1263016187125&r=1263016189593&r=1263016197593&r=1263016201906&r=1263016212968&r=1263016219218&r=1263016221375&r=1263016278984&r=1263016284406&r=1263016336093&r=1263016339890&r=1263016341421&r=1263016342859&r=1263016344156&r=1263016345437&r=1263016347843&r=1263016349281&r=1263016350703&r=1263016352109&r=1263016353656&r=1263016355437&r=1263016358843&r=1263016361671&r=1263016363000&r=1263016364000&r=1263016366093&r=1263016368171&r=1263016373000&r=1263016374656&r=1263016388125&r=1263016391234&r=1263016392609&r=1263016395453&r=1263016397000&r=1263016400703&r=1263016405125&r=1263016411078&r=1263016413796&r=1263016421843&r=1263016425281&r=1263016428343&r=1263016433156&r=1263016435203&r=1263016436640&r=1263016440453&r=1263016441875&r=1263016476187&r=1263016481343&r=1263016529171&r=1263016535390&r=1263016536812&r=1263016538046&r=1263016539343&r=1263016546515&r=1263016549109&r=1263016550890&r=1263016552484&r=1263016553796&r=1263016555125&r=1263016556593&r=1263016558406&r=1263016560343让人纠结吧。

 

下面开始我们的破解之旅:淘宝验证码破解之轮廓线。

先从指定连接获取验证码图片:

//获取验证码
        private Bitmap GetCode(string url)
        {
          
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            System.IO.Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
            Image original = Image.FromStream(responseStream);

          
             
               
                Bitmap bitMap = new Bitmap(original);
              
                responseStream.Close();
                return bitMap;

        }

//图片拷贝

由于我们建立的对象是引用类型。所以得用拷贝函数。

private Bitmap CopyImg(Bitmap img)
        {
            int W = img.Width;
            int H = img.Height;
            Bitmap NewImg = new Bitmap(W, H);
            for (int y = 0; y < H; y++)
            {
                for (int x = 0; x < W; x++)
                {
                    Color C = img.GetPixel(x, y);

                    NewImg.SetPixel(x, y, C);
                }
            }
            return NewImg;
        }


        //图象二值化
        public Bitmap BitmapToHead(Bitmap Timg)
        {
            Bitmap img = CopyImg(Timg);
            int W = img.Width;
            int H = img.Height;
           
            for (int y = 0; y < H;y++ )
            {
                for(int x=0;x<W;x++)
                {
                Color c = img.GetPixel(x, y);
                int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);//转换灰度的算法
                img.SetPixel(x, y, Color.FromArgb(luma, luma, luma));


            }
           }

            for(int y = 0; y < H;y++){

                for (int x = 0; x < W; x++)
                {

                  
                Color c = img.GetPixel(x, y);
                if (c.R >= 120)   //设置一个合适的临界值 大于等于临界值设为白色,小于临界值为黑色
                    img.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                else img.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                }
            }
            return img;
          
        }

下面就是关键算法了。跟踪轮廓线:

//轮廓线算法

private Bitmap ContourFollowing(Bitmap Timg)
        {
            Bitmap img = CopyImg(Timg);
            int W = img.Width-1;
            int H = img.Height-1;
            for (int y = 0; y <=H; y++)
            {
                for (int x = 0; x <=W; x++)
                {

                    if (y == 0 && x == 0)
                    {
                        //break;

                    }
                    else if (y == 0 && x > 0 && x < W)
                    {
                       // break;

                    }
                    else if (y == 0 && x == W)
                    {
                       //break;

                    }

                    else if (y > 0 && y < H && x == 0)
                    {
                       // break;

                    }
                    else if (x == 0 && y == H)
                    {
                        //break;

                    }
                    else if (x > 0 && x < W && y == H)
                    {
                        //break;

                    }
                    else if (x == W && y == H)
                    {
                        //break;

                    }
                    else if (x == W&& y > 0 && y < H)
                    {
                        //break;

                    }
                    else
                    {
                       Color Cm = img.GetPixel(x, y);
                        Color[] Ccenter = new Color[] { img.GetPixel(x - 1, y), img.GetPixel(x, y - 1), img.GetPixel(x, y + 1), img.GetPixel(x + 1, y) };

                       
                        int flag = 0;
                        if (Cm.R == 0)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                if (Ccenter[i].R==0||Ccenter[i].R==100)
                                {

                                    flag++;
                                }
                            }
                        }
                       
                        if (flag == 4)
                        {
                          
                            img.SetPixel(x, y, Color.FromArgb(100, 100, 100));
                        }
                       

                    }


                }

 

            }

            for (int y = 0; y < H; y++)
            {

                for (int x = 0; x < W; x++)
                {


                    Color c = img.GetPixel(x, y);
                    if (c.R == 100)   
                        img.SetPixel(x, y, Color.FromArgb(255, 255, 255));
                  
                }
            }
            return img;
        }

下面函数调用,画出轮廓线。

private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox4.Text;
            //Bitmap Map =GetCode(url);
            Bitmap Mapp = CopyImg(GetCode1(url));
            Bitmap Map1 = BitmapToHead(Mapp);
            Bitmap Fuck = CopyImg(Map1);
            Bitmap Map2 = ContourFollowing(Fuck);

            pictureBox3.Image = Map1;

            pictureBox4.Image = Map2;


        }

 2010060118112365.jpg

转载于:https://www.cnblogs.com/supko/archive/2010/06/01/1749439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值