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

使用 LEADTOOLS 对 Windows 商店应用程序进行 WinRT OCR

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2012年11月1日

CPOL

4分钟阅读

viewsIcon

31575

downloadIcon

496

使用 LEADTOOLS 对 Windows 商店应用程序进行 WinRT OCR

引言

Windows 8 终于发布了,LEADTOOLS 已准备就绪,通过其新的 WinRT SDK 助您快速上手。Windows 应用商店蕴藏着创建大量成像应用的机遇,而 LEADTOOLS 应有尽有:支持 150 多种格式、PDF 和 PDF/A、支持触摸屏的查看器控件、OCR、条形码、DICOM、PACS、注释、图像处理等等。借助 LEAD Technologies 领先且屡获殊荣的文档和医学成像 SDK,可以轻松创建任何 Windows 应用商店应用。

其中一个机会便是光学字符识别 (OCR)。最先进的 LEADTOOLS OCR SDK 提供原生的 WinRT 库,可在任何桌面、平板电脑或移动设备上运行。扫描文档并将其转换为可搜索的 PDF 以便存档,或者拍摄名片照片并将其添加到您的联系人中。您的想象力有多远,LEADTOOLS 就能助您到达那里。

LEADTOOLS 中的关键 WinRT 功能

  • 适用于 Win32、x64 和 ARM 的原生 WinRT 二进制文件
  • 开发面向任何 Windows 8 桌面、平板电脑或移动设备的 Windows 应用商店应用
  • 专为 WinRT 和 Windows 应用商店应用设计的图像查看器控件
    • 与 Expression Blend 兼容
    • 支持鼠标和多点触控手势输入
    • 内置交互模式,如平移、缩放、捏合缩放、放大镜等
    • 自动缩放图像以适应、适应宽度并拉伸到控件大小
  • 加载、转换和保存 150 多种图像格式
    • 对常见格式(包括 PDF、PDF/A、JPEG、JPEG 2000、TIFF、JBIG2 等)的高级位深度、色彩空间和压缩支持
  • 全面的注释和标记,包括几何形状、便笺、编辑、突出显示和图章
  • 条形码读取和写入,支持 QR、PDF417、DataMatrix、UPC/EAN 等
  • 使用窗口级和 DICOM 注释读取、写入和编辑 DICOM 数据集
  • 使用 DICOM 通信创建原生的 WinRT PACS 客户端
  • LEADTOOLS RasterImage 对象与 WinRT ImageSourceWritableBitmap 对象之间的互操作性

WinRT 的 OCR 功能

  • 快速、准确、可靠的光学字符识别,适用于任何应用程序或环境
  • 可选择多种内置和自定义词典来提高 OCR 结果
  • 识别 30 多种语言和字符集的文本,包括英语、西班牙语、法语、德语、日语、中文、阿拉伯语等
  • 自动检测文档语言
  • 整页分析和区域识别
  • 独特的彩色和二值图像识别
  • 自动文档图像清理
  • 输出可搜索文本文档格式,如 PDF、PDF/A、XPS 和 Word,保持原始外观和感觉

WinRT OCR 代码

以下示例涵盖了任何 OCR 应用的基础支柱:转换为可搜索文本格式(例如 PDF、PDF/A、DOCX、TXT 等)、整页文本识别和区域(感兴趣区域)文本识别。

我们首先初始化 LEADTOOLS OCR 引擎并准备文档。

// Create an instance of the engine
string strEngineDirectory = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"OCR");
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
_ocrEngine.Startup(null, null, string.Empty, strEngineDirectory);
 
// Create the OCR document
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument();

最后的准备步骤是加载图像并将其作为一页添加到我们的文档中。LEADTOOLS 能够处理无限数量的页面,但在此示例中,我们只添加一页。

// Show the file picker
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.ViewMode = PickerViewMode.List;
foreach (var imageFormat in _imageFormats)
   picker.FileTypeFilter.Add(imageFormat.Extension);
 
var file = await picker.PickSingleFileAsync();
if (file == null)
   return;
 
// Create a LEADTOOLS stream from the file
ILeadStream leadStream = LeadStreamFactory.Create(file);
 
// Get the RasterCodecs object to load the image from the OCR engine
RasterCodecs codecs = _ocrEngine.RasterCodecsInstance;
 
// Load the image (first page only)
RasterImage rasterImage = await codecs.LoadAsync(leadStream, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);
 
// Add it to the OCR engine
// Check if we have previous pages, remove them
_ocrDocument.Pages.Clear();
_ocrPage = _ocrDocument.Pages.AddPage(rasterImage, null);

OCR 至 PDF/A

使用 LEADTOOLS 可轻松实现扫描图像到可搜索文本格式(如 PDF、PDF/A、Word、XML 和 TXT)的转换,并且具有极高的可定制性。Recognize 函数处理文档并将识别数据内部存储为 EMF。

// Auto-zone the page
_ocrPage.AutoZone(null);

// Recognize the page
_ocrPage.Recognize(null);

识别完成后,OCR 引擎使用 DocumentWriter 类将 OCR 结果转换为任何格式。每种支持的格式都有自己的一组选项,这些选项扩展了 DocumentOptions 基类。下面我们将格式设置为 PDF/A,然后保存文档。

// Create a LEADTOOLS stream from the file
ILeadStream leadStream = LeadStreamFactory.Create(file);
 
// Set PDF output options, use PDF/A
PdfDocumentOptions options = _ocrEngine.DocumentWriterInstance.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
options.DocumentType = PdfDocumentType.PdfA;
_ocrEngine.DocumentWriterInstance.SetOptions(DocumentFormat.Pdf, options);
 
// Save the OCR'd document as searchable PDF
await _ocrDocument.SaveAsync(leadStream, DocumentFormat.Pdf, null);

OCR 至文本

使用 RecognizeText 函数将图像转换为纯文本非常简单,该函数将结果作为字符串对象返回。在我们的示例中,我们只需设置 TextBlock 对象的值以向用户显示。

// Auto-zone the page
_ocrPage.AutoZone(null);
 
// Recognize the page and get the results as text
TextResults.Text = _ocrPage.RecognizeText(null);

区域 OCR

开发人员可以使用多种方式定义页面的区域,而不是使用 LEADTOOLS 的 AutoZone 函数,包括修改现有区域、以编程方式为定义的表单字段添加区域,或者允许用户手动绘制感兴趣的区域,我们将在下面执行此操作。

LEADTOOLS RasterImageViewer 控件支持鼠标和多点触控手势输入,并提供许多有用的事件和回调。RubberBandCompleted 事件是处理此任务的理想事件。

// Set up rubber band as the interactive mode

ImageViewerRubberBandInteractiveMode rubberband = new ImageViewerRubberBandInteractiveMode();
rubberband.RubberBandCompleted += delegate(object rubberBandSender, ImageViewerRubberBandEventArgs rubberBandEventArgs)
{
   // Get the rubber band rectangle
   Rect bounds = new Rect(rubberBandEventArgs.Point1, rubberBandEventArgs.Point2);
 
   if (bounds.Width > 1 && bounds.Height > 1)
   {
      // Convert it to image coordinates
      bounds = rasterImageViewer1.ConvertRect(CoordinateType.Control, CoordinateType.Image, bounds);
 
      // Clear the zones first
      _ocrPage.Zones.Clear();
 
      // Add our bounds
      OcrZone zone = OcrTypeManager.CreateDefaultOcrZone();
      zone.ZoneType = OcrZoneType.Text;
      zone.Bounds = LeadRectHelper.Create((int)bounds.X, (int)bounds.Y, (int)bounds.Width, (int)bounds.Height);
      _ocrPage.Zones.Add(zone);
 
      // Recognize the page and get the results as text
      TextResults.Text = _ocrPage.RecognizeText(null);  
   }
};

rasterImageViewer1.DefaultInteractiveMode = rubberband;

结论

LEADTOOLS 以易于使用的、高级的编程接口为开发人员提供了世界上性能最佳、最稳定的成像库,从而能够快速开发关键业务应用程序。

其 WinRT OCR SDK 只是 LEADTOOLS 提供的众多技术之一。有关我们其他产品的更多信息,请务必访问我们的 主页,下载一个完全功能的免费评估 SDK,并在您的评估期间利用我们免费的技术支持。

下载完整的 WinRT 示例

您可以下载一个完整的演示,其中包含上述功能。要运行此示例,您需要以下内容:

支持

需要帮助来运行此示例吗?联系我们的支持团队以获得免费技术支持!有关定价或许可问题,您可以联系我们的销售团队(sales@leadtools.com)或致电 704-332-5532。

有关 LEADTOOLS WinRT 成像的更多信息

© . All rights reserved.