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



callback
Optional callback to show operation progress.
callback
Optional callback to show operation progress.
Performs auto decomposition of the page to find the text and graphics zones using predefined parameters

Syntax

Visual Basic (Declaration) 
Overloads Overridable Sub AutoZone( _
   ByVal callback As OcrProgressCallback _
) 
Visual Basic (Usage)Copy Code
Dim instance As IOcrPage
Dim callback As OcrProgressCallback
 
instance.AutoZone(callback)
C# 
virtual void AutoZone( 
   OcrProgressCallback callback
)
C++/CLI 
virtual void AutoZone( 
   OcrProgressCallback^ callback
) 

Parameters

callback
Optional callback to show operation progress.

Example

This example will perform auto-zoning on an OCR page then displays information about the zones found.

Visual BasicCopy Code
Public Sub OcrAutoZoneExample()
   ' 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")
   ' Create an image with some text in it
   Dim image As New RasterImage(RasterMemoryFlags.Conventional, 320, 200, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, IntPtr.Zero, 0)
   Dim imageRect As New Rectangle(0, 0, image.ImageWidth, image.ImageHeight)

   Dim hdc As IntPtr = image.CreateLeadDC()
   Using g As Graphics = Graphics.FromHdc(hdc)
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
      g.FillRectangle(Brushes.White, imageRect)

      Using f As New Font("Arial", 20, FontStyle.Regular)
         g.DrawString("Normal line", f, Brushes.Black, 0, 0)
      End Using

      Using f As New Font("Courier New", 20, FontStyle.Regular)
         g.DrawString("Monospaced line", f, Brushes.Black, 0, 80)
      End Using
   End Using

   RasterImage.DeleteLeadDC(hdc)

   Dim zonesFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "MyZones.xml"

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

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

         ' Add this image to the document
         Dim ocrPage As IOcrPage = ocrDocument.Pages.AddPage(image, Nothing)
         image.Dispose()

         ' Show the zones, there should be no zones yet
         ShowZones("Right after the page was created", ocrPage)

         ' Perform default AutoZoning on the page
         ocrPage.AutoZone(Nothing)

         ' Show the zones, there should be two zones, one for each line
         ShowZones("AutoZone with default parameters", ocrPage)

         ' Upda the fill methods of the zones
         ocrPage.UpdateFillMethod()

         ' Show the zones after the fill method has been updated
         ShowZones("After updating the fill methods", ocrPage)

         ' Update the first zone manually
         Dim ocrZone As OcrZone = ocrPage.Zones(0)
         ocrZone.ZoneType = OcrZoneType.Text
         ocrPage.Zones(0) = ocrZone

         ' Show the zones
         ShowZones("After updating the type of the first zone", ocrPage)

         ' Save the zones to a file and then clear them
         ocrPage.SaveZones(zonesFileName)
         ocrPage.Zones.Clear()

         ' Show the zones, there should be no zones since we just cleared them
         ShowZones("After calling save and clear", ocrPage)

         ' Re-load the zones
         ocrPage.LoadZones(zonesFileName)
         ShowZones("After re-loading the zones", ocrPage)

         ' To do manual auto zoning, it is helpful to first get the fill method of the page
         Dim fillMethod As OcrZoneFillMethod = ocrPage.DetectFillMethod()
         Console.WriteLine("Fill method detected: {0}", fillMethod)

         ' Clear the zones and do auto zone with specific options
         ocrPage.AutoZone(OcrZoneParser.Default, fillMethod, LogicalUnit.Inch, Nothing)
         ShowZones("After AutoZone with specific options, units should be in inches", ocrPage)
      End Using

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

Private Sub ShowZones(ByVal message As String, ByVal ocrPage As IOcrPage)
   Console.WriteLine("Zones after {0}:", message)
   For Each ocrZone As OcrZone In ocrPage.Zones
      Dim index As Integer = ocrPage.Zones.IndexOf(ocrZone)
      Console.WriteLine("Zone index: {0}", index)
      Console.WriteLine(" Id {0}", ocrZone.Id)
      Console.WriteLine(" Bounds {0}", ocrZone.Bounds)
      Console.WriteLine(" ZoneType {0}", ocrZone.ZoneType)
      Console.WriteLine(" FillMethod: {0}", ocrZone.FillMethod)
      Console.WriteLine(" RecognitionModule: {0}", ocrZone.RecognitionModule)
      Console.WriteLine(" CharacterFilters: {0}", ocrZone.CharacterFilters)
      Console.WriteLine("----------------------------------")
   Next

   Console.WriteLine("Hit enter to continue")
   Console.ReadLine()
End Sub
C#Copy Code
public void OcrAutoZoneExample() 

   // 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"); 
   // Create an image with some text in it 
   RasterImage image = new RasterImage(RasterMemoryFlags.Conventional, 320, 200, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0); 
   Rectangle imageRect = new Rectangle(0, 0, image.ImageWidth, image.ImageHeight); 
 
   IntPtr hdc = image.CreateLeadDC(); 
   using(Graphics g = Graphics.FromHdc(hdc)) 
   { 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      g.FillRectangle(Brushes.White, imageRect); 
 
      using(Font f = new Font("Arial", 20, FontStyle.Regular)) 
         g.DrawString("Normal line", f, Brushes.Black, 0, 0); 
 
      using(Font f = new Font("Courier New", 20, FontStyle.Regular)) 
         g.DrawString("Monospaced line", f, Brushes.Black, 0, 80); 
   } 
 
   RasterImage.DeleteLeadDC(hdc); 
 
   string zonesFileName = LeadtoolsExamples.Common.ImagesPath.Path + "MyZones.xml"; 
 
   // Create an instance of the engine 
   using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, null); 
 
      // Create an OCR document 
      using(IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
         // Add this image to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(image, null); 
         image.Dispose(); 
 
         // Show the zones, there should be no zones yet 
         ShowZones("Right after the page was created", ocrPage); 
 
         // Perform default AutoZoning on the page 
         ocrPage.AutoZone(null); 
 
         // Show the zones, there should be two zones, one for each line 
         ShowZones("AutoZone with default parameters", ocrPage); 
 
         // Upda the fill methods of the zones 
         ocrPage.UpdateFillMethod(); 
 
         // Show the zones after the fill method has been updated 
         ShowZones("After updating the fill methods", ocrPage); 
 
         // Update the first zone manually 
         OcrZone ocrZone = ocrPage.Zones[0]; 
         ocrZone.ZoneType = OcrZoneType.Text; 
         ocrPage.Zones[0] = ocrZone; 
 
         // Show the zones 
         ShowZones("After updating the type of the first zone", ocrPage); 
 
         // Save the zones to a file and then clear them 
         ocrPage.SaveZones(zonesFileName); 
         ocrPage.Zones.Clear(); 
 
         // Show the zones, there should be no zones since we just cleared them 
         ShowZones("After calling save and clear", ocrPage); 
 
         // Re-load the zones 
         ocrPage.LoadZones(zonesFileName); 
         ShowZones("After re-loading the zones", ocrPage); 
 
         // To do manual auto zoning, it is helpful to first get the fill method of the page 
         OcrZoneFillMethod fillMethod = ocrPage.DetectFillMethod(); 
         Console.WriteLine("Fill method detected: {0}", fillMethod); 
 
         // Clear the zones and do auto zone with specific options 
         ocrPage.AutoZone(OcrZoneParser.Default, fillMethod, LogicalUnit.Inch, null); 
         ShowZones("After AutoZone with specific options, units should be in inches", ocrPage); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 

 
private void ShowZones(string message, IOcrPage ocrPage) 

   Console.WriteLine("Zones after {0}:", message); 
   foreach(OcrZone ocrZone in ocrPage.Zones) 
   { 
      int index = ocrPage.Zones.IndexOf(ocrZone); 
      Console.WriteLine("Zone index: {0}", index); 
      Console.WriteLine("  Id                  {0}", ocrZone.Id); 
      Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
      Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
      Console.WriteLine("  FillMethod:         {0}", ocrZone.FillMethod); 
      Console.WriteLine("  RecognitionModule:  {0}", ocrZone.RecognitionModule); 
      Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
      Console.WriteLine("----------------------------------"); 
   } 
 
   Console.WriteLine("Hit enter to continue"); 
   Console.ReadLine(); 
}

Remarks

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

The zones found by this method are added to the Zones collection of this page. Any previously added zones will be removed from Zones first.

This method finds the zone coordinates (OcrZone.Bounds which will always have units of LogicalUnit.Pixels) and type (OcrZone.ZoneType). The type of the zone determines its role in the page layout and can be classified into three different groups:

  1. The flowed text zone types: OcrZoneType.Text, OcrZoneType.Column, OcrZoneType.Header, OcrZoneType.Footer, OcrZoneType.Caption, OcrZoneType.Title, OcrZoneType.VerticalText, OcrZoneType.LeftRotatedText, OcrZoneType.RightRotatedText and OcrZoneType.Other. These types mean that the zone contains textual information without a table type structure inside (it is flowed text). These types listed above are considered the same, they will not be differentiated from each other during the later processing stages.

    Note: Only OcrZoneType.Text is currently supported for the LEADTOOLS OCR Advantage Engine.

  2. The OcrZoneType.Table type. A zone having this type means that the zone is detected as containing a table, i.e. with columns, with or without a grid. Such zones will be handled differently from those of flowed text type zones.
  3. The OcrZoneType.Graphic type means a zone contains graphics, i.e. this zone will not be recognized at all. The only reason to have such a zone is to save or export the image inside the zone area.

The new zones found by this method will always have the OcrZone.FillMethod and OcrZone.RecognitionModule set to OcrZoneFillMethod.Default and OcrZoneRecognitionModule.Auto respectively. In order to override this automatic fill method and recognition module, you must update each zone in Zones by getting the zone with the IOcrZoneCollection getter, update and set it back with the setter. You can get the number of zones found by this method with the Zones.Count property.

New zones created by this method will always have an empty string in its OcrZone.UserDictionarySectionName property which means the use of the default section of the User dictionary. For more information, refer to IOcrUserDictionary.

The AutoZone(OcrZoneParser zoneParser, OcrZoneFillMethod fillMethod, LogicalUnit zonesUnit, OcrProgressCallback callback) override of this method lets you control the zone parser, default fill method and the units of the zones found.

To update the zones of a page with fill method set to OcrZoneFillMethod.Default, use UpdateFillMethod.

To detect the global fill method of a page, use DetectFillMethod.

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 IOcrPage.Recognize or RecognizeText methods 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 IOcrPage.Recognize or or 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.

Requirements

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

See Also

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