LEADTOOLS Anywhere™ 是一个令人兴奋的新特性,它将获奖的成像技术移植到了包括 WinRT、iOS、OS X、Android 和 Linux 在内的多个平台。LEADTOOLS 提供的 iOS 和 OS X 成像 SDK 技术,包含了开发者为苹果 iPhone、iPad 和 Macintosh 构建图像启用应用程序所需的一切。查看器、注释和标记、OCR、条码、PDF、图像格式、压缩、图像处理等功能,只是 LEADTOOLS 为开发者提供的一小部分。
LEADTOOLS SDK 的关键 iOS & OS X 特性包括:
在本文中,将展示如何使用 LEADTOOLS 的新 iOS 库通过 OCR 识别文本和从图像中读取条码。
获取 LEADTOOLS 图像: 在 iOS 中定义图像的对象是标准的 UIImage(或低级别的 CGImage)。可以通过多种方式在应用程序中获取图像:
Objective-C
//
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 引擎的实例:
Objective-C
//
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 文档,并将图像作为页面添加:
Objective-C
//
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.
And finally, recognize the page and get the text:
C++
// Recognize it and print the results to the console
NSString* result = [ocrPage recognizeText:nil selector:nil error:nil];
printf("\n%s\n", result.UTF8String);
LEADTOOLS 还提供了额外的低级控制,用于高级自定义处理。例如,可以获取结果字符和单词及其位置、字体属性和置信度信息:
Objective-C
//
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("\nWord: %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 条码引擎的实例:
Objective-C
//
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.
Next we set some searching options and then read the
barcode(s) from the image. It is possible to narrow down the search to
specific barcode types or areas of the image, but for this example we simply search
the entire image for any type of barcode:
//
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 对象包含有关找到的条码的信息,例如其类型、值、位置等。有了这些信息,创造力可以发挥出来,为客户生产出令人惊叹的应用程序。例如,可以对产品进行价格检查网络搜索,或者转到条码中嵌入的网页。在这个例子中,只是将条码数据打印到控制台:
Objective-C
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:@"\nFound %@ barcode at %d,%d,%d,%d\nData: %@\n",
symbology,
bounds.x, bounds.y,
bounds.x + bounds.width,
bounds.y + bounds.height,
value];
printf("%s\n", result.UTF8String);
}
else {
printf("\nNo barcode found\n");
}