【验证码】使用Tesseract实现简单的验证码识别

1、Tesseract介绍:

Tesseract的OCR引擎最先由HP实验室于 1985年开始研发,至1995年时已经成为OCR业内最准确的三款识别引擎之一。然而,HP不久便决定放弃OCR业务,Tesseract也从此尘封。 数年以后,HP意识到,与其将Tesseract束之高阁,不如贡献给开源软件业,让其重焕新生--2005年,Tesseract由美国内华达州信息技 术研究所获得,并求诸于Google对Tesseract进行改进、消除Bug、优化工作。

2、下载 Tesseract:

http://code.google.com/p/tesseract-ocr/downloads/list ,可以到这里下载一些识别必须的文件。

3、编写代码:

Tesseract可以在命令行中运行,但觉得不太好用,于是就通过代码调用DOS命令实现图像识别 

 

复制代码
using System.Diagnostics;
                 Process p = new Process();
                 p.StartInfo.FileName = "cmd.exe ";
             p.StartInfo.UseShellExecute = false;
                 p.StartInfo.RedirectStandardInput = true;
                 p.StartInfo.RedirectStandardOutput = true;
                 p.StartInfo.RedirectStandardError = true;
                 p.StartInfo.CreateNoWindow = true;
 
                 p.Start();
                 string cmd = "tesseract.exe " + PicPath + " " + filename + " -l chi_sim";//主要这句代码在起作用
                 p.StandardInput.WriteLine(cmd);
             p.Close();
复制代码

图片识别以后会把识别的内容保存在一个txt文件中,我们可以通过代码将保存在txt文件中的内容读出来,直观的显示出来

读取txt文件 

public static string LoadDataFromTXT(string filePath)
       {
            string[] line = System.IO.File.ReadAllLines(filePath);
           return line[0];
         }

   

 

这样运行以后就可以看到这样的效果

 

经过多次实验发现对于这种简单的数字验证码,识别正确率还是非常高的,但是对于我们中华民族博大精深的汉字,它表现的是否也如此优秀呢?那就让我们来做一下实验。。

在网上随便截一张带有汉字的图片:

让我们来看看汉字的识别效果吧:

大家看了一定很失望吧,对于汉字不是太给力,得到的结果乱七八糟,根本不能用,还要有很大的改进之处啊。。。不过它还是为我们识别一般验证码提供了方便。。

 

到这也基本讲完了,顺便讲一下如何实现窗体淡入淡出效果

复制代码
using System.Runtime.InteropServices;
    public class Win32
    {
        public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口
        public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口
        public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口
        public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口
        public const Int32 AW_CENTER = 0x00000010;
        public const Int32 AW_HIDE = 0x00010000; // 在窗体卸载时若想使用本函数就得加上此常量
        public const Int32 AW_ACTIVATE = 0x00020000; //在窗体通过本函数打开后,默认情况下会失去焦点,除非加上本常量
        public const Int32 AW_SLIDE = 0x00040000;
        public const Int32 AW_BLEND = 0x00080000; // 淡入淡出效果
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool AnimateWindow(
        IntPtr hwnd, // handle to window
        int dwTime, // duration of animation
        int dwFlags // animation type
        );
    }
Win32.AnimateWindow(this.Handle, 1000, Win32.AW_BLEND);//淡入
Win32.AnimateWindow(this.Handle, 1000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND);//淡出
复制代码

 

效果呢就是这样:

原文地址:https://www.cnblogs.com/teacher/p/3916548.html