LEADTOOLS v18的全新设计极大地简化了开发而无需牺牲控制力。LEADTOOLS v18增强了其用于光学字符识别的.Net类。LEADTOOLS的架构灵活、直观而且易于使用。开发人员仅通过三行代码就可以实现图像的OCR功能。本文接下来将着重介绍全新的LEADTOOLS .NET OCR 类,以及展示如何创建一个OCR程序。
LEADTOOLS OCR .NET类库提供了Win32 和 x64 版本,适用于下列开发环境:
LEADTOOLS使用OCR手柄连接OCR引擎和包含在页面列表中的OCR文档。OCR手柄是LEADTOOLS OCR和OCR引擎之间的通信会话。OCR手柄是一个包含了识别、获取/设置信息以及文本验证的所有必要信息的内部结构。
以下是识别单页或者多文档所涉及到的基本步骤:
1、选择你想使用的引擎类型并创建一个IOcrEngine接口实例。
2、根据IOcrEngine.Startup方法启动OCR引擎。
3、创建一个OCR文档。
4、手动或者自动创建页面区域。(该步骤为可选步骤)
5、设置OCR引擎所使用的活动语言。(默认为英语)
6、设置拼写语言。(默认为英语)
7、设置任意特殊识别模块选项。如果页面包含区域,该步骤是必须的。
8、识别。
9、如果需要,可以保存识别结果。可将识别结果保存到一个文件或者内存中。
10、关闭OCR引擎。
只要步骤4、5、6和7在启动OCR引擎后和完成识别前进行都可以,而且可以不按照顺序进行。
添加引用到.Net程序的Leadtools.Forms.Ocr.dll 程序集中,以启动LEADTOOLS for .NET OCR。该程序集包含了各种接口、类和结构。由于LEADTOOLS图像开发包提供了多引擎支持,因此 链接引擎的实际代码被储存在一个单独的程序集中。因此,必须确保你打算用的引擎程序集位于Leadtools.Forms.Ocr.dll 程序集中。如果需要自动检测依赖性,你可以将引擎程序集作为引用添加到项目中。
下列代码演示了如何执行上次步骤:
Visual Basic
' *** Step 1: Select the engine type and
' create an instance of the IOcrEngine interface.
' We will use the LEADTOOLS OCR Plus engine and use it in the same process
Dim ocrEngine As IOcrEngine = _
OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
' *** Step 2: Startup the engine.
' Use the default parameters
ocrEngine.Startup(Nothing, Nothing, Nothing)
' *** Step 3: Create an OCR document with one or more pages.
Dim ocrDocument As IOcrDocument = _
ocrEngine.DocumentManager.CreateDocument()
' Add all the pages of a multi-page TIF image to the document
ocrDocument.Pages.AddPages("C:\Images\Ocr.tif", 1, -1, Nothing)
' *** Step 4: Establish zones on the page(s), either manually or automatically
' Automatic zoning
ocrDocument.Pages.AutoZone(Nothing)
' *** Step 5: (Optional) Set the active languages to be used by the OCR engine
' Enable English and German languages
ocrEngine.LanguageManager.EnableLanguages(New String() {"en", "de"})
' *** Step 6: (Optional) Set the spell checking language
' Enable the spell checking system and set English as the spell language
ocrEngine.SpellCheckManager.Enabled = True
ocrEngine.SpellCheckManager.SpellLanguage = "en"
' *** Step 7: (Optional) Set any special recognition module options
' Change the fill method for the first zone in the first page to be Omr
Dim ocrZone As OcrZone = ocrDocument.Pages(0).Zones(0)
ocrZone.FillMethod = OcrZoneFillMethod.Omr
ocrDocument.Pages(0).Zones(0) = ocrZone
' *** Step 8: Recognize
ocrDocument.Pages.Recognize(Nothing)
' *** Step 9: Save recognition results
' Save the results to a PDF file
ocrDocument.Save("C:\\Images\Document.pdf", OcrDocumentFormat.PdfA, Nothing)
ocrDocument.Dispose()
' *** Step 10: Shut down the OCR engine when finished
ocrEngine.Shutdown()
ocrEngine.Dispose()
C#
// *** Step 1: Select the engine type and
// create an instance of the IOcrEngine interface.
// We will use the LEADTOOLS OCR Plus engine and use it in the same process
IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false);
// *** Step 2: Startup the engine.
// Use the default parameters
ocrEngine.Startup(null, null, null);
// *** Step 3: Create an OCR document with one or more pages.
IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument();
// Add all the pages of a multi-page TIF image to the document
ocrDocument.Pages.AddPages(@"C:\Images\Ocr.tif", 1, -1, null);
// *** Step 4: Establish zones on the page(s), either manually or automatically
// Automatic zoning
ocrDocument.Pages.AutoZone(null);
// *** Step 5: (Optional) Set the active languages to be used by the OCR engine
// Enable English and German languages
ocrEngine.LanguageManager.EnableLanguages(new string[] { "en", "de"});
// *** Step 6: (Optional) Set the spell checking language
// Enable the spell checking system and set English as the spell language
ocrEngine.SpellCheckManager.Enabled = true;
ocrEngine.SpellCheckManager.SpellLanguage = "en";
// *** Step 7: (Optional) Set any special recognition module options
// Change the fill method for the first zone in the first page to be default
OcrZone ocrZone = ocrDocument.Pages[0].Zones[0];
ocrZone.FillMethod = OcrZoneFillMethod.Default;
ocrDocument.Pages[0].Zones[0] = ocrZone;
// *** Step 8: Recognize
ocrDocument.Pages.Recognize(null);
// *** Step 9: Save recognition results
// Save the results to a PDF file
ocrDocument.Save(@"C:\Images\Document.pdf", OcrDocumentFormat.PdfA, null);
ocrDocument.Dispose();
// *** Step 10: Shut down the OCR engine when finished
ocrEngine.Shutdown();
ocrEngine.Dispose();
接下来的示例展示了如何利用IOcrAutoRecognizeManager执行相同任务,
Visual Basic
' Create the engine instance
Using ocrEngine As IOcrEngine = _
OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
' Startup the engine
ocrEngine.Startup(Nothing, Nothing, Nothing)
' Convert the multi-page TIF image to a PDF document
ocrEngine.AutoRecognizeManager.Run( _
"C:\Images\Ocr.tif", _
"C:\Images\Document.pdf", _
Nothing, _
OcrDocumentFormat.PdfA, _
Nothing)
End Using
C#
// Create the engine instance
using (IOcrEngine ocrEngine =
OcrEngineManager.CreateEngine(OcrEngineType.Plus, false))
{
// Startup the engine
ocrEngine.Startup(null, null, null);
// Convert the multi-page TIF image to a PDF document
ocrEngine.AutoRecognizeManager.Run(
@"C:\Images\Ocr.tif",
@"C:\Images\Document.pdf",
null,
OcrDocumentFormat.PdfA,
null);
}