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



engine
The OCR engine instance.
page
The page currently being recognized. To get the index of this page, use OcrDocument.Pages.IndexOf.
zoneIndex
The zero-based index of the zone in page currently being recognized. To get the zone info, use IOcrPage.Zones passing zoneIndex as the index value.
value
Word or line to be checked by the method.
Provides a callback for the global user-defined spell checking.

Syntax

Visual Basic (Declaration) 
Public Delegate Function OcrSpellCheckCallback( _
   ByVal engine As IOcrEngine, _
   ByVal page As IOcrPage, _
   ByVal zoneIndex As Integer, _
   ByVal value As String _
) As OcrSpellCheckOpinion
Visual Basic (Usage)Copy Code
Dim instance As New OcrSpellCheckCallback(AddressOf HandlerMethod)
C# 
public delegate OcrSpellCheckOpinion OcrSpellCheckCallback( 
   IOcrEngine engine,
   IOcrPage page,
   int zoneIndex,
   string value
)
C++/CLI 
public delegate OcrSpellCheckOpinion OcrSpellCheckCallback( 
   IOcrEngine^ engine,
   IOcrPage^ page,
   int zoneIndex,
   String^ value
)

Parameters

engine
The OCR engine instance.
page
The page currently being recognized. To get the index of this page, use OcrDocument.Pages.IndexOf.
zoneIndex
The zero-based index of the zone in page currently being recognized. To get the zone info, use IOcrPage.Zones passing zoneIndex as the index value.
value
Word or line to be checked by the method.

Example

This example attempts to recognize an image having a misspelled word and then uses the user-defined spell checking callback to give an opinion about the engine suggestions.

Visual BasicCopy Code
Public Sub OcrSpellCheckWordCallbackExample()
   ' 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 misspelled number
   Dim numberString As String = "123!4"

   Dim image As New RasterImage(RasterMemoryFlags.Conventional, 320, 200, 1, 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.FillRectangle(Brushes.White, imageRect)

      Using f As New Font("Arial", 60, FontStyle.Regular)
         Using sf As New StringFormat()
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            g.DrawString(numberString, f, Brushes.Black, imageRect, sf)
         End Using
      End Using
   End Using

   RasterImage.DeleteLeadDC(hdc)

   ' 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()

         ' Add a callback for user-defined spell checking
         ocrEngine.SpellCheckManager.SetSpellCheckCallback(AddressOf MySpellCheckCallback)

         ' For the spell checking to work, we need to add the zone manually as apposed to calling
         ' AutoZone. So add one zone for the whole page
         Dim ocrZone As New OcrZone()
         ocrZone.Name = "MyZone"
         ocrZone.Selected = False
         ocrZone.Bounds = New LogicalRectangle(imageRect)
         ocrZone.ZoneType = OcrZoneType.Text
         ocrZone.FillMethod = OcrZoneFillMethod.Default
         ocrZone.RecognitionModule = OcrZoneRecognitionModule.Auto
         ocrZone.CharacterFilters = OcrZoneCharacterFilters.None
         ocrZone.UserDictionarySectionName = Nothing
         ocrZone.RecognitionOptions = OcrZoneRecognitionOptions.None

         ' Add the zone, this will use our spell check callback since we set it before
         ocrPage.Zones.Add(ocrZone)

         ' Recognize this page
         Dim text As String = ocrPage.RecognizeText(Nothing)
         Console.WriteLine("Recognition results\n--------------\n{0}", text)
      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 Function MySpellCheckCallback(ByVal engine As IOcrEngine, ByVal page As IOcrPage, ByVal zoneIndex As Integer, ByVal recognizedValue As String) As OcrSpellCheckOpinion
   Dim pageIndex As Integer = page.Document.Pages.IndexOf(page)
   Console.WriteLine("Checking (Page: {0}/Zone: {1}): {2}", pageIndex, zoneIndex, recognizedValue)

   ' See if the text contains digits only
   Dim result As Integer
   If (Integer.TryParse(recognizedValue, result)) Then
      Return OcrSpellCheckOpinion.Sure
   Else
      Return OcrSpellCheckOpinion.Impossible
   End If
End Function
C#Copy Code
public void OcrSpellCheckWordCallbackExample() 

   // 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 misspelled number 
   string numberString = "123!4"; 
 
   RasterImage image = new RasterImage(RasterMemoryFlags.Conventional, 320, 200, 1, 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.FillRectangle(Brushes.White, imageRect); 
 
      using(Font f = new Font("Arial", 60, FontStyle.Regular)) 
      { 
         using(StringFormat sf = new StringFormat()) 
         { 
            sf.Alignment = StringAlignment.Center; 
            sf.LineAlignment = StringAlignment.Center; 
            g.DrawString(numberString, f, Brushes.Black, imageRect, sf); 
         } 
      } 
   } 
 
   RasterImage.DeleteLeadDC(hdc); 
 
   // 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(); 
 
         // Add a callback for user-defined spell checking 
         ocrEngine.SpellCheckManager.SetSpellCheckCallback(new OcrSpellCheckCallback(MySpellCheckCallback)); 
 
         // For the spell checking to work, we need to add the zone manually as apposed to calling 
         // AutoZone. So add one zone for the whole page 
         OcrZone ocrZone = new OcrZone(); 
         ocrZone.Name = "MyZone"; 
         ocrZone.Selected = false; 
         ocrZone.Bounds = new LogicalRectangle(imageRect); 
         ocrZone.ZoneType = OcrZoneType.Text; 
         ocrZone.FillMethod = OcrZoneFillMethod.Default; 
         ocrZone.RecognitionModule = OcrZoneRecognitionModule.Auto; 
         ocrZone.CharacterFilters = OcrZoneCharacterFilters.None; 
         ocrZone.UserDictionarySectionName = null; 
         ocrZone.RecognitionOptions = OcrZoneRecognitionOptions.None; 
 
         // Add the zone, this will use our spell check callback since we set it before 
         ocrPage.Zones.Add(ocrZone); 
 
         // Recognize this page and get the text 
         string text = ocrPage.RecognizeText(null); 
 
         Console.WriteLine("Recognition results\n--------------\n{0}", text); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 

 
private OcrSpellCheckOpinion MySpellCheckCallback(IOcrEngine engine, IOcrPage page, int zoneIndex, string recognizedValue) 

   int pageIndex = page.Document.Pages.IndexOf(page); 
   Console.WriteLine("Checking (Page: {0}/Zone: {1}): {2}", pageIndex, zoneIndex, recognizedValue); 
 
   // See if the text contains digits only 
   int result; 
   if(int.TryParse(recognizedValue, out result)) 
      return OcrSpellCheckOpinion.Sure; 
   else 
      return OcrSpellCheckOpinion.Impossible; 
}

Remarks

You can set the OcrSpellCheckCallback to be used globally in the engine through the IOcrSpellCheckManager.SetSpellCheckCallback method.

The callback will only work with zones added manually by the user. The zones automatically found by the engine through IOcrPage.AutoZone will not cause the callback to be called.

This callback allows the user intervention in the spell checking process. The callback is called by the checking subsystem of the engine during the recognition operation. The user can then influence the decision about the recognized result through the returned OcrSpellCheckOpinion value.

For an example, if the particular zone in the page to be recognized is allowed to contain digits and only certain values are allowed. The user can return the following values:

If the particular zone in the page to be recognized is allowed to have digits only by no further restrictions, the user can return the following values:

Note: User-defined checking through the OcrSpellCheckCallback callback is not supported when using the LEADTOOLS OCR Advantage Engine.

Requirements

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

See Also

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