←Select platform

IOcrEngine Interface

Summary
Provides support for OCR functionality in LEADTOOLS.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public interface IOcrEngine : IDisposable 
@interface LTOcrEngine : NSObject 
public class OcrEngine 
public interface class IOcrEngine : public System.IDisposable   
class IOcrEngine(IDisposable): 
Remarks

The IOcrEngine interface is your application entry point to the OCR functionality provided by LEADTOOLS.

LEADTOOLS OCR class library uses various interfaces to perform various OCR functions. These interfaces group logically related operations and encapsulates them from the rest of the toolkit. By using interfaces, LEADTOOLS ensures that you can use an engine-independent approach when programming your OCR-based application. At any time you can switch the engine type and ensure that your program will continue to function correctly. (Providing you have used the various "GetSupported" and "IsSupported" methods when dealing with engine-specific capabilities).

Obtain an instance of IOcrEngine by calling the OcrEngineManager.CreateEngine method with the appropriate engine type.

Once an instance is obtained, use the members of the IOcrEngine to perform various OCR tasks. OCR functions are grouped into "managers". Through these managers, you can create OCR documents (and add pages to these documents), perform zoning, recognition and saving the result documents. These managers are standard .NET interfaces with the implementation hidden inside the corresponding engine assembly. The following table lists the various "managers" and their main functionality:

Member Description
DocumentManager member

Allows you to create IOcrDocument objects that encapsulate an OCR'ed document. Each IOcrDocument contains an IOcrDocument.Pages property that is an implementation of standard .NET collection of IOcrPage objects. Use this member to add, remove or update image (raster) pages in the OCR document. Pages can be image files on disk, memory or even in a remote URL. Any file format supported by LEADTOOLS (TIFF, JPEG, BMP, etc) can be loaded into the OCR document. At any time, use the various IOcrPage methods to zone the page (or pages) and recognize the objects in them in preparation to saved as a document. For more information refer to IOcrDocument, IOcrPageCollection and IOcrPage. Once you are done with adding and preparing the pages, you can use the save methods of the IOcrDocument object to save the document into its final format. LEADTOOLS supports saving to various standard document formats such as PDF, Microsoft Word, HTML and several others. For more information, refer to IOcrDocumentManager, IOcrDocument and DocumentFormat.

ZoneManager member Provides support for determining the various zone types, recognition modules and fill methods supported by this engine type. For more information, refer to IOcrZoneManager and OcrZoneType.
AutoRecognizeManager member Provides support for the one-shot "fire and forget" approach to OCR. The methods of this interface will let you create a result document from an image file on disk with optional progress and status monitors. For more information, refer to IOcrAutoRecognizeManager.
LanguageManager member Provides access to the language environment used by the OCR engine. You can use the methods and properties of this member to set the character set used by the OCR engine as well as spell correction. For more information, refer to IOcrLanguageManager.
SpellCheckManager member Allows you to enable/disable the spell checking system as well to maintain language and user dictionaries. Also lets you set up a global callback for manual word or line verification when performing a recognition operation.
SettingManager member Each OCR engine supported by LEADTOOLS has additional options and functionalities that can be accessed through this member. After setting up the engine, you can quickly save and later load the settings using the SettingManager. For more information, refer to IOcrSettingManager.

The LEADTOOLS OCR engine is generally used in the following ways:

OCR an image file (or LEADTOOLS RasterImage object) and obtain the text with optional formatting and position info. In this mode, an IOcrDocument object is not needed since the result is not going to be saved. The IOcrEngine.CreatePage method can be used to quickly create an IOcrPage from the RasterImage directly, call the necessary method (such as IOcrPage.Recognize) and then obtain the text directly using IOcrPage.GetText or IOcrPage.GetRecognizedCharacters. For an example, refer to IOcrEngine.CreatePage.

Low-level OCRing of one or more pages and creating a final document such as PDF or DOCX. In this mode, the user generally creates an IOcrDocument object (in memory or file based) and then add IOcrPage objects to it. The pages can be previously recognized or are recognized at a later time. When all the pages are added and recognized, IOcrDocument.Save is called to convert the recognition data to the final document. For an example, refer to IOcrDocument.

High-level OCRing from an input image file to a final document such as PDF or DOCX. In this mode, you can use IOcrAutoRecognizeManager to convert the document in one shot. Various events and logging mechanism can be used to modify and track the recognize operation. For an example, refer to IOcrAutoRecognizeManager.

Example

The following example will convert an image file to a PDF document.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Document.Writer; 
 
public void OcrEngineExample() 
{ 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"); 
      string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); 
 
      // Create an OCR document 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
         // Add a page to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null); 
 
         // Recognize the page 
         // Note, Recognize can be called without calling AutoZone or manually adding zones. The engine will 
         // check and automatically auto-zones the page 
         ocrPage.AutoZone(null); 
         ocrPage.Recognize(null); 
 
         // Save the document we have as PDF 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"; 
} 
 
import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.assertTrue; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void IOcrEngineExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   final String OCR_LEAD_RUNTIME_DIR = "C:\\LEADTOOLS23\\Bin\\Common\\OcrLEADRuntime"; 
   // Create an instance of the engine 
   OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
   // Start the engine using default parameters 
   String pdfFileName = combine(LEAD_VARS_IMAGES_DIR, "Ocr1.pdf"); 
   ILeadStream leadStream = LeadStreamFactory.create("C:\\LEADTOOLS23\\Resources\\Images\\Ocr1.tif"); 
 
   ocrEngine.startup(null, null, null, OCR_LEAD_RUNTIME_DIR); 
   assertTrue(ocrEngine.isStarted()); 
 
   // Create an OCR document 
   OcrDocument ocrDocument = ocrEngine.getDocumentManager().createDocument(); 
 
   // Add a page to the document 
   OcrPage ocrPage = ocrDocument.getPages().addPage(leadStream, null); 
 
   // Recognize the page 
   // Note, Recognize can be called without calling AutoZone or manually adding 
   // zones. The engine will check and automatically auto-zones the page 
   ocrPage.autoZone(null); 
   ocrPage.recognize(null); 
 
   // Save the document we have as PDF 
   ocrDocument.save(pdfFileName, DocumentFormat.PDF, null); 
 
   // Shutdown the engine 
   // Note: calling Dispose will also automatically shutdown the engine if it has 
   // been started 
   ocrEngine.shutdown(); 
   assertTrue("File unsuccessfully saved", new File(pdfFileName).exists()); 
   System.out.println("Command run, file saved to " + combine(LEAD_VARS_IMAGES_DIR, "Ocr1.pdf")); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.4.19
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.