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

使用 LEADTOOLS 18 进行 iOS OCR 和条形码识别

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013年1月11日

CPOL

3分钟阅读

viewsIcon

35026

使用LEADTOOLS 18进行iOS OCR和条形码识别。

引言

LEADTOOLS,全球领先的Windows、WinRT和HTML5成像SDK,已发布18版本。18版本中最令人兴奋的新功能是Version 18,即LEADTOOLS Anywhere™,该功能是其屡获殊荣的成像技术已移植到包括WinRT、iOS、OS X、Android和Linux在内的多个平台。

LEADTOOLS中先进的iOS和OS X成像SDK技术包含了开发人员为Apple iPhone、iPad和Macintosh构建图像驱动的应用程序所需的一切。查看器、注释和标记、OCR、条形码、PDF、图像格式、压缩、图像处理等等,只是LEADTOOLS为创建日益流行的Apple平台软件的开发人员提供的众多功能中的一小部分。

LEADTOOLS SDK中的关键iOS和OS X功能

  • 面向iPhone、iPad和Mac的通用框架
    • Xcode 3.2及更高版本
    • iOS 4及更高版本
    • OS X Lion (10.7)及更高版本
  • 全面且易于使用的Objective-C类库,与.NET和Android等其他LEADTOOLS库非常相似
  • 加载、转换和保存 150 多种图像格式
    • 对常见格式(包括 PDF、PDF/A、JPEG、JPEG 2000、TIFF、JBIG2 等)的高级位深度、色彩空间和压缩支持
  • 交互式图像查看器
    • 触摸屏交互模式
    • Retina显示屏优化,带来令人惊叹的图像质量
    • 通过内置图像处理快速处理图像
    • 根据物理和逻辑单位显示图像
  • 200 多个图像处理功能,用于增强、校正和操作图像
  • 全面的注释和标记,包括几何形状、便笺、编辑、突出显示和图章
  • OCR,将图像转换为可搜索文本
  • QR、PDF417、Data Matrix、UPC/EAN 等的条形码读取和写入

iOS示例:OCR和条形码

在本文中,我们将展示如何使用LEADTOOLS的iOS新库从图像中识别文本(OCR)和读取条形码。

获取LEADTOOLS图像

在iOS中定义图像的对象是标准的UIImage(或低级的CGImage)。您可以通过多种方式在应用中获取图像:

  • 直接从应用包加载图像
  • 浏览设备的相册
  • 通过设备相机进行实时捕获

LEADTOOLS库使用LTRasterImage对象进行所有图像显示和处理。幸运的是,LEADTOOLS通过提供转换实用程序,仅需几行代码即可轻松与iOS进行互操作。

// Obtain the image from bundle, photo library or live capture
UIImage* uiImage = ...
// Convert UIImage to LTRasterImage using default options
LTRasterImage* rasterImage = [LTRasterImageConverter convertFromImage:uiImage
     options:LTConvertFromImageOptions_None
       error:nil];

现在我们已经有了图像,就可以利用LEADTOOLS提供的先进成像技术,例如OCR和条形码。

OCR示例

首先,我们需要LEADTOOLS OCR引擎的一个实例

// Create an instance of LEADTOOLS OCR engine
LTOcrEngine* ocrEngine = [LTOcrEngineManager createEngine:LTOcrEngineType_Advantage];
   
// Start up the engine with default parameters...
// We already added the OCR engine required language data files to the main bundle
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
[ocrEngine startup:nil workDirectory:nil startupParameters:bundlePath];

// Optionally, modify the settings for the OCR engine
// here (through ocrEngine.settingsManager)

接下来,我们创建一个新的OCR文档并将我们的图像添加为一页

// First create a document
LTOcrDocument* ocrDocument = [ocrEngine.documentManager createDocument];

// Add the image as a page into the document pages collection
LTOcrPage* ocrPage = [ocrDocument.pages addPageWithImage:rasterImage
   target:nil
   selector:nil
   error:nil];

// You can add manual zones (text or graphics area)
// to the page at this point through the ocrPage.zones collection.
// In this example we will let the engine auto-zone the page for us.

最后,识别页面并获取文本

// Recognize it and print the results to the console
NSString* result = [ocrPage recognizeText:nil
   selector:nil
   error:nil];
printf("%s\n", result.UTF8String);

LEADTOOLS还提供了额外的低级控件,用于高级自定义处理。例如,您可以获取结果字符和单词,以及它们的位置、字体属性和置信度信息。

// Recognize the page
[ocrPage recognize:nil selector:nil error:nil];
LTOcrPageCharacters* pageCharacters = [ocrPage getRecognizedCharacters:nil];
   
// Show the words
for (LTOcrZoneCharacters* zoneCharacters in pageCharacters)
{
   NSArray* words = [zoneCharacters getWords];
   for (LTOcrWord* word in words)
   {
      // Show its value and location
      printf("Word: %s at %d,%d,%d,%d\n",
         word.value.UTF8String,
         word.bounds.x,
         word.bounds.y,
         word.bounds.x + word.bounds.width,
         word.bounds.y + word.bounds.height);
   }
}

条形码示例

就像开始OCR一样,我们必须先创建一个LEADTOOLS条形码引擎的实例

// Create an instance of LEADTOOLS barcode engine
LTBarcodeEngine* barcodeEngine = [LTBarcodeEngine new];
   
// Get the barcode reader object
LTBarcodeReader* barcodeReader = barcodeEngine.reader;
   
// At this point, you can modify the barcode reading
// options (such as search direction, error checking, etc.)
// through the barcodeReader members. In this example we
// will leave everything as default.

接下来,我们设置一些搜索选项,然后从图像中读取条形码。可以缩小搜索范围到特定的条形码类型或图像区域,但在本示例中,我们仅搜索整个图像中的任何类型的条形码。

// Read the barcode in the image, first lets setup the options:
// The search location and size in the image, all of it
LeadRect searchBounds = LeadRect_Empty();

// Symbologies (barcode types such as UPC-A, UPC-E,
// QR, etc.) we are interested in, all of them
LTBarcodeSymbology* symbologies = nil;

// Call readBarcode
LTBarcodeData* barcodeData = [barcodeReader readBarcode:rasterImage
   searchBounds:searchBounds
   symbologies:symbologies
   symbologiesCount:0
   error:nil];

LTBarcodeData对象包含有关找到的条形码的信息,例如其类型、值、位置等。有了这些信息,您的创造力就可以尽情发挥,为客户在设备上使用的应用程序带来精彩的体验。例如,您可以对产品进行价格查询的Web搜索,或者访问嵌入在条形码中的网页。在本示例中,我们将仅将条形码数据打印到控制台。

if (barcodeData != nil)
{
   // We have a barcode
   // Get the name of the symbology (type) such as UPC-A,
   // UPC-E, QR, EAN, etc.
   NSString* symbology = [LTBarcodeEngine getSymbologyFriendlyName:barcodeData.symbology];
   // Get the location in the image
   LeadRect bounds = barcodeData.bounds;
   // Get a text representation of the data
   NSString* value = barcodeData.value;
      
   // Print the result to the console
   NSString* result = [NSString stringWithFormat:
      @"Found %@ barcode at %d,%d,%d,%d\nData: %@",
      symbology,
      bounds.x, bounds.y,
      bounds.x + bounds.width,
      bounds.y + bounds.height,
      value];
   printf("%s\n", result.UTF8String);
}
else
{
   printf("No barcode found\n");
}

下载完整的iOS和OS X示例

您可以下载一个功能齐全的演示程序,其中包含上述功能。要运行这些示例,您需要以下内容

支持

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

© . All rights reserved.