找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3923|回复: 0

一验证码识别的小程序源码

[复制链接]
发表于 2012-2-2 19:39:15 | 显示全部楼层 |阅读模式

前些天,12306这个网站挺火的,看到网上出现了各种各样的登陆、订票插件跟程序。虽然没经历过春运,看到网上各种各样的插件跟工具挺有意思的,下载了几个看了看,都挺不错的。印象中有个java版本的订票程序里面有个验证码识别功能,用tesseract-ocr来识别验证码的,如果验证码不是很复杂识别效果还可以。

开发环境 vs2008  
开发语言C#


使用方法很简单

1.下载tesseract 的.net 类库tessnet2_32.dll ,添加引用。   http://www.pixel-technology.com/freeware/tessnet2/
2.下载tesseract 相对应的语言包。 http://code.google.com/p/tesseract-ocr/downloads/list
3.调用tesseract 的方法进行识别。


具体代码
1.读取网上的验证码到pictureBox中//string url = "https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=lrand";
  1.             
  2. string url =
  3. "http://static.baixing.net/pages/mobile.php?c=bcqsFelX%2bvKQcrnIbhyDYQ%3d%3d/2.jpg";
  4.   
  5.           HttpWebRequest request = WebRequest.Create(url) as
  6. HttpWebRequest;
  7.             
  8.             HttpWebResponse response =
  9. request.GetResponse() as HttpWebResponse;
  10.             System.IO.Stream
  11. responseStream = response.GetResponseStream();
  12.             
  13. this.pictureBox1.Image = Image.FromStream(responseStream);
  14. 2.OCR类
  15.   
  16.       public class Ocr
  17.         {
  18.             public void
  19. DumpResult(List<tessnet2.Word> result)
  20.             {
  21.                
  22. foreach (tessnet2.Word word in result)
  23.                     
  24. //Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
  25.                
  26.     MessageBox.Show(string.Format("{0} : {1}", word.Confidence,
  27. word.Text));
  28.             }
  29.             public
  30. List<tessnet2.Word> DoOCRNormal(Bitmap image, string lang)
  31.             
  32. {
  33.                 tessnet2.Tesseract ocr = new tessnet2.Tesseract();
  34.       
  35.           ocr.Init(null, lang, false);
  36.                
  37. List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
  38.         
  39.         DumpResult(result);
  40.                 return result;
  41.             
  42. }
  43.             System.Threading.ManualResetEvent m_event;
  44.   
  45.           public void DoOCRMultiThred(Bitmap image, string lang)
  46.             
  47. {
  48.                 tessnet2.Tesseract ocr = new tessnet2.Tesseract();
  49.       
  50.           ocr.Init(null, lang, false);
  51.                 // If the OcrDone
  52. delegate is not null then this'll be the multithreaded version
  53.               
  54.   ocr.OcrDone = new tessnet2.Tesseract.OcrDoneHandler(Finished);
  55.             
  56.     // For event to work, must use the multithreaded version
  57.                
  58. ocr.ProgressEvent += new
  59. tessnet2.Tesseract.ProgressHandler(ocr_ProgressEvent);
  60.                
  61. m_event = new ManualResetEvent(false);
  62.                 ocr.DoOCR(image,
  63. Rectangle.Empty);
  64.                 // Wait here it's finished
  65.               
  66.   m_event.WaitOne();
  67.             }
  68.             public void
  69. Finished(List<tessnet2.Word> result)
  70.             {
  71.                
  72. DumpResult(result);
  73.                 m_event.Set();
  74.             
  75. }
  76.             void ocr_ProgressEvent(int percent)
  77.             
  78. {
  79.                 Console.WriteLine("{0}% progression", percent);
  80.         
  81.     }
  82.         }
  83. 3.调用ocr类的方法识别验证码
  84.   
  85.           Ocr ocr = new Ocr();
  86.             //using (Bitmap bmp = new
  87. Bitmap(@"D:\temp\ocr\b1.bmp"))
  88.             using (Bitmap bmp = new
  89. Bitmap(this.pictureBox1.Image))
  90.             {
  91.                
  92. tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
  93.                
  94. tessocr.SetVariable("tessedit_char_whitelist",
  95. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  96.                 tessocr.Init(null,
  97. "eng", false);
  98.                 tessocr.GetThresholdedImage(bmp,
  99. Rectangle.Empty).Save(System.Environment.GetEnvironmentVariable("TEMP") + "\" +
  100. Guid.NewGuid().ToString() + ".bmp");
  101.                 // Tessdata directory
  102. must be in the directory than this exe
  103.                
  104. //Console.WriteLine("Multithread version");
  105.                
  106. //ocr.DoOCRMultiThred(bmp, "eng");
  107.                
  108. //Console.WriteLine("Normal version");
  109.                
  110.               
  111.   
  112.                 ocr.DoOCRNormal(bmp, "eng");
  113.             }
复制代码
不足之处,如果验证码比较复杂,识别效果不太好,为了提高验证率,可以自己进行训练。具体可参考 http://code.google.com/p/tesseract-ocr/












如果你发现有什么不合理的,需要改进的地方,或者你有什么更好的实现方法邮件联系328452421@qq.com(qq常年不在线,邮件联系) 朱晓 。相互交流 谢谢

源码下载地址  http://download.csdn.net/detail/xiaoxiao108/4041404


作者:xiaoxiao108 发表于2012-2-2 17:03:37 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 14:52 , Processed in 0.014545 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表