根据下面的步骤来创建和运行一个程序用来展示如何使用OCR识别一个图片然后得到识别结果。
1. 打开Visual Studio
2. 在菜单中选择文件->新建->项目
3. 在新建项目对话框中,模板选择"Visual C#",然后选择Windows窗体应用程序。
4. 在名称栏输入这个项目的名称:"OcrTutorial3",然后选择确定 ,当然如果需要的话可以重新指定一个目录来存放这个项目。
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这种引用是根据支持的图像格式命名的,例如BMP、TIF、FAX、JPG等,请根据您的需要添加不同的格式支持。
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的构造函数中添加如下代码:
// 请将这两个字段替换为你得到的License文件路径和Developer Key
string licenseFilePath = @"你的License文件路径";string developerKey = "你的DeveloperKey";RasterSupport.SetLicense(licenseFilePath, developerKey);
8. 在Form1类中添加如下的私有变量:
// Image viewer
private ImageViewer _imageViewer;
// OCR 引擎
private IOcrEngine _ocrEngine;
// OCR 页面
private IOcrPage _ocrPage;
// 识别结果 (带有字符位置信息)
private IOcrZoneCharacters _zoneCharacters;
9. 重写Form1的 Onload事件,然后添加如下代码:
protected override void OnLoad(EventArgs e){// 将Image Viewer添加到Form1中
_imageViewer = new ImageViewer();
_imageViewer.Dock = DockStyle.Fill;Controls.Add(_imageViewer);_imageViewer.BringToFront();// 显示图片原始大小
_imageViewer.UseDpi = true;
// 添加鼠标左键拖动图片
ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode();
panZoomMode.MouseButtons = MouseButtons.Left;_imageViewer.InteractiveModes.Add(panZoomMode);// 为右键添加括选功能
ImageViewerRubberBandInteractiveMode rubbBandMode = new ImageViewerRubberBandInteractiveMode();
rubbBandMode.MouseButtons = MouseButtons.Right;// 追加一个事件到括选结束,这样能够在结束时识别括选内容
rubbBandMode.RubberBandCompleted += rubbBandMode_RubberBandCompleted;_imageViewer.InteractiveModes.Add(rubbBandMode);// 初始化OCR引擎
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
// 启动引擎
_ocrEngine.Startup(null, null, null, @"D:\Program Files\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime");// 启用中文、英文识别
_ocrEngine.LanguageManager.EnableLanguages(new string[] { "zh-Hans", "en" });// 使用示例图片创建一个页面
string fileName = @"..\..\ocr1.jpg";RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1);_ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose);// 在Viewer中显示这个图片
_imageViewer.Image = _ocrPage.GetRasterImage();Text = "鼠标左键拖拽图片, CTRL+左键可以缩放, 右键选择识别区域";
// 添加PostRender事件,用来绘制识别括选区域
_imageViewer.PostRender += _imageViewer_PostRender;base.OnLoad(e);
}
10. 重写Form1的OnFormClosed方法,然后添加如下代码:
protected override void OnFormClosed(FormClosedEventArgs e){// 销毁页面
_ocrPage.Dispose();// 释放引擎
_ocrEngine.Dispose();base.OnFormClosed(e);
}
11. 添加如下代码实现括选区域识别功能:
private void rubbBandMode_RubberBandCompleted(object sender, ImageViewerRubberBandEventArgs e){// 使用已经绘制好的区域
// 先移除所有识别区域
_ocrPage.Zones.Clear();// 添加一个新的区域, 绘制的矩形区域是控件的坐标,我们需要将这个坐标转换为图片坐标
LeadRect bounds = LeadRect.FromLTRB(e.Points[0].X, e.Points[0].Y, e.Points[1].X, e.Points[1].Y);bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, bounds);
// 使用这个坐标来创建一个识别区域
OcrZone ocrZone = new OcrZone();
ocrZone.ZoneType = OcrZoneType.Text;ocrZone.Bounds = new LogicalRectangle(bounds);
_ocrPage.Zones.Add(ocrZone);// 进行识别,获取文字
_ocrPage.Recognize(null);
// 取得识别结果
IOcrPageCharacters pageCharacters = _ocrPage.GetRecognizedCharacters();_zoneCharacters = pageCharacters[0];// 刷新Viewer来绘制识别字符
_imageViewer.Refresh();// 使用Messagebox来显示识别结果
string text = _ocrPage.GetText(0);
if (string.IsNullOrEmpty(text))text = "[无文字]";
MessageBox.Show(this, text);
}
12. 最后,添加如下代码,将识别的文字绘制在图片上:
private void _imageViewer_PostRender(object sender, ImageViewerRenderEventArgs e){// 绘制识别结果,如果有的话
if (_zoneCharacters == null)return;
Graphics graphics = e.PaintEventArgs.Graphics;using (Brush characterBrush = new SolidBrush(Color.FromArgb(128, Color.Black))){foreach (OcrCharacter character in _zoneCharacters){// 取得字符位置
LogicalRectangle characterBounds = character.Bounds;// 字符位置是一个逻辑区域, 有可能在单位像素外,将它转换为像素
LeadRect bounds = characterBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY);// 转换这个位置到Viewer上
// 注意,这个Demo并没有旋转图片所以你需要使用四个点来定位
bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds);
// 高亮字符区域
graphics.FillRectangle(characterBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height);graphics.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);// 最后绘制字符
graphics.DrawString(new string(new char[] { character.Code }), this.Font, Brushes.White, bounds.X, bounds.Y);}}}
13. 保存然后编译执行。
教程示例源代码下载: