Leadtools.Forms.Ocr Requires Document/Medical product license | Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
RecognizeText Method
See Also  Example
Leadtools.Forms.Ocr Namespace > IOcrPage Interface : RecognizeText Method



callback
Optional callback to show operation progress.
callback
Optional callback to show operation progress.
Recognizes the OCR data found on this IOcrPage and returns the result as a string.

Syntax

Visual Basic (Declaration) 
Overridable Function RecognizeText( _
   ByVal callback As OcrProgressCallback _
) As String
Visual Basic (Usage)Copy Code
Dim instance As IOcrPage
Dim callback As OcrProgressCallback
Dim value As String
 
value = instance.RecognizeText(callback)
C# 
virtual string RecognizeText( 
   OcrProgressCallback callback
)
C++/CLI 
virtual String^ RecognizeText( 
   OcrProgressCallback^ callback
) 

Parameters

callback
Optional callback to show operation progress.

Return Value

A String containing the recognized characters found (or an empty string if zones on the page contains no recognition data).

Example

This example gets the values of particular fields from a document.

Visual BasicCopy Code
Public Sub RecognizeTextExample()
   ' 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")
   ' Get the form file name
   Console.WriteLine("Setting up the form...")
   Dim formFileName As String = GetMyForm()

   ' Assume we get the field informations from an external source such as a database or an XML file
   Dim fieldNames() As String = _
   { _
      "Name", _
      "Address", _
      "SSN" _
   }

   Dim fieldBounds() As LogicalRectangle = _
   { _
      New LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel), _
      New LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel), _
      New LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel) _
   }

   Dim fieldCount As Integer = fieldNames.Length
   Dim fieldValues(fieldCount - 1) As String

   ' Create an instance of the engine
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
      ' Start the engine using default parameters
      Console.WriteLine("Starting up the engine...")
      ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)

      ' Create an OCR document
      Dim ocrDocumentManager As IOcrDocumentManager = ocrEngine.DocumentManager
      Using ocrDocument As IOcrDocument = ocrDocumentManager.CreateDocument()

         ' Add the form to the document
         Console.WriteLine("Adding the form to the document...")
         Dim ocrPage As IOcrPage = ocrDocument.Pages.AddPage(formFileName, Nothing)

         ' Get our fields
         For i As Integer = 0 To fieldCount - 1
            ' Clear all the zones in the page
            ocrPage.Zones.Clear()

            ' Add our field zone
            Dim ocrZone As New OcrZone()
            ocrZone.ZoneType = OcrZoneType.Text
            ocrZone.Bounds = fieldBounds(i)
            ocrZone.Name = fieldNames(i) ' Optional

            Console.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name)
            ocrPage.Zones.Add(ocrZone)

            ' Recognize the page. This will only recognize the zone we added
            Console.WriteLine("Recognizing the page...")
            fieldValues(i) = ocrPage.RecognizeText(Nothing)
         Next

      End Using

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

   ' We are done, show the fields
   Console.WriteLine("-------------------------------------")
   Console.WriteLine("Done, values extracted from the form:")
   Console.WriteLine("-------------------------------------")
   For i As Integer = 0 To fieldCount - 1
      Console.WriteLine("{0} : {1}", fieldNames(i), fieldValues(i))
   Next
End Sub

Private Function GetMyForm() As String
   Dim formFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "MyForm.tif"

   ' In this example we will create the form every time
   ' This will be a TIF file 11 by 8.5 inches in size containing a name, address and social security fields
   Using image As RasterImage = New RasterImage( _
         RasterMemoryFlags.Conventional, _
         2544, _
         3294, _
         24, _
         RasterByteOrder.Bgr, _
         RasterViewPerspective.BottomLeft, _
         Nothing, _
         IntPtr.Zero, _
         0)
      image.XResolution = 300
      image.YResolution = 300

      ' Draw our fields into this image
      Dim hdc As IntPtr = image.CreateLeadDC()
      Try
         Using g As Graphics = Graphics.FromHdc(hdc)
            g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1)

            Using f As New Font("Times New Roman", 80, FontStyle.Regular)
               Using p As New Pen(Color.Black, 4)
                  Using sf As New StringFormat()
                     sf.LineAlignment = StringAlignment.Center
                     ' Draw the fields

                     ' Name
                     g.DrawString("Name:", f, Brushes.Black, 200, 200)
                     Dim rc As New Rectangle(800, 160, 1500, 220)
                     g.DrawRectangle(p, rc)
                     Dim value As String = "John Doe"
                     g.DrawString(value, f, Brushes.Black, rc, sf)

                     ' Address
                     g.DrawString("Address:", f, Brushes.Black, 200, 600)
                     rc = New Rectangle(800, 560, 1500, 220)
                     g.DrawRectangle(p, rc)
                     value = "1234 Main Street, USA"
                     g.DrawString(value, f, Brushes.Black, rc, sf)

                     ' Social security number
                     g.DrawString("SSN:", f, Brushes.Black, 200, 1000)
                     rc = New Rectangle(800, 960, 1500, 220)
                     g.DrawRectangle(p, rc)
                     value = "123-45-6789"
                     g.DrawString(value, f, Brushes.Black, rc, sf)
                  End Using
               End Using
            End Using
         End Using
      Finally
         RasterImage.DeleteLeadDC(hdc)
      End Try

      ' Save this image to disk
      RasterCodecs.Startup()
      Using codecs As New RasterCodecs()
         codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1)
      End Using
      RasterCodecs.Shutdown()
   End Using

   Return formFileName
End Function
C#Copy Code
public void RecognizeTextExample() 

   // 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"); 
   // Get the form file name 
   Console.WriteLine("Setting up the form..."); 
   string formFileName = GetMyForm(); 
 
   // Assume we get the field informations from an external source such as a database or an XML file 
   string[] fieldNames = 
   { 
      "Name", 
      "Address", 
      "SSN" 
   }; 
 
   LogicalRectangle[] fieldBounds = 
   { 
      new LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel), 
      new LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel), 
      new LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel) 
   }; 
 
   int fieldCount = fieldNames.Length; 
   string[] fieldValues = new string[fieldCount]; 
 
   // Create an instance of the engine 
   using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false)) 
   { 
      // Start the engine using default parameters 
      Console.WriteLine("Starting up the engine..."); 
      ocrEngine.Startup(null, null, null, null); 
 
      // Create a document 
      IOcrDocumentManager ocrDocumentManager = ocrEngine.DocumentManager; 
      using(IOcrDocument ocrDocument = ocrDocumentManager.CreateDocument()) 
      { 
         // Add the form to the document 
         Console.WriteLine("Adding the form to the document..."); 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(formFileName, null); 
 
         // Get our fields 
         for(int i = 0; i < fieldCount; i++) 
         { 
            // Clear all the zones in the page 
            ocrPage.Zones.Clear(); 
 
            // Add our field zone 
            OcrZone ocrZone = new OcrZone(); 
            ocrZone.ZoneType = OcrZoneType.Text; 
            ocrZone.Bounds = fieldBounds[i]; 
            ocrZone.Name = fieldNames[i]; // Optional 
 
            Console.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name); 
            ocrPage.Zones.Add(ocrZone); 
 
            // Recognize the page. This will only recognize the zone we added 
            Console.WriteLine("Recognizing the page..."); 
            fieldValues[i] = ocrPage.RecognizeText(null); 
         } 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      Console.WriteLine("Shutting down..."); 
      ocrEngine.Shutdown(); 
   } 
 
   // We are done, show the fields 
   Console.WriteLine("-------------------------------------"); 
   Console.WriteLine("Done, values extracted from the form:"); 
   Console.WriteLine("-------------------------------------"); 
   for(int i =0; i < fieldCount; i++) 
      Console.WriteLine("{0} : {1}", fieldNames[i], fieldValues[i]); 

 
private string GetMyForm() 

   string formFileName = LeadtoolsExamples.Common.ImagesPath.Path + "MyForm.tif"; 
 
   // In this example we will create the form every time 
   // This will be a TIF file 11 by 8.5 inches in size containing a name, address and social security fields 
   using(RasterImage image = new RasterImage( 
      RasterMemoryFlags.Conventional, 
      2544, 
      3294, 
      24, 
      RasterByteOrder.Bgr, 
      RasterViewPerspective.BottomLeft, 
      null, 
      IntPtr.Zero, 
      0)) 
   { 
      image.XResolution = 300; 
      image.YResolution = 300; 
 
      // Draw our fields into this image 
      IntPtr hdc = image.CreateLeadDC(); 
      try 
      { 
         using(Graphics g = Graphics.FromHdc(hdc)) 
         { 
            g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1); 
 
            using(Font f = new Font("Times New Roman", 80, FontStyle.Regular)) 
            { 
               using(Pen p = new Pen(Color.Black, 4)) 
               { 
                  using(StringFormat sf = new StringFormat()) 
                  { 
                     sf.LineAlignment = StringAlignment.Center; 
                     // Draw the fields 
 
                     // Name 
                     g.DrawString("Name:", f, Brushes.Black, 200, 200); 
                     Rectangle rc = new Rectangle(800, 160, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     string value = "John Doe"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
 
                     // Address 
                     g.DrawString("Address:", f, Brushes.Black, 200, 600); 
                     rc = new Rectangle(800, 560, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     value = "1234 Main Street, USA"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
 
                     // Social security number 
                     g.DrawString("SSN:", f, Brushes.Black, 200, 1000); 
                     rc = new Rectangle(800, 960, 1500, 220); 
                     g.DrawRectangle(p, rc); 
                     value = "123-45-6789"; 
                     g.DrawString(value, f, Brushes.Black, rc, sf); 
                  } 
               } 
            } 
         } 
      } 
      finally 
      { 
         RasterImage.DeleteLeadDC(hdc); 
      } 
 
      // Save this image to disk 
      RasterCodecs.Startup(); 
      using(RasterCodecs codecs = new RasterCodecs()) 
      { 
         codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1); 
      } 
      RasterCodecs.Shutdown(); 
   } 
 
   return formFileName; 
}

Remarks

Before calling this method call AutoPreprocess Method to perform automatic pre-processing to improve image quality.

Use this method to get the document result in a simple String object. Getting the result as text is helpful in situations when adding zones manually for form processing. For example, suppose the form you are processing has two areas of interests, a name field at coordinates 100, 100, 400, 120 and a social security number at coordinates 100, 200, 400, 220. You can structure your application as follows:

  1. Create a new IOcrDocument object D:\LEAD15\Help\DotNet\Projects\Leadtools.Forms.Ocr\Exceptions\OcrException.cs
  2. Add the page to the OCR document using IOcrDocument.Pages
  3. Add the name zone manually:

    
    OcrZone nameZone = new OcrZone();
    nameZone.ZoneType = OcrZoneType.Text;
    nameZone.Bounds = new LogicalRectangle(100, 100, 400, 120);
    ocrPage.Zones.Add(nameZone);
    

  4. Recognize the page and get the value of the name field (only this one zone will recognized):

    
    string name = ocrPage.RecognizeText(null);
    

  5. Remove the name zone from the page:

    
    ocrPage.Zones.Clear();
    

  6. Repeat the steps from (2) above to get the social security field.

If this page is not a black/white one (i.e. it contains a gray-scale or a 24-bit color image), then an implicit secondary image conversion step will be performed automatically to convert the image to a B/W one.

RecognizeText utilizes the zone information to activate the appropriate recognition module on every zone Zones property. Each recognition module recognizes the page parts assigned to it in the zones.

If the zone collection Zones of this IOcrPage is empty (i.e. there are no zones defined), then the page-layout decomposition process will be activated automatically in order to create a zone list for the image, before recognition. Hence, AutoZone will be implicitly called.

Note: If this IOcrPage is an empty page, in other words, when the OCR engine performs automatic page decomposing with the AutoZone method and could not find any zones in it, the RecognizeText method will fail with an exception. It is recommended you call AutoZone and then check if there is at least one zone found by the engine (using Zones.Count). If the count is zero, do not call RecognizeText.

If a recognition module is not able to recognize an object (i.e. character, or checkmark etc.), this object will be marked as a rejected one. It will become marked by a rejection symbol during conversion to the final output document. Note that IOcrDocumentManager.RejectionSymbol can be set to specify the rejection symbol used in the final document.

This method uses the checking subsystem (IOcrSpellCheckManager) to either flag suspicious characters or words, or to allow auto-correction during the recognition process.

You can use the OcrProgressCallback to show the operation progress or to abort it. For more information and an example, refer to OcrProgressCallback.

Since the format of the recognized data file is not documented, you can use GetRecognizedCharacters and SetRecognizedCharacters to examine or modify the data. Any changes you make to the recognition data will be saved in the resulting document when you save IOcrDocument.

After the page is successfully recognized, the value of the IsRecognized property should be true.

Use Unrecognize to clear the recognition data stored in a page.

Use IOcrPage.Recognize to keep the recognition data stored internally inside the page. You can later use the methods of the IOcrDocument object that owns this page or pages to save the data to a file or memory using the many formats supported by this IOcrEngine such as Text, PDF or Microsoft Word.

Since the recognition algorithm may use the checking subsystem, you must set up the IOcrSpellCheckManager prior to calling RecognizeText. Checking recognized zone contents may consist of any combination of the following:

To get the accuracy and timing data of the latest successful recognition process use IOcrEngine.GetLastStatistic after calling IOcrPage.Recognize.

Requirements

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

See Also

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