使用C#版Tesseract库

栏目: ASP.NET · 发布时间: 6年前

内容简介:上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版本,本篇介绍一下如何使用C#版的Tesseract。源码是vs2015编译的,需要安装vs2015以上版本。打开项目后如:

上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版本,本篇介绍一下如何使用C#版的Tesseract。

C#版本源码下载地址:https://github.com/charlesw/tesseract

源码是vs2015编译的,需要安装vs2015以上版本。

打开项目后如:

使用C#版Tesseract库

我们再添加一个winform项目,画界面如:

使用C#版Tesseract库

实现点击“选择需要识别的图片”,打开一张图片,调用算法并显示结果。比较简单。源码如下:

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;
using Tesseract;

namespace TesseractDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //选图片并调用ocr识别方法
        private void btnRec_Click(object sender, EventArgs e)
        {
            //openFileDialog1.Filter = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var imgPath = openFileDialog1.FileName;
                pictureBox1.Image=Image.FromFile(imgPath);
                string strResult = ImageToText(imgPath);
                if (string.IsNullOrEmpty(strResult))
                {
                    txtResult.Text = "无法识别";
                }
                else
                {
                    txtResult.Text = strResult;
                }
            }
        }
        //调用tesseract实现OCR识别
        public string ImageToText(string imgPath)
        {
            using (var engine = new TesseractEngine("tessdata", "eng", EngineMode.Default))
            {
                using (var img = Pix.LoadFromFile(imgPath))
                {
                    using (var page = engine.Process(img))
                    {
                        return page.GetText();
                    }
                }
            }
        }
    }
}

有一点要注意的是,tesseract的识别语言包要自己下载后包含到项目里面,并设置为始终复制,或者直接把这个文件包放到运行程序目录(bin\debug)下:

使用C#版Tesseract库

eng是英文字符的意思,要识别其他语言字符,需要自己下载:

Tesseract has unicode (UTF-8) support, and can recognize more than 100 languages "out of the box".

这个库支持100种语言的识别

字库下载地址为: https://github.com/tesseract-ocr/tessdata


以上所述就是小编给大家介绍的《使用C#版Tesseract库》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Node.js in Action

Node.js in Action

Mike Cantelon、Marc Harter、TJ Holowaychuk、Nathan Rajlich / Manning Publications / 2013-11-25 / USD 44.99

* Simplifies web application development * Outlines valuable online resources * Teaches Node.js from the ground up Node.js is an elegant server-side JavaScript development environment perfect for scal......一起来看看 《Node.js in Action》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具