using System;
using System.IO;
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 Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms;
using Leadtools.Forms.DocumentWriters;
using Leadtools.Forms.Ocr;
using Leadtools.ImageProcessing;
using Leadtools.WinForms;
using Leadtools.Forms.Ocr.Advantage;
namespace SampleOcrChinese1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IOcrEngine _ocrEngine;
private IOcrDocument _ocrDocument;
private void Form1_Load(object sender, EventArgs e)
{
InitializeComponent();
// 解锁OCR功能,用您的密钥替换此处
string MY_LICENSE_FILE = @"D:\full_license.key\full_license.lic";
string MY_DEVELOPER_KEY = "************";
RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY);
// 初始化OCR引擎
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false);
// 启动OCR引擎
_ocrEngine.Startup(null, null, null, @"D:\LEADTOOLS 19\Bin\Common\OcrProfessionalRuntime64");
// 创建文档
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
//将语言设置为中文简体,中文繁体为"zh-Hant"
_ocrEngine.LanguageManager.EnableLanguages(new string[] { "zh-Hans" });
MessageBox.Show("OCR引擎成功启动");
}
private void button1_Click(object sender, EventArgs e)
{
// 启动OCR引擎
_ocrEngine.Startup(null, null, null, @"D:\LEADTOOLS 19\Bin\Common\OcrProfessionalRuntime64");
// 创建文档
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
//将语言设置为中文简体,中文繁体为"zh-Hant"
_ocrEngine.LanguageManager.EnableLanguages(new string[] {"zh-Hans"});
MessageBox.Show("OCR引擎成功启动");
}
private void button2_Click(object sender, EventArgs e)
{
string fileName = @"D:\2.jpg";
//将包含中文字符的文件添加至OCR文档
IOcrPage page = _ocrDocument.Pages.AddPage(fileName, null);
int pageCount = _ocrDocument.Pages.Count;
// // 显示此页面的相关信息
string message = string.Format(
"共{0}页\n最后添加的页面大小 = {1} * {2}\n分辨率 = {3} * {4}\n每个像素的位数 = {5}\n",
pageCount,
page.Width, page.Height,
page.DpiX, page.DpiY,
page.BitsPerPixel);
MessageBox.Show(message);
}
private void button3_Click(object sender, EventArgs e)
{
// 从OCR文档中移除所有添加的页面
_ocrDocument.Pages.Clear();
MessageBox.Show("所有页面已被移除");
}
private void button4_Click(object sender, EventArgs e)
{
// 识别所有页面
// 注意,我们不需要调用AutoZone,引擎会检查页面是否被分区,若无,则会自动分区
_ocrDocument.Pages.Recognize(null);
// 将结果保存为Word文件
string wordFileName = @"D:\Result\Result1.pdf";
_ocrDocument.Save(wordFileName, DocumentFormat.Pdf, null);
// 显示我们刚刚保存的word文件
System.Diagnostics.Process.Start(wordFileName);
}
private void button5_Click(object sender, EventArgs e)
{
// 释放此文档
_ocrDocument.Dispose();
// 关闭OCR引擎
_ocrEngine.Shutdown();
MessageBox.Show("OCR引擎关闭");
}
}
}
|