最近在搞一个无人值守系统时,需要能自动登录,在登录时需要输入验证码,所以研究了验证码识别技术,否则我这个无人值守系统的作用就没有了。目前只测试了字母和数字的识别,准确率还是可以的,呵呵,已经够我自已用了~~,至于中文的识别可以参考我上篇文章:利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别。
验证码识别率如下图:(准确率还可以吧)
好吧,切入正题,赶快上架源码吧~~,不足之处请多多包涵
注意:在使用验证码识别类之前,一、请先安装好ImageMagick,二、需要下载tesseract-2.04.exe.tar.gz和 tesseract-2.00.eng.tar.gz,并解压内容到tesseract目录中,之后拷备tesseract目录至应用程序下(debug 目录下)
一、图片识别,验证识别类
- public class Identify
- {
-
-
-
-
-
-
-
- public static string StartIdentifyingCaptcha(string img_path, string save_dir,int scale)
- {
- return StartIdentifyingCaptcha(img_path, save_dir, 4, 0, scale);
- }
-
-
-
-
-
-
-
-
-
- public static string StartIdentifyingCaptcha(string img_path, string save_dir, byte content_type, int content_length, int scale)
- {
- string captcha = "";
-
-
-
-
-
- do
- {
-
- Common.StartProcess("cmd.exe", new string[] { "cd tesseract" ,
-
-
- String.Format(@"convert.exe -compress none -depth 8 -alpha off -scale {0}% -colorspace gray {1} {2}\captcha.tif",scale,img_path,save_dir),
-
-
- String.Format(@"tesseract.exe {0}\captcha.tif {0}\result",save_dir),
-
- "exit"});
-
-
- StreamReader reader = new StreamReader(String.Format(@"{0}\result.txt", save_dir));
-
- captcha = reader.ReadLine().Trim();
-
- reader.Close();
-
-
- string pattern = "";
-
-
- if (content_type == 0)
- {
- pattern = (content_length > 0) ? "^[0-9]{" + content_length + "}$" : "^[0-9]+$";
- }
-
- else if (content_type == 1)
- {
- pattern = (content_length > 0) ? "^[a-zA-Z]{" + content_length + "}$" : "^[a-zA-Z]+$";
- }
-
- else if (content_type == 2)
- {
- pattern = (content_length > 0) ? "^[a-zA-Z0-9]{" + content_length + "}$" : "^[a-zA-Z0-9]+$";
- }
-
- else if (content_type == 3)
- {
- pattern = (content_length > 0) ? "^[\u4e00-\u9fa5]{" + content_length + "}$" : "^[\u4e00-\u9fa5]+$";
- }
-
- else
- {
- pattern = (content_length > 0) ? "^[A-Za-z0-9\u4e00-\u9fa5]{" + content_length + "}$" : "^[A-Za-z0-9\u4e00-\u9fa5]+$";
- }
-
-
-
- if (pattern != "" && !Regex.IsMatch(captcha, pattern))
- {
- captcha = "";
-
- scale++;
- }
-
-
- if (scale > 900)
- {
- captcha = "";
-
- break;
- }
-
- } while (captcha == "");
-
- return captcha;
- }
- }
二、C#中执行命令行命令的函数
- public class Common
- {
-
- #region 功能描述:静态成员 C#里运行命令行里的命令
-
-
-
-
-
-
- public static string StartProcess(string startFileName, string[] commands)
- {
-
- Process p = new Process();
-
-
-
-
-
-
- p.StartInfo.FileName = startFileName;
-
-
- p.StartInfo.UseShellExecute = false;
-
-
- p.StartInfo.RedirectStandardInput = true;
-
-
- p.StartInfo.RedirectStandardOutput = true;
-
-
- p.StartInfo.RedirectStandardError = true;
-
-
- p.StartInfo.CreateNoWindow = true;
-
-
-
-
- p.Start();
-
-
- foreach (string command in commands)
- {
- p.StandardInput.WriteLine(command);
- }
-
-
- return p.StandardOutput.ReadToEnd();
- }
- #endregion
- }
【识别码】使用(ImageMagick+tesseract-ocr)实现图像验证码识别实例,布布扣,bubuko.com
【识别码】使用(ImageMagick+tesseract-ocr)实现图像验证码识别实例
原文:http://www.cnblogs.com/teacher/p/3916547.html