超级简单的Region对比识别验证码

  在一些验证码相对简单的站点中,停一下,什么是简单?以我现在的图形算法修为,就是对于特定的字符,在生成时,其字符的Region应该是一样的,如 chars.bmp(chars.bmp)图片中的6总是这个字体这个字号......
  如果是这样,算法很简单了,没有什么技术含量了。我们只须把验证码图片从左向右一列一列扫描,分隔出每个字符的RGN,然后和chars.bmp中的每个字符的RGN对比,就知道是哪个数字了。
   关键代码如下:
  
None.gif namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        IniFile config 
= null;
InBlock.gif
InBlock.gif        
//查看两个颜色是不是一样,注意这里有一定误差也算相同
InBlock.gif
        public bool IsSameColor(Color c1, Color c2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Math.Abs(c1.R - c2.R) < 10
InBlock.gif                
&& Math.Abs(c1.G - c2.G) < 10
InBlock.gif                
&& Math.Abs(c1.B - c2.B) < 10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//计算一个Region中,像素的个数
InBlock.gif
        public int RegionPointCount(Region r,int width,int height)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
int count = 0;
InBlock.gif            
for(int h = 0; h<width; ++h)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int v = 0; v< height; ++v)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(r.IsVisible(h,v))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
++count;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return count;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//初始化每个字符的Region
InBlock.gif
        public void InitPictureCharInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Bitmap bmp 
= (Bitmap)Bitmap.FromFile("chars.bmp");
InBlock.gif            List
<BitmapCharInfo> bcil = charRgnList;
InBlock.gif            Region rgn 
= new Region();
InBlock.gif            rgn.MakeEmpty();
InBlock.gif            
if (bmp.Height > 0 && bmp.Width > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Color bkColor 
= bmp.GetPixel(00);
InBlock.gif                
bool bInWorking = false;
InBlock.gif                
int nNextStartPos = 0;
InBlock.gif                
for (int h = 0; h < bmp.Width; ++h)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
bool bFindColor = false;
InBlock.gif                    
for (int v = 0; v < bmp.Height; ++v)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (!IsSameColor(bkColor, bmp.GetPixel(h, v)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            rgn.Union(
new Rectangle(h, v, 11));
InBlock.gif                            bFindColor 
= true;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (bInWorking)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (!bFindColor)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            bInWorking 
= false;
InBlock.gif                            rgn.Translate(
-nNextStartPos, 0);
InBlock.gif                            BitmapCharInfo bci 
= new BitmapCharInfo(rgn, h - nNextStartPos, bmp.Height);
InBlock.gif                            bci.orgPos 
= nNextStartPos;
InBlock.gif                            bcil.Add(bci);
InBlock.gif                            rgn 
= new Region();
InBlock.gif                            rgn.MakeEmpty();
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (bFindColor)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            bInWorking 
= true;
InBlock.gif                            nNextStartPos 
= h;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                chars.AddRange(
"0123456789".ToCharArray());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//扫描并识别验证码
InBlock.gif
        public void ScanValidCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Bitmap bmp 
= this.bmpValidCode;
InBlock.gif            List
<BitmapCharInfo> bcil = new List<BitmapCharInfo>();
InBlock.gif            Region rgn 
= new Region();
InBlock.gif            rgn.MakeEmpty();
InBlock.gif            
if (bmp.Height > 0 && bmp.Width > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Color bkColor 
= bmp.GetPixel(00);
InBlock.gif                
bool bInWorking = false;
InBlock.gif                
int nNextStartPos = 0;
InBlock.gif                
for (int h = 0; h < bmp.Width; ++h)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
bool bFindColor = false;
InBlock.gif                    
for (int v = 0; v < bmp.Height; ++v)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (!IsSameColor(bkColor, bmp.GetPixel(h, v)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            rgn.Union(
new Rectangle(h, v, 11));
InBlock.gif                            bFindColor 
= true;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (bInWorking)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (!bFindColor)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            bInWorking 
= false;
InBlock.gif                            rgn.Translate(
-nNextStartPos, 0);
InBlock.gif                            BitmapCharInfo bci 
= new BitmapCharInfo(rgn, h - nNextStartPos, bmp.Height);
InBlock.gif                            bci.orgPos 
= nNextStartPos;
InBlock.gif                            bcil.Add(bci);
InBlock.gif                            rgn 
= new Region();
InBlock.gif                            rgn.MakeEmpty();
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (bFindColor)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            bInWorking 
= true;
InBlock.gif                            nNextStartPos 
= h;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                List
<char> chs = new List<char>();
InBlock.gif
InBlock.gif                Graphics gh 
= Graphics.FromImage(bmp);
InBlock.gif                
foreach (BitmapCharInfo bci in bcil)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int minPos = -1;
InBlock.gif                    
int minLng = -1;
InBlock.gif                    
for (int i = 0; i < charRgnList.Count; ++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Region r 
= bci.rgn.Clone();
InBlock.gif                        r.Union(charRgnList[i].rgn);
InBlock.gif                        r.Exclude(bci.rgn);
InBlock.gif
InBlock.gif                        
int lng = RegionPointCount(r, bci.width, bci.height);
InBlock.gif
InBlock.gif                        
if (minLng == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            minLng 
= lng;
InBlock.gif                            minPos 
= i;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (lng < minLng)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                minLng 
= lng;
InBlock.gif                                minPos 
= i;
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
if (minPos != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        chs.Add(chars[minPos]);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
string str = new string(chs.ToArray(), 0, chs.Count);
InBlock.gif                
//MessageBox.Show(str);
InBlock.gif
                this.currScanValidCode = str;
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void GetValidCodePicture(CookieContainer cc, WebProxy wp )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//this.pbValidCode.ImageLocation = this.pbValidCode.ImageLocation;
InBlock.gif
            Exception exp = null;
InBlock.gif            
for (int i = 0; i < 3++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Stream s 
= GetDataNonProxy(this.tbValidPicUrl.Text, cc,wp);
InBlock.gif                    Bitmap bmp 
= (Bitmap)Bitmap.FromStream(s);
InBlock.gif                    s.Close();
InBlock.gif
InBlock.gif                    bmpValidCode 
= bmp;
InBlock.gif
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    exp 
= ex;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif        
InBlock.gif            MessageBox.Show(
"猎取图片失败:" + exp == null?"unkown":exp.Message);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        Bitmap bmpValidCode 
= null;
InBlock.gif        
private void btnLoadPic_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
this.GetValidCodePicture(null,null);
InBlock.gif            currValidCode 
= this.currScanValidCode;
InBlock.gif            
this.UpdateDate(false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        List
<BitmapCharInfo> charRgnList = new List<BitmapCharInfo>();
InBlock.gif        List
<char> chars = new List<char>();
InBlock.gif        
private void btnInit_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitPictureCharInfo();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
private void btnScanValidCode_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ScanValidCode();
InBlock.gif            
this.currValidCode = this.currScanValidCode;
InBlock.gif            
this.UpdateDate(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
string currScanValidCode;
InBlock.gif
InBlock.gif          dot.gif
InBlock.gif          dot.gif
InBlock.gif          dot.gif
InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.UpdateDate(true);
InBlock.gif            InitPictureCharInfo();
InBlock.gif            config 
= new IniFile(Application.ExecutablePath + ".ini");
InBlock.gif            lastPwdPosInDict 
= config.GetInt("Process""PwdPostion"0);
InBlock.gif            InitPwdDict();
InBlock.gif            UpdateProcessText();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class BitmapCharInfo 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public BitmapCharInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.rgn = new Region();
InBlock.gif            
this.rgn.MakeEmpty();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public BitmapCharInfo(Region r, int w, int h)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.rgn = r;
InBlock.gif            
this.width = w;
InBlock.gif            
this.height = h;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public Region rgn;
InBlock.gif        
public int width = 0;
InBlock.gif        
public int height = 0;
InBlock.gif        
public int orgPos = 0;
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
  识别效率提高的,但是局限性太高了。现在这类我网站也少得多了,现在放出来。希望抛砖引玉引出图形的高级算法来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值