您的位置:首页 > 编程语言 > C#

C#WinForm-OCRTessnet图像识别

2017-05-10 15:16 507 查看
基于OCRTessnet图像识别

首先下载OCRTessnet库文件和语言包。点击下载

在项目中添加库文件tessnet2-64的引用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
using System.Drawing;

using tessnet2;
public partial class OCRTessnet : Form
{
public string imgPath;
private Bitmap image;
private Tesseract ocr;
public OCRTessnet(string imgPath)
{
InitializeComponent();
this.imgPath = imgPath;
imgPath = @"F:\doc\cLibrary\C#\2.png"; //识别目标图片
image = new Bitmap(imgPath);
//image = new Bitmap(@"C:\OCRTest\number.jpg");
ocr = new Tesseract();
ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only
//@"C:\OCRTest\tessdata" contains the language package, without this the method crash and app breaks
ocr.Init(@"F:\doc\cLibrary\tessdata", "eng", true);//添加语言包引用
pbResult.ImageLocation = imgPath;
}

private void btnTessnet_Click(object sender, EventArgs e)
{
try
{
var result = ocr.DoOCR(image, Rectangle.Empty);
string temp="";
foreach (Word word in result)
temp += word.Text+ "\r\n";
tbResult.Text = temp;

}
catch (Exception exception)
{

}
}

private void pbResult_Click(object sender, EventArgs e)
{

}

private void btnBack_Click(object sender, EventArgs e)
{
backMain();
}
private void backMain()
{
Form1 form1 = new Form1();
this.Hide();
form1.Show();
}

}
}


这里我是在其他窗体跳转到当前窗体,区别在于构造函数多了一个参数imgPath,这里你可以删掉然后写成局部变量。
下边是运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: