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

WinRT OCR for Windows Store Apps with LEADTOOLS

7 Jan 2013 1  
WinRT OCR for Windows Store Apps with LEADTOOLS

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

Windows 8 is finally here, and LEADTOOLS is ready to help you hit the ground running with its new WinRT SDK. The Windows Store is ripe with opportunities to create a multitude of imaging apps and LEADTOOLS has it all: support for 150+ formats, PDF and PDF/A, touch screen enabled viewer controls, OCR, Barcode, DICOM, PACS, annotations, image processing and more. Any Windows Store application can easily be created with the help of LEAD Technologies’ advanced and award-winning Document and Medical imaging SDKs.

One such opportunity is with Optical Character Recognition. The state of the art LEADTOOLS OCR SDK provides native WinRT libraries that can run on any desktop, tablet or mobile device. Scan and convert a document to searchable PDF for archival, or snap a picture of a business card and add it to your contacts. Whatever your mind can dream up, LEADTOOLS can help you get there.

Key WinRT Features in LEADTOOLS 

  • Native WinRT binaries for Win32, x64 and ARM
  • Develop Windows Store applications that target any Windows 8 desktop, tablet or mobile device
  • Image viewer controls designed specifically for WinRT and Windows Store apps
    • Compatible with Expression Blend
    • Supports both mouse and multi-touch gesture input
    • Built-in interactive modes such as pan, scale, pinch and zoom, magnifying glass and more
    • Automatically scale images to fit, fit width and stretch to the control size
  • 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
  • Comprehensive Annotation and Markup including geometric shapes, sticky note, redact, highlight and rubber stamp
  • Barcode reading and writing for QR, PDF417, DataMatrix, UPC/EAN and more
  • Read, write and edit DICOM Data Sets with Window Leveling and DICOM Annotations
  • Use DICOM Communication to create native WinRT PACS clients
  • Interoperability between the LEADTOOLS RasterImage object and WinRT ImageSource or WritableBitmap objects

OCR Features for WinRT

  • Fast, accurate and reliable optical character recognition for use in any application or environment
  • Choose from several built-in and custom dictionaries to improve OCR results
  • Recognize text from over 30 languages and character sets including English, Spanish, French, German, Japanese, Chinese, Arabic and more
  • Automatically detect the document's language
  • Full page analysis and Zonal recognition
  • Unique color and bitonal image recognition
  • Automated document image cleanup
  • Output searchable text document formats such as PDF, PDF/A, XPS and Word, maintaining the original look and feel

The WinRT OCR Code

The following example covers the foundational pillars of any OCR application: conversion to searchable text formats (e.g. PDF, PDF/A, DOCX, TXT, etc.), full page text recognition, and zonal (region of interest) text recognition.

We begin by initializing the LEADTOOLS OCR engine and preparing a document.

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

The final preparation step is to load an image and add it as a page to our document. LEADTOOLS is capable of processing an infinite number of pages, but for this example we will only add a single page.

// 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 to PDF/A

Converting scanned images to searchable text formats such as PDF, PDF/A, Word, XML and TXT is both easy to implement and extremely customizable with LEADTOOLS. The Recognize function processes the document and stores the recognition data internally as EMF.

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

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

After the recognition is complete, the OCR engine uses the DocumentWriter class to convert the OCR results to any format. Each supported format comes with its own set of options that extends the DocumentOptions base class. Below we set the format to PDF/A and then save the document.

// 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 to Text

Converting an image to raw text couldn’t be any easier with the RecognizeText function which returns the results as a string object. In our example, we simply set the value of a TextBlock object to display it to the user.

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

Zonal OCR

Instead of using LEADTOOLS’ AutoZone function, developers can define the page’s zones in any number of ways including modifying existing zones, programmatically adding zones for defined form fields, or allowing the user to manually draw a region of interest which we will do here. 

The LEADTOOLS RasterImageViewer control supports both mouse and multi-touch gesture input and provides many useful events and callbacks. The RubberBandCompleted event is the perfect event to handle this task.

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

Conclusion

LEADTOOLS provides developers with access to the world’s best performing and most stable imaging libraries in an easy-to-use, high-level programming interface enabling rapid development of business-critical applications.

Its WinRT OCR SDK is only one of the many technologies LEADTOOLS has to offer. For more information on our other products, be sure to visit our home page, download a free fully functioning evaluation SDK, and take advantage of our free technical support during your evaluation.

Download the Full WinRT Example

You can download a fully functional demo which includes the features discussed above. To run this example 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.

For More Information on WinRT Imaging with LEADTOOLS

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