Leadtools.Forms.Ocr Requires Document/Medical product license | Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
OcrEngineManager Class
See Also  Members   Example 
Leadtools.Forms.Ocr Namespace : OcrEngineManager Class



Provides methods to create OCR engine instances.

Syntax

Visual Basic (Declaration) 
Public MustInherit NotInheritable Class OcrEngineManager 
Visual Basic (Usage)Copy Code
Dim instance As OcrEngineManager
C# 
public static class OcrEngineManager 
C++/CLI 
public ref class OcrEngineManager abstract sealed 

Example

This example will show how to use the IOcrEngine in a multi-threaded application. It will simultaneously convert four TIF files into PDF.

For an example on how to use the IOcrEngine in a single-threaded application, refer to IOcrEngine example.

Visual BasicCopy Code
Private Structure MyThraedData
   Public ImageFileName As String
   Public WaitHandle As AutoResetEvent
End Structure
Public Sub CreateEngineInAppDomainExample()
   ' Unlock the support needed for LEADTOOLS Plus OCR engine
   RasterSupport.Unlock(RasterSupportType.Document, "Replace with your own key here")
   RasterSupport.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here")
   RasterSupport.Unlock(RasterSupportType.OcrPlusPdfLeadOutput, "Replace with your own key here")

   ' The image file names we are going to OCR and convert to PDF
   Dim tifFileNames() As String = _
   { _
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr1.tif", _
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr2.tif", _
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr3.tif", _
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr4.tif" _
   }

   Dim threadCount As Integer = tifFileNames.Length

   ' Create the thread
   Dim threads(threadCount - 1) As Thread
   Dim waitHandles(threadCount - 1) As AutoResetEvent
   For i As Integer = 0 To threadCount - 1
      threads(i) = New Thread(AddressOf MyThreadProc)
      threads(i).Name = "OCR thread + " + i.ToString()

      waitHandles(i) = New AutoResetEvent(False)
   Next

   Console.WriteLine("Starting the threads and waiting...")

   ' Start the threads
   For i As Integer = 0 To threadCount - 1
      Dim threadData As New MyThraedData()
      threadData.ImageFileName = tifFileNames(i)
      threadData.WaitHandle = waitHandles(i)
      threads(i).Start(threadData)
   Next

   ' Wait till all threads are done
   WaitHandle.WaitAll(waitHandles)
   Console.WriteLine("All threads finished")
End Sub

Private Sub MyThreadProc(ByVal data As Object)
   ' Grab the data
   Dim threadData As MyThraedData = CType(data, MyThraedData)
   Dim imageFileName As String = threadData.ImageFileName

   ' Show a status message
   Console.WriteLine("Begin: {0}", imageFileName)

   ' Create an instance of the OCR engine using the LEADTOOLS Thunk Server
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, True)
      ' Start the engine using default parameters
      ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)

      ' Get the PDf file name
      Dim pdfFileName As String = Path.ChangeExtension(imageFileName, "pdf")

      ' Create an OCR document
      Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument()
         ' Add a page to the document
         Dim ocrPage As IOcrPage = ocrDocument.Pages.AddPage(imageFileName, Nothing)

         ' 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.Recognize(Nothing)

         ' Save the document we have as PDF
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing)
      End Using

      ' Shutdown the engine
      ' Note: calling Dispose will also automatically shutdown the engine if it has been started
      ocrEngine.Shutdown()
   End Using

   Console.WriteLine("End: {0}", imageFileName)

   ' Singal the main thread
   threadData.WaitHandle.Set()
End Sub
C#Copy Code
private struct MyThraedData 

   public string ImageFileName; 
   public AutoResetEvent WaitHandle; 

public void CreateEngineInAppDomainExample() 

   // Unlock the support needed for LEADTOOLS Plus OCR engine 
   RasterSupport.Unlock(RasterSupportType.Document, "Replace with your own key here"); 
   RasterSupport.Unlock(RasterSupportType.OcrPlus, "Replace with your own key here"); 
   RasterSupport.Unlock(RasterSupportType.OcrPlusPdfLeadOutput, "Replace with your own key here"); 
 
   // The image file names we are going to OCR and convert to PDF 
   string[] tifFileNames = 
   { 
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr1.tif", 
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr2.tif", 
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr3.tif", 
      LeadtoolsExamples.Common.ImagesPath.Path + "Ocr4.tif" 
   }; 
 
   int threadCount = tifFileNames.Length; 
 
   // Create the thread 
   Thread[] threads = new Thread[threadCount]; 
   AutoResetEvent[] waitHandles = new AutoResetEvent[threadCount]; 
   for(int i = 0; i < threadCount; i++) 
   { 
      threads[i] = new Thread(new ParameterizedThreadStart(MyThreadProc)); 
      threads[i].Name = "OCR thread + " + i.ToString(); 
 
      waitHandles[i] = new AutoResetEvent(false); 
   } 
 
   Console.WriteLine("Starting the threads and waiting..."); 
 
   // Start the threads 
   for(int i = 0; i < threadCount; i++) 
   { 
      MyThraedData threadData = new MyThraedData(); 
      threadData.ImageFileName = tifFileNames[i]; 
      threadData.WaitHandle = waitHandles[i]; 
      threads[i].Start(threadData); 
   } 
 
   // Wait till all threads are done 
   WaitHandle.WaitAll(waitHandles); 
   Console.WriteLine("All threads finished"); 

 
private void MyThreadProc(object data) 

   // Grab the data 
   MyThraedData threadData = (MyThraedData)data; 
   string imageFileName = threadData.ImageFileName; 
 
   // Show a status message 
   Console.WriteLine("Begin: {0}", imageFileName); 
 
   // Create an instance of the OCR engine using the LEADTOOLS Thunk Server 
   using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, true)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, null); 
 
      // Get the PDf file name 
      string pdfFileName = Path.ChangeExtension(imageFileName, "pdf"); 
 
      // Create an OCR document 
      using(IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
 
         // Add a page to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(imageFileName, 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.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(); 
   } 
 
   Console.WriteLine("End: {0}", imageFileName); 
 
   // Singal the main thread 
   threadData.WaitHandle.Set(); 
}

Remarks

The OcrEngineManager and its methods are your entry to using the Leadtools.Forms.Ocr class library.

This class provides the methods you need to create an object of the IOcrEngine interface. Afterwards, you can use the properties and methods of this interface to perform your OCR tasks.

Based on the engine type passed to the "CreateEngine" methods, the OcrEngineManager will load the OCR engine defined in one of the supporting assemblies and return an interface to IOcrEngine. Use this interface and its included types to start using the Leadtools.Forms.Ocr class library. For more information about the engine types, refer to OcrEngineType

The CreateEngine method lets you create an instance of IOcrEngine, loading the corresponding Leadtools.Forms.Ocr.[EngineName].dll assembly in a specific manner as follows:

  • The CreateEngine method uses the .NET 2.0 Assembly.Load method to load an assembly. You cannot unload this assembly once it has been successfully loaded.
  • If the useThunkServer parameter to CreateEngine is set to true, the internal engine DLL's are loaded using the LEADTOOLS Thunk Server. For more information, refer to LEADTOOLS OCR Thunk Server.

Your application requirements determine how to call "CreateEngine", as follows:

  • If your application is going to use a single instance of IOcrEngine; pass false to useThunkServer. This is the most common behavior.
  • If your application is going to use multiple threads for OCR, pass true to useThunkServer. The various OCR engines provided by LEADTOOLS are not thread safe. Passing true causes the LEADTOOLS Thunk Server to load the internal OCR engine in a separate process and unload it when the thread procedure terminates, ensuring thread safety and cleanup.
  • If your application is server-based, pass true to useThunkServer. Server-based applications usually use a separate thread for each "connection". Creating the OCR engine in a separate process ensures proper thread/process safety and cleanup.
  • IMPORTANT: The above discussion is for the LEADTOOLS Plus and Professional engines only (the LEADTOOLS Advantage engine supports multi-threading). To ensure thread safety and cleanup, use the LEADTOOLS Thunk Server to load the internal OCR engine in a separate process and unload it when the thread procedure terminates.
  • If your application is server-based, pass true to useThunkServer. Server-based applications usuallly use a separate thread for each "connection". Creating the OCR engine in a separate process ensures proper thread/process safety and cleanup.
  • IMPORTANT: The above discussion is for the LEADTOOLS Plus and Professional engines only. The LEADTOOLS Advantage engine supports multi-threading and x64 platform natively and does not require the use of a "thunk server".

Either way, you use the LEADTOOLS OCR class library various interfaces, classes and structures in the same manner. Your application code should not be affected by the way you created the engine.

Inheritance Hierarchy

System.Object
   Leadtools.Forms.Ocr.OcrEngineManager

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also

OcrEngineManager requires an OCR module license and unlock key. For more information, refer to: Imaging Pro/Document/Medical Features