
正文
简单验证码识别 tessnet2
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
今天突然间对识别验证码感兴趣,于是网上搜了一下
最简单的是引用tessnet2.dll,然后通过它来识别,代码如下
private void button1_Click(object sender, EventArgs e)
{
string strUrl = @"http://www.gz.gov.cn/sofpro/gecs/common/image.jsp?dt=Thu%20Nov%2024%202011%2017:20:21%20GMT+0800%20(China%20Standard%20Time)"; Bitmap image = GetSourceCode(strUrl);//识别图像
pictureBox1.Image = image;
tessnet2.Tesseract ocr= new tessnet2.Tesseract();//声明一个OCR类
ocr.SetVariable("tessedit_char_whitelist", ""); //设置识别变量,当前只能识别数字。
ocr.Init(Application.StartupPath+@"\tmpe", "eng", false); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:http://code.google.com/p/tesseract-ocr/downloads/list
List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);//执行识别操作
string str="";
foreach (tessnet2.Word word in result) //遍历识别结果。
str += word.Confidence + ":" + word.Text + Environment.NewLine;
MessageBox.Show(str);
} private Bitmap GetSourceCode(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream st = response.GetResponseStream();
Bitmap bitmap = (Bitmap)Bitmap.FromStream(st);
//bitmap.Save(System.Windows.Forms.Application.StartupPath + @"\tmp.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
//bitmap = (Bitmap)Bitmap.FromFile(System.Windows.Forms.Application.StartupPath + @"\tmp.bmp");
return bitmap;
}
由于tessnet2是在netframework2.0下生成的,所以需要配置一下config文件
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
</configuration>
于是就可以用了
注意:http://www.gz.gov.cn/sofpro/gecs/common/image.jsp?dt=Thu%20Nov%2024%202011%2017:20:21%20GMT+0800%20(China%20Standard%20Time)是官方的一个专门是数字的一个验证码图片的网址






