环境vs 2019 .Net 4.8
新建一个wpf工程,拖放上一个button一个textbox
nuget下载tesseract,版本信息如图所示
MainWindow.xaml.cs文件代码如下:
using Microsoft.Win32;
using System.Windows;
using Tesseract;
namespace TestOCR
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
var pix = Pix.LoadFromFile(ofd.FileName);
var ocr = new TesseractEngine("./tessdata", "eng",EngineMode.TesseractAndCube);
var page = ocr.Process(pix);
textbox.Text = page.GetText();
textbox.FontSize = 30;
}
}
}
}
去下载tessdata训练文件:
https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
下载后解压,然后一层层文件夹进去找到“tessdata”文件夹,把该文件夹放在wpf工程bin/debug目录里:
然后准备一个带文字的图片,测试: