快捷搜索:  汽车  科技

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)6. 切换到Form1的代码视图,然后添加如下代码到文件的最前面,如果已经有了using代码的话请添加到已有的代码之后:注意:Leadtools.Codecs.*.dll这种引用是根据支持的图像格式命名的,请根据您的需要添加不同的格式支持。4. 在名称栏输入这个项目的名称:"OcrTutorial1",然后选择确定 ,当然如果需要的话可以重新指定一个目录来存放这个项目。5. 在"解决方案资源管理器"窗口,右键点击"引用",然后在弹出菜单中选择"添加引用"。在弹出的引用管理器对话框中,选择"框架"然后选择"浏览(B)"按钮,定位到LEADTOOLS安装目录:"\Bin\DotNet4\Win32" 然后选择如下几个DLL:

根据下面的步骤用户可创建和运行一个程序,用来展示如何在OCR文档上追加/删除和绘制识别区域。

1. 打开Visual Studio。

2. 在菜单中选择文件->新建->项目

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)(1)

3. 在新建项目对话框中,模板选择"Visual C#",然后选择Windows窗体应用程序。

4. 在名称栏输入这个项目的名称:"OcrTutorial1",然后选择确定 ,当然如果需要的话可以重新指定一个目录来存放这个项目。

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)(2)

5. 在"解决方案资源管理器"窗口,右键点击"引用",然后在弹出菜单中选择"添加引用"。在弹出的引用管理器对话框中,选择"框架"然后选择"浏览(B)"按钮,定位到LEADTOOLS安装目录:

"\Bin\DotNet4\Win32" 然后选择如下几个DLL:

  • Leadtools.dll
  • Leadtools.Drawing.dll
  • Leadtools.Codecs.dll
  • Leadtools.Controls.WinForms.dll
  • Leadtools.Forms.dll
  • Leadtools.Forms.DocumentWriters.dll
  • Leadtools.Forms.Ocr.dll
  • Leadtools.Forms.Ocr.Advantage.dll
  • Leadtools.Codecs.Bmp.dll
  • Leadtools.Codecs.Cmp.dll
  • Leadtools.Codecs.Tif.dll
  • Leadtools.Codecs.Fax.dll

注意Leadtools.Codecs.*.dll这种引用是根据支持的图像格式命名的,请根据您的需要添加不同的格式支持。

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)(3)

6. 切换到Form1的代码视图,然后添加如下代码到文件的最前面,如果已经有了using代码的话请添加到已有的代码之后:

using Leadtools; using Leadtools.Codecs; using Leadtools.Drawing; using Leadtools.Controls; using Leadtools.Forms; using Leadtools.Forms.Ocr; using Leadtools.Forms.DocumentWriters;

7. 在Form1类中添加如下的私有变量:

private ImageViewer _imageViewer; private IOcrEngine _ocrEngine; private IOcrPage _ocrPage;

8. 重写Form1的OnLoad方法,然后添加如下代码:

protected override void OnLoad(EventArgs e) { string licenseFilePath = "这里添加你的License文件路径。" string developerKey = "这里添加你的DeveloperKey"; RasterSupport.SetLicense(licenseFilePath developerKey); // 为Form添加一个ImageViewer _imageViewer = new ImageViewer; _imageViewer.Dock = DockStyle.Fill; Controls.Add(_imageViewer); _imageViewer.BringToFront; // 使用原始大小展示图片 _imageViewer.UseDpi = true; // 为鼠标添加放大/缩小图片功能,同时禁用鼠标的双击模式,因为我们要添加自己的事件 ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode; panZoomMode.DoubleTapSizeMode = ControlSizeMode.None; _imageViewer.InteractiveModes.Add(panZoomMode); // 初始化OCR引擎 _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage false); // 启动OCR引擎 _ocrEngine.Startup(null null null @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); // 从一个图片创建一个OCR页面 string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"; RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName 1); _ocrPage = _ocrEngine.CreatePage(rasterImage OcrImageSharingMode.AutoDispose); // 自动为这个图片添加识别区域 _ocrPage.AutoZone(null); // 追加一个额外的区域,这个是我们自己定义的 OcrZone zone = new OcrZone; zone.Name = "Custom zone"; zone.ZoneType = OcrZoneType.Text; zone.Bounds = new LogicalRectangle(10 10 _ocrPage.Width - 20 100 LogicalUnit.Pixel); _ocrPage.Zones.Add(zone); // 在ImageViewer中显示图片 _imageViewer.Image = _ocrPage.GetRasterImage; Text = "需要删除区域的话请右键点击任何一个即可,双击任何地方可以将识别结果保存为PDF文件。"; // 根据需要挂接一些事件 _imageViewer.PostRender = _imageViewer_PostRender; _imageViewer.MouseDown = _imageViewer_MouseDown; _imageViewer.MouseDoubleClick = _imageViewer_MouseDoubleClick; base.OnLoad(e); }

9. 重写Form1的OnFormClosed方法,然后添加如下代码:

protected override void OnFormClosed(FormClosedEventArgs e) { // 释放识别页面 _ocrPage.Dispose; // 释放识别引擎 _ocrEngine.Dispose; base.OnFormClosed(e); }

10. 添加如下代码实现ImageViewer的PostRender事件,来绘制我们的自定义区域:

private void _imageViewer_PostRender(object sender ImageViewerRenderEventArgs e) { // 绘制区域 foreach (OcrZone zone in _ocrPage.Zones) { // 取得区域边界 LogicalRectangle zoneBounds = zone.Bounds; // 该区域边界是逻辑矩形,它可能会在单位比像素以外。转换为像素 LeadRect bounds = zoneBounds.ToRectangle(_ocrPage.DpiX _ocrPage.DpiY); // 将边界转换为ImageViewer中的单位 //需要注意的是这个Demo没有旋转图片,否则你需要使用四个角点。 bounds = _imageViewer.ConvertRect(null ImageViewerCoordinateType.Image ImageViewerCoordinateType.Control bounds); // 判断是不是我们的自定义区域,如果是就将边框画为红色,否则用蓝色 if(zone.Name == "Custom zone") e.PaintEventArgs.Graphics.DrawRectangle(Pens.Red bounds.X bounds.Y bounds.Width - 1 bounds.Height - 1); else e.PaintEventArgs.Graphics.DrawRectangle(Pens.Blue bounds.X bounds.Y bounds.Width - 1 bounds.Height - 1); } }

11. 添加如下代码来实现ImageViewer的MouseDown事件,用来删除区域:

private void _imageViewer_MouseDown(object sender MouseEventArgs e) { // 判断是否是右键点击 if (e.Button != MouseButtons.Right) return; // 从控件的坐标转换为图像坐标 LeadPoint point = new LeadPoint(e.X e.Y); point = _imageViewer.ConvertPoint(null ImageViewerCoordinateType.Control ImageViewerCoordinateType.Image point); // 使用HitTestZone方法来找到鼠标当前按下时的位置 int zoneIndex = _ocrPage.HitTestZone(new LogicalPoint(point.X point.Y LogicalUnit.Pixel)); if (zoneIndex != -1) { // 移除这个区域 _ocrPage.Zones.RemoveAt(zoneIndex); // 重绘显示区域 _imageViewer.Invalidate; _imageViewer.Update; // 如果没有剩下的区域,显示一个Message if (_ocrPage.Zones.Count == 0) MessageBox.Show(this "页面上没有剩余的可辨识区域,保存为PDF选项现在不可用。"); } }

12. 最后我们实现鼠标双击保存事件,添加如下代码:

private void _imageViewer_MouseDoubleClick(object sender MouseEventArgs e) { // 检查当前页面上是否有可辨识区域 if (_ocrPage.Zones.Count == 0) { MessageBox.Show(this "页面上没有剩余的可辨识区域,保存为PDF选项现在不可用。"); return; } // 有的话开始识别 string pdfFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf"; // 如果文件存在的话试着删除掉。也有可能被其它应用程序打开。 if (System.IO.File.Exists(pdfFileName)) { try { System.IO.File.Delete(pdfFileName); } catch { MessageBox.Show(this "这个文件可能被其他程序占用,请关闭后重试。"); return; } } _ocrPage.Recognize(null);// 创建一个文档 using (IOcrDocument ocrDocument = _ocrEngine.DocumentManager.CreateDocument( null OcrCreateDocumentOptions.AutoDeleteFile)) { // 添加一个页面 ocrDocument.Pages.Add(_ocrPage); // 保存为PDF ocrDocument.Save(pdfFileName DocumentFormat.Pdf null); } // 显示这个文件 System.Diagnostics.Process.Start(pdfFileName); }

13. 保存这个项目,并运行它。

LEADTOOLSOCR文字识别教程追加和绘制识别区域(LEADTOOLSOCR文字识别教程追加和绘制识别区域)(4)

按住Ctrl可以放大缩小图片,双击图片就可以识别文字并保存为PDF,右键点击任何一个蓝色区域都可以删除识别区域。

本站文章除注明转载外,均为本站原创或翻译

猜您喜欢: