Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

iOS OCR and Barcode with LEADTOOLS 18

11 Jan 2013 1  
iOS OCR and Barcode with LEADTOOLS 18.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

LEADTOOLS, the world’s leading imaging SDK for Windows, WinRT and HTML5, has released Version 18. The most exciting new feature in Version 18 is LEADTOOLS Anywhere™, which is its award-winning imaging technology ported to multiple platforms including WinRT, iOS, OS X, Android and Linux.

The advanced iOS and OS X imaging SDK technology in LEADTOOLS includes everything developers need to build image-enabled applications for Apple iPhone, iPad and Macintosh. Viewers, annotations and markup, OCR, barcode, PDF, image formats, compression, image processing and more are just a sampling of what LEADTOOLS has to offer developers creating software for the increasingly popular Apple platforms.

Key iOS & OS X Features in LEADTOOLS SDKs

  • Universal frameworks targeting iPhone, iPad and Mac
    • Xcode 3.2 and later
    • iOS 4 and later
    • OS X Lion (10.7) and later
  • Comprehensive and easy to use Objective-C class library that closely resembles other LEADTOOLS libraries such as .NET and Android
  • Load, convert and save more than 150 image formats
    • Advanced bit depth, color space and compression support for common formats including PDF, PDF/A, JPEG, JPEG 2000, TIFF, JBIG2 and more
  • Interactive image viewers
    • Touch screen enabled interactive modes
    • Retina display optimizations for incredible image quality
    • Quickly process images using built-in image manipulation
    • Display images based on physical and logical units
  • Over 200 Image processing functions for enhancing, correcting and manipulating images
  • Comprehensive Annotation and Markup including geometric shapes, sticky note, redact, highlight and rubber stamp
  • OCR to convert images to searchable text
  • Barcode reading and writing for QR, PDF417, Data Matrix, UPC/EAN and more

An iOS Example: OCR and Barcode

In this article, we’ll show how to use LEADTOOLS’ new iOS libraries to recognize text with OCR and read barcodes from an image.

Obtaining a LEADTOOLS Image

The object that defines an image in iOS is the standard UIImage (or the low level CGImage). You can obtain an image in your app through a variety of ways:

  • Load the image directly from the app bundle
  • Browse the device’s photo library
  • Live capture through the device’s camera

LEADTOOLS libraries use the LTRasterImage object for all of its image display and processing. Fortunately, LEADTOOLS makes it easy to interoperate with iOS by providing its conversion utilities which only require a couple lines of code:

// 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];

Now that we have an image, we can utilize the advanced imaging technology offered by LEADTOOLS, such as OCR and Barcode.

OCR Example

First, we need an instance of the LEADTOOLS OCR engine:

// 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)

Next, we create a new OCR document and add our image as a page:

// 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:

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

LEADTOOLS also provides additional low level control for advanced custom processing. For example, you can obtain the resulting characters and words along with their location, font properties and confidence level information:

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

Barcode Example

Just as we started for OCR, we must first create an instance of the LEADTOOLS Barcode engine:

// 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];

The LTBarcodeData object contains information about the barcode found, such its type, value, location and more. Armed with this information, your creativity can take off to produce spectacular apps for customers to use on their devices. For example, you can perform a price-checking web search on the product, or go to the web page embedded in the barcode. In this example, we will just print the barcode data to the console:

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

Download the Full iOS and OS X Examples

You can download a fully functional demo which includes the features discussed above. To run these examples you will need the following:

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here