65.9K
CodeProject 正在变化。 阅读更多。
Home

LEADTOOLS .NET OCR 编程入门 - 适用于 32 位和 64 位开发的多线程 OCR SDK

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2008 年 10 月 22 日

CPOL

6分钟阅读

viewsIcon

55431

向您介绍新的 .NET OCR 类的主要功能,为您提供创建 OCR 应用程序的分步方法,并提供示例代码。

引言

LEADTOOLS 是世界上排名第一的图像工具包,通过始终如一地以“程序员友好”的格式提供最高质量、性能和稳定性的图像组件,赢得了其顶级地位。开发人员能够显著缩短其应用程序的上市时间,从而最大限度地提高生产力并确保最大的投资回报。

LEADTOOLS 采用全新的设计,极大地简化了开发,同时又不牺牲控制权。一个重要的增强功能是可用于启用扫描图像光学字符识别 (OCR) 的高级 .NET 类集。这种新架构直观、灵活且非常易于遵循。程序员只需三行代码即可启用图像 OCR 功能,同时保持特定应用程序或工作流所需的控制级别。

在本文中,我们将向您介绍新的 .NET OCR 类的主要功能,为您提供创建 OCR 应用程序的分步方法,并提供示例代码。您可以随意下载以下链接提供的功能齐全的评估 SDK,亲身体验一下。

主要特点

LEADTOOLS 提供以下方法:

  • 识别和导出文本,可从各种文本、字处理、数据库或电子表格文件格式中进行选择。
  • 在单线程或多线程环境中执行 OCR 过程,并针对基于服务器的操作进行优化。
  • 支持多种 OCR 引擎,并通过使用通用 .NET 类库将它们从用户中抽象出来。在各种引擎之间切换几乎不需要更改应用程序代码。
  • 选择要识别的文档语言。可选择英语、丹麦语、荷兰语、芬兰语、法语、德语、意大利语、挪威语、葡萄牙语、俄语、西班牙语或瑞典语。
  • 手动或自动将复杂页面分割成文本区域、图像区域、表格区域、线条、页眉和页脚。
  • 在识别前设置准确度阈值以控制识别的准确度。
  • 学习、保存和加载相似文档的字符识别数据。该软件通过正常识别进行学习,并通过使用 OCR 的文本验证系统获取更多信息。
  • 识别 5 到 72 磅,几乎任何字体的文本。
  • 通过内置和用户词典提高识别准确度。
  • 自动检测传真、点阵打印和其他质量下降的文档并进行相应补偿。
  • 处理文本和图形。识别软件区分半色调图形和文本的能力可以为复合文档处理系统提供基础。
  • 将文档保存为 40 种格式中的任何一种,包括 Adobe PDF 和 PDF/A、MS Word、MS Excel 以及各种 ASCII 和 UNICODE 文本格式。

环境

LEADTOOLS OCR .NET 类库有 Win32 和 x64 版本,可支持以下任何环境的软件应用程序开发:

  • Windows 8(32 位和 64 位版本)
  • Windows 7(32 位和 64 位版本)
  • Windows 2008(32 位和 64 位版本)
  • Windows Vista(32 位和 64 位版本)
  • Windows XP(32 位和 64 位版本)
  • Windows 2000

提供的示例将在 Visual Studio 2005 或 Visual Studio 2008 中工作。

LEADTOOLS OCR 的工作原理

LEADTOOLS 使用 OCR 句柄与 OCR 引擎和包含页面列表的 OCR 文档进行交互。OCR 句柄是 LEADTOOLS OCR 与系统上安装的 OCR 引擎之间的通信会话。此 OCR 句柄是一个内部结构,包含识别、获取和设置信息以及文本验证所需的所有信息。

以下是识别一个或多个页面所涉及的一般步骤的概述。有关更详细的解释,请下载 LEADTOOLS 评估版并参阅 .NET 帮助中的“使用 LEADTOOLS .NET OCR 编程”主题

  1. 选择您希望使用的引擎类型并创建 IOcrEngine 接口的实例。
  2. 使用 IOcrEngine.Startup 方法启动 OCR 引擎。
  3. 建立一个包含一个或多个页面的 OCR 文档。
  4. 手动或自动在页面上建立区域。(这是可选的。页面可以带区域或不带区域进行识别。)
  5. 可选。设置 OCR 引擎要使用的活动语言。(默认为英语)。
  6. 可选。设置拼写检查语言。(默认为英语)。
  7. 可选。设置任何特殊的识别模块选项。这仅在页面包含区域(无论是自动创建还是手动创建)时才需要。
  8. 识别。
  9. 如果需要,保存识别结果。结果可以保存到文件或内存中。
  10. 完成后关闭 OCR 引擎。

其中步骤 4、5、6 和 7 几乎可以按任何顺序完成,只要它们在启动 OCR 引擎之后和识别页面之前执行。

您可以通过将对 Leadtools.Forms.Ocr.dll 程序集的引用添加到您的 .NET 应用程序中,开始在您的应用程序中使用 LEADTOOLS for .NET OCR。此程序集包含用于 LEADTOOLS OCR 编程的各种接口、类、结构和委托。

由于工具包支持多个引擎,因此与引擎交互的实际代码存储在一个单独的程序集中,一旦创建 IOcrEngine 接口的实例,该程序集将被动态加载。因此,您必须确保您计划使用的引擎程序集位于 Leadtools.Forms.Ocr.dll 程序集旁边。如果需要自动检测依赖项,您可以将引擎程序集作为引用添加到您的项目中,尽管 LEADTOOLS 不要求这样做。

代码

以下示例展示了如何在代码中执行上述步骤

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);
}

结论

LEADTOOLS 为开发人员提供了访问世界上性能最佳、最稳定的图像库的权限,并通过易于使用的高级编程接口,实现业务关键型应用程序的快速开发。新设计将简化开发工作,同时又不牺牲特定应用程序所要求的控制级别。

如上述示例所示,LEAD 的新高级 OCR 接口和设计提供了一种逻辑灵活的方法,可将扫描图像转换为可编辑和可搜索的文档。提供了类,允许您控制整个过程,或者您可以简单地启动引擎,并通过单个方法调用将 150 多种支持的图像格式中的任何一种转换为所有常见的文档格式。

OCR 是 LEADTOOLS 提供的众多功能之一。欲了解更多信息,请务必访问我们的主页并下载免费的、功能齐全的评估 SDK。

构建此示例所需的软件

LEADTOOLS 提供多种工具包、附加组件和节省成本的产品捆绑包,提供其屡获殊荣的 OCR 技术。我们推荐 Recognition ImagingDocument Imaging Suite,其中包括 Document Imaging SDK 以及所有 OCR 和可搜索 PDF 输出所需的附加组件。如需更多选项,请联系我们的销售部门。

或者,如果您想在做出购买决定之前试用,可以下载 LEADTOOLS 的免费 60 天功能齐全的评估版

支持

需要帮助启动和运行此示例吗?联系我们的支持团队以获得免费评估支持!

© . All rights reserved.