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



Class that contains the processing engine used to perform field processing.

Object Model



Syntax

Visual Basic (Declaration) 
Public Class FormProcessingEngine 
Visual Basic (Usage)Copy Code
Dim instance As FormProcessingEngine
C# 
public class FormProcessingEngine 
C++/CLI 
public ref class FormProcessingEngine 

Example

This example shows a complete skeleton of a form recognition and processing application.

Visual BasicCopy Code
''' This class contains all Master Form information.
Friend Class MasterForm
   Public Image As RasterImage
   Public Attributes As FormRecognitionAttributes
   Public Properties As FormRecognitionProperties
   Public ProcessingPages As FormPages
   ''' This constructor initialize all MasterForm memebers to zero/null/Empty.
   Public Sub New()
      Image = Nothing
      Attributes = Nothing
      Properties = FormRecognitionProperties.Empty
      ProcessingPages = Nothing
   End Sub
End Class

''' This class contains all my Form information. My Form is the filled form that is going to be identified and processed by the application.
Friend Class MyForm
   Public FileName As String
   Public Image As RasterImage
   Public Attributes As FormRecognitionAttributes
   Public Master As MasterForm
   Public Result As FormRecognitionResult
   Public Alignment As IList(Of PageAlignment)
   Public ProcessingPages As FormPages

   ''' This constructor initialize all MyForm memebers to zero/null/Empty.
   Public Sub New()
      FileName = Nothing
      Image = Nothing
      Attributes = Nothing
      Master = Nothing
      Result = Nothing
      Alignment = Nothing
   End Sub
End Class

''' TestForms is a class that has all the needed methods to recognize and process unknown forms. Once an instance of it is created StartUp should be called first to intialize
''' Ocr and barcode engines, besides building the Master Forms set, then Run method should be called to recognize and process a specific form, finally the ShutDown method should be called
''' once the program is terminated.
Friend Class TestForms
   Private RecognitionEngine As FormRecognitionEngine
   Private ProcessingEngine As FormProcessingEngine
   Private FormsBarcodeEngine As BarcodeEngine
   Private FormsOcrEngine As IOcrEngine
   Private FormSet As List(Of MasterForm)
   Private RecognizeFirstPageOnly As Boolean
   Private LoadFromScanner As Boolean
   Private FormsCodec As RasterCodecs


   '===========================================================================
   ''' This method loads an image from scanner.
   Public Function LoadImageScanner(ByVal count As Integer) As RasterImage
      'TODO
      Return Nothing
   End Function

   ''' This method loads an image from scanner for recognition purposes.
   Public Function LoadRecognitionImageScanner() As RasterImage
      'TODO
      Return Nothing
   End Function

   ''' This method loads an image from a file.
   Public Function LoadImageFile(ByVal fileName As String, ByVal firstPage As Integer, ByVal lastPage As Integer) As RasterImage
      ' Load the image and verify that we loaded all the pages
      Dim image As RasterImage = FormsCodec.Load(fileName, 1, CodecsLoadByteOrder.Bgr, firstPage, lastPage)
      Return image
   End Function
   '===========================================================================
   ''' This method loads an image from a file for recognition purposes.
   Public Function LoadRecognitionImageFile(ByVal fileName As String) As RasterImage
      Dim image As RasterImage
      If RecognizeFirstPageOnly Then
         image = LoadImageFile(fileName, 1, 1)
      Else
         image = LoadImageFile(fileName, 1, -1)
      End If
      Return image
   End Function

   ''' This method loads an image for recognition purposes.
   Public Function LoadRecognitionImage(ByVal fileName As String) As RasterImage
      Dim image As RasterImage
      If RecognizeFirstPageOnly Then
         image = LoadRecognitionImageScanner()
      Else
         image = LoadRecognitionImageFile(fileName)
      End If
      Return image
   End Function

   '===========================================================================
   ''' This method creates a form attributes for recognition.
   Public Function CreateForm() As FormRecognitionAttributes
      Dim attributes As FormRecognitionAttributes = RecognitionEngine.CreateForm(Nothing)
      RecognitionEngine.CloseForm(attributes)
      Return attributes
   End Function

   ''' This method adds a page to form attributes for recognition.
   Public Sub AddPageToForm(ByVal image As RasterImage, ByVal attributes As FormRecognitionAttributes)
      RecognitionEngine.OpenForm(attributes)
      RecognitionEngine.AddFormPage(attributes, image, Nothing)
      RecognitionEngine.CloseForm(attributes)
   End Sub

   ''' This method deletes a page from form attributes for recognition.
   Public Sub DeletePageFromForm(ByVal pagenumber As Integer, ByVal form As FormRecognitionAttributes)
      RecognitionEngine.OpenForm(form)
      RecognitionEngine.DeleteFormPage(form, pagenumber)
      RecognitionEngine.CloseForm(form)
   End Sub

   ''' This method creates a form with its pages for recognition.
   Public Sub CreateFormForRecognition(ByVal form As MyForm)
      form.Attributes = CreateForm()
      Dim saveCurrentPageIndex As Integer = form.Image.Page
      Dim i As Integer = 0
      Do While i < form.Image.PageCount
         form.Image.Page = i + 1 'page index is a 1-based starts from 1 not zero
         AddPageToForm(form.Image, form.Attributes)
         i += 1
      Loop
      form.Image.Page = saveCurrentPageIndex
   End Sub

   '===========================================================================
   ''' This method creates a Master Form recognition attributes.
   Public Function CreateMasterForm(ByVal name As String) As FormRecognitionAttributes
      Dim attributes As FormRecognitionAttributes = RecognitionEngine.CreateMasterForm(name, Guid.Empty, Nothing)
      RecognitionEngine.CloseMasterForm(attributes)
      Return attributes
   End Function

   ''' This method adds a page to a Master Form recognition attributes.
   Public Sub AddPageToMasterForm(ByVal image As RasterImage, ByVal attributes As FormRecognitionAttributes)
      RecognitionEngine.OpenMasterForm(attributes)
      RecognitionEngine.AddMasterFormPage(attributes, image, Nothing)
      RecognitionEngine.CloseMasterForm(attributes)
   End Sub

   ''' This method deletes a page from a Master Form recognition attributes.
   Public Sub DeletePageFromMasterForm(ByVal pagenumber As Integer, ByVal form As FormRecognitionAttributes)
      RecognitionEngine.OpenMasterForm(form)
      RecognitionEngine.DeleteMasterFormPage(form, pagenumber)
      RecognitionEngine.CloseMasterForm(form)
   End Sub

   ''' This method creates a Master Form with its pages for recognition purposes.
   Public Sub CreateMasterFormForRecognition(ByVal name As String, ByVal form As MasterForm)
      form.Attributes = CreateMasterForm(name)
      Dim saveCurrentPageIndex As Integer = form.Image.Page
      Dim i As Integer = 0
      Do While i < form.Image.PageCount
         form.Image.Page = i + 1 'page index is a 1-based starts from 1 not zero
         AddPageToMasterForm(form.Image, form.Attributes)
         i += 1
      Loop
      form.Image.Page = saveCurrentPageIndex
   End Sub

   '===========================================================================
   '''This method compares a Form to a Master Form using all pages.
   Private Function CompareForm(ByVal master As FormRecognitionAttributes, ByVal form As FormRecognitionAttributes) As FormRecognitionResult
      Return RecognitionEngine.CompareForm(master, form, Nothing)
   End Function

   '''This method compares the first page of a Form to the first page of a Master Form.
   Private Function CompareFirstPage(ByVal master As FormRecognitionAttributes, ByVal form As FormRecognitionAttributes) As FormRecognitionResult
      Dim resultPage As PageRecognitionResult = RecognitionEngine.ComparePage(master, 1, form, 1)
      Dim result As FormRecognitionResult = New FormRecognitionResult()
      result.Confidence = resultPage.Confidence
      result.LargestConfidencePageNumber = 1
      result.PageResults.Add(resultPage)
      result.Reason = FormRecognitionReason.Success
      Return result
   End Function

   '''This method identifies the type of the form based on the comparison results.
   Public Function IdentefyForm(ByVal results As FormRecognitionResult()) As Integer
      Dim maxIndex As Integer = 0
      maxIndex = 0
      Dim i As Integer = 1
      Do While i < results.Length
         If results(maxIndex).Confidence < results(i).Confidence Then
            maxIndex = i
         End If
         i += 1
      Loop
      If results(maxIndex).Confidence < 30 Then
         maxIndex = -1 'no match
      End If
      Return maxIndex
   End Function

   '''This method recognizes the type of the unknown form
   Public Sub RecognizeForm(ByVal form As MyForm)
      CreateFormForRecognition(form)
      Dim results As FormRecognitionResult() = New FormRecognitionResult(FormSet.Count - 1){}
      Dim i As Integer = 0
      Do While i < FormSet.Count
         If RecognizeFirstPageOnly Then
            results(i) = CompareFirstPage(FormSet(i).Attributes, form.Attributes)
         Else
            results(i) = CompareForm(FormSet(i).Attributes, form.Attributes)
         End If
         i += 1
      Loop
      Dim index As Integer = IdentefyForm(results)
      If index >= 0 Then
         form.Master = FormSet(index)
         form.Result = results(index)
      Else
         form.Master = Nothing
         form.Result = Nothing
      End If
   End Sub
   '===========================================================================
   '''This method load images for processing purposes if needed.
   Private Function LoadProcessingImage(ByVal form As MyForm) As Boolean
      Dim count As Integer = form.Master.Properties.Pages - form.Image.PageCount
      If count = 0 Then
         Return False
      End If

      Dim image As RasterImage

      If LoadFromScanner Then
         image = LoadImageScanner(count)
      Else
         image = LoadImageFile(form.FileName, form.Image.PageCount + 1, form.Image.PageCount + count)
      End If

      form.Image.AddPages(image, 1, count)
      Return True
   End Function
   '===========================================================================
   '''This method calculates the aligmnet for the recognized form.
   Public Sub AlignForm(ByVal form As MyForm, ByVal calculateAlignment As Boolean)
      If calculateAlignment Then
         CreateFormForRecognition(form)
         form.Alignment = RecognitionEngine.GetFormAlignment(form.Master.Attributes, form.Attributes, Nothing)
      Else
         form.Alignment = New List(Of PageAlignment)()
         Dim i As Integer = 0
         Do While i < form.Result.PageResults.Count
            form.Alignment.Add(form.Result.PageResults(i).Alignment)
            i += 1
         Loop
      End If
   End Sub

   '===========================================================================
   '''This method processes the recognized form.
   Public Sub ProcessForm(ByVal form As MyForm)
      form.ProcessingPages = form.Master.ProcessingPages
      ProcessingEngine.Pages.Clear()
      ProcessingEngine.Pages.AddRange(form.ProcessingPages)
      ProcessingEngine.Process(form.Image, form.Alignment)
   End Sub

   '===========================================================================
   '''This method recognizes the unknown form then processes it.
   Public Sub RunFormRecognitionAndProcessing(ByVal form As MyForm)
      form.Image = LoadRecognitionImage(form.FileName)
      RecognizeForm(form)
      If form.Master Is Nothing Then
         Console.WriteLine("Unknown form")
         Return
      End If
      Dim calculateAlignment As Boolean = LoadProcessingImage(form)
      AlignForm(form, calculateAlignment)
      ProcessForm(form)
      PrintOutResults(form)
   End Sub

   '===========================================================================
   ''' This method loads the specified master form attributes, fields, and image.
   Public Function LoadMasterForm(ByVal attributesFileName As String, ByVal fieldsFileName As String, ByVal imageFileName As String) As MasterForm
      Dim formData As Byte()
      Dim form As MasterForm = New MasterForm()
      formData = File.ReadAllBytes(attributesFileName)
      form.Attributes = New FormRecognitionAttributes
      form.Attributes.SetData(formData)
      form.Properties = RecognitionEngine.GetFormProperties(form.Attributes)
      ProcessingEngine.LoadFields(fieldsFileName)
      form.ProcessingPages = ProcessingEngine.Pages
      form.Image = FormsCodec.Load(imageFileName, 1, CodecsLoadByteOrder.Bgr, 1, -1)
      Return form
   End Function

   ''' This method saves master form attributes to the specified file name.
   Public Sub SaveMasterFormAttributes(ByVal form As MasterForm, ByVal attributesFileName As String)
      Dim formData As Byte() = form.Attributes.GetData()
      File.WriteAllBytes(attributesFileName, formData)
   End Sub

   ''' This method saves master form fields to the specified file name.
   Public Sub SaveMasterFormFields(ByVal form As MasterForm, ByVal fieldsFileName As String)
      ProcessingEngine.Pages.Clear()
      ProcessingEngine.Pages.AddRange(form.ProcessingPages)
      ProcessingEngine.SaveFields(fieldsFileName)
   End Sub

   '===========================================================================
   ''' This method builds the Form Set.
   Public Sub BuildFormsSet()
      FormSet = New List(Of MasterForm)()

      Dim Form107 As MasterForm = LoadMasterForm(LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\FFC-107\Recognition.bin", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\FFC-107\Processing.xml", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\FFC-107\MasterForm.tif")
      FormSet.Add(Form107)

      Dim Form180 As MasterForm = LoadMasterForm(LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-180\Recognition.bin", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-180\Processing.xml", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-180\MasterForm.tif")
      FormSet.Add(Form180)

      Dim Form3881 As MasterForm = LoadMasterForm(LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-3881\Recognition.bin", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-3881\Processing.xml", LeadtoolsExamples.Common.ImagesPath.Path + "Forms\FormsDemo\OCR_ICR\SF-3881\MasterForm.tif")
      FormSet.Add(Form3881)
   End Sub

   '===========================================================================

   ''' This method prints the image field results
   Public Sub PrintOutImage(ByVal field As ImageFormField)
      Console.WriteLine()
      Console.WriteLine("*** Image Field")
      Dim result As ImageFormFieldResult = TryCast(field.Result, ImageFormFieldResult)
      FormsCodec.Save(result.Image, LeadtoolsExamples.Common.ImagesPath.Path + "ImageField.tif", RasterImageFormat.Tif, 1)
   End Sub

   ''' This method prints the barcode field results
   Public Sub PrintOutBarcode(ByVal field As BarcodeFormField)
      Console.WriteLine()
      Console.WriteLine("*** Barcode Field")
      Dim result As BarcodeFormFieldResult = TryCast(field.Result, BarcodeFormFieldResult)
      For Each barcode As BarcodeFieldData In result.BarcodeData
         Console.WriteLine(" *- Barcode Type: " & barcode.SearchCodes)
         Console.WriteLine(" - Barcode Data: ")
         Console.WriteLine(barcode.GetData())
      Next barcode
   End Sub

   ''' This method prints the Omr field results
   Public Sub PrintOutOmr(ByVal field As OmrFormField)
      Console.WriteLine()
      Console.WriteLine("*** Omr Field")
      Dim result As OmrFormFieldResult = TryCast(field.Result, OmrFormFieldResult)
      Console.WriteLine(" Is Checked?[1 = yes, 0 = No] = " & result.Text)
   End Sub

   ''' This method prints the text field results
   Public Sub PrintOutText(ByVal field As TextFormField)
      Console.WriteLine()
      Console.WriteLine("*** Text Field")
      Dim result As TextFormFieldResult = TryCast(field.Result, TextFormFieldResult)
      Console.WriteLine("Text: " & result.Text)
   End Sub

   ''' This method prints the processing results
   Public Sub PrintOutResults(ByVal form As MyForm)
      If form.Result.Confidence < 30 Then
         Console.WriteLine("No match found")
         Return
      End If
      Console.WriteLine("=======================Type===========================")
      Console.WriteLine("Form Type is: " & form.Master.Properties.Name)
      Console.WriteLine("Confidance = " & form.Result.Confidence)
      Console.WriteLine()
      Console.WriteLine("====================Fieds Result=======================")

      Dim pageIndex As Integer = 0
      Do While pageIndex < form.ProcessingPages.Count
         Console.WriteLine()
         Console.WriteLine("=====Page # " & form.ProcessingPages(pageIndex).PageNumber & "=====")
         Console.WriteLine()
         For Each field As FormField In form.ProcessingPages(pageIndex)
            If TypeOf field Is ImageFormField Then
               PrintOutImage(TryCast(field, ImageFormField))
            ElseIf TypeOf field Is BarcodeFormField Then
               PrintOutBarcode(TryCast(field, BarcodeFormField))
            ElseIf TypeOf field Is OmrFormField Then
               PrintOutOmr(TryCast(field, OmrFormField))
            ElseIf TypeOf field Is TextFormField Then
               PrintOutText(TryCast(field, TextFormField))
            End If
         Next field
         pageIndex += 1
      Loop
   End Sub

   '===========================================================================
   '''This method starts up the Ocr engine
   Private Sub StartUpOcrEngine()
      Try
         FormsOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
         FormsOcrEngine.Startup(FormsCodec, System.IO.Path.GetTempPath(), "")
      Catch exp As Exception
         Console.WriteLine(exp.Message)
      End Try
   End Sub

   '''This method shuts down the Ocr engine
   Private Sub ShutDownOcrEngine()
      FormsOcrEngine.Shutdown()
      FormsOcrEngine.Dispose()
   End Sub

   '''This method starts up the Barcode engine
   Private Sub StartUpBarcodeEngine()
      Try
         BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d Or BarcodeMajorTypeFlags.Barcodes2dRead Or BarcodeMajorTypeFlags.BarcodesDatamatrixRead Or BarcodeMajorTypeFlags.BarcodesPdfRead Or BarcodeMajorTypeFlags.BarcodesQrRead)
         FormsBarcodeEngine = New BarcodeEngine()
      Catch exp As Exception
         Console.WriteLine(exp.Message)
      End Try
   End Sub

   '''This method shuts down the Barcode engine
   Private Sub ShutDownBarcodeEngine()
      BarcodeEngine.Shutdown()
   End Sub

   '''This method sets the object managers.
   Public Sub SetObjectManagers(ByVal enableDefault As Boolean, ByVal enableOcr As Boolean, ByVal enableBarcode As Boolean)
      If RecognitionEngine Is Nothing Then
         Return
      End If
      If enableDefault Then
         Dim defaultObjectManager As DefaultObjectsManager = New DefaultObjectsManager()
         RecognitionEngine.ObjectsManagers.Add(defaultObjectManager)
      End If

      If enableOcr Then
         Dim ocrObejectManager As OcrObjectsManager = New OcrObjectsManager(FormsOcrEngine)
         ocrObejectManager.Engine = FormsOcrEngine
         RecognitionEngine.ObjectsManagers.Add(ocrObejectManager)
      End If

      If enableBarcode Then
         Dim barcodeObjectManager As BarcodeObjectsManager = New BarcodeObjectsManager(FormsBarcodeEngine)
         barcodeObjectManager.Engine = FormsBarcodeEngine
         RecognitionEngine.ObjectsManagers.Add(barcodeObjectManager)
      End If
   End Sub

   '''This method starts up all engines.
   Public Sub StartUpEngines()
      StartUpOcrEngine()
      StartUpBarcodeEngine()
      RecognitionEngine = New FormRecognitionEngine()
      SetObjectManagers(False, True, False)
      ProcessingEngine = New FormProcessingEngine()
      ProcessingEngine.OcrEngine = FormsOcrEngine
      ProcessingEngine.BarcodeEngine = FormsBarcodeEngine
   End Sub

   '''This method shuts down all engines.
   Public Sub ShutDownEngines()
      ShutDownOcrEngine()
      ShutDownBarcodeEngine()
   End Sub

   '''This method starts up RasterCodec, intialize Form Codec, starts up all engines, and Build the form set
   Public Sub StartUp()
      '1)
      RasterCodecs.Startup()
      FormsCodec = New RasterCodecs()
      '2)
      StartUpEngines()
      '3)
      RecognizeFirstPageOnly = False
      LoadFromScanner = False
      '4)
      BuildFormsSet()
   End Sub

   '''This method shuts down engines, dispose all images, and shut down the RasterCodec.
   Public Sub ShutDown()
      '1)
      ShutDownEngines()
      '2)
      For Each master As MasterForm In FormSet
         master.Image.Dispose()
      Next master
      '3)
      FormsCodec.Dispose()
      RasterCodecs.Shutdown()
   End Sub


   ''' Here all starts for to recognize and process the unknown form that is stored in a fiel with passed fileName.
   Public Sub Run(ByVal fileName As String)
      Dim form As MyForm = New MyForm()
      form.FileName = fileName
      RunFormRecognitionAndProcessing(form)
      If Not form.Image Is Nothing Then
         form.Image.Dispose()
      End If
   End Sub
End Class

Friend Class Program
   ''' The main entry to the program.
   Shared Sub Main(ByVal args As String())
      If args Is Nothing OrElse args.Length = 0 Then
         Return
      End If

      Dim test As TestForms = New TestForms()

      test.StartUp()

      test.Run(args(0))

      test.ShutDown()
   End Sub
End Class
C#Copy Code
/// This class contains all Master Form information. 
class MasterForm 

   public RasterImage Image; 
   public FormRecognitionAttributes Attributes; 
   public FormRecognitionProperties Properties; 
   public FormPages ProcessingPages; 
   /// This constructor initialize all MasterForm memebers to zero/null/Empty. 
   public MasterForm() 
   { 
      Image = null; 
      Attributes = null; 
      Properties = FormRecognitionProperties.Empty; 
      ProcessingPages = null; 
   } 

 
/// This class contains all my Form information. My Form is the filled form that is going to be identified and processed by the application. 
class MyForm 

   public string FileName; 
   public RasterImage Image; 
   public FormRecognitionAttributes Attributes; 
   public MasterForm Master; 
   public FormRecognitionResult Result; 
   public IList<PageAlignment> Alignment; 
   public FormPages ProcessingPages; 
 
   /// This constructor initialize all MyForm memebers to zero/null/Empty. 
   public MyForm() 
   { 
      FileName = null; 
      Image = null; 
      Attributes = null; 
      Master = null; 
      Result = null; 
      Alignment = null; 
   } 

 
/// TestForms is a class that has all the needed methods to recognize and process unknown forms. Once an instance of it is created StartUp should be called first to intialize 
/// Ocr and barcode engines, besides building the Master Forms set, then Run method should be called to recognize and process a specific form, finally the ShutDown method should be called 
/// once the program is terminated. 
class TestForms 

   FormRecognitionEngine RecognitionEngine; 
   FormProcessingEngine ProcessingEngine; 
   BarcodeEngine FormsBarcodeEngine; 
   IOcrEngine FormsOcrEngine; 
   List<MasterForm> FormSet; 
   bool RecognizeFirstPageOnly; 
   bool LoadFromScanner; 
   RasterCodecs FormsCodec; 
 
 
   //=========================================================================== 
   /// This method loads an image from scanner. 
   public RasterImage LoadImageScanner(int count) 
   { 
      //TODO 
      return null; 
   } 
 
   /// This method loads an image from scanner for recognition purposes. 
   public RasterImage LoadRecognitionImageScanner() 
   { 
      //TODO 
      return null; 
   } 
 
   /// This method loads an image from a file. 
   public RasterImage LoadImageFile(string fileName, int firstPage, int lastPage) 
   { 
      // Load the image and verify that we loaded all the pages  
      RasterImage image = FormsCodec.Load(fileName, 1, CodecsLoadByteOrder.Bgr, firstPage, lastPage); 
      return image; 
   } 
   //=========================================================================== 
   /// This method loads an image from a file for recognition purposes. 
   public RasterImage LoadRecognitionImageFile(string fileName) 
   { 
      RasterImage image; 
      if(RecognizeFirstPageOnly) 
         image = LoadImageFile(fileName, 1, 1); 
      else 
         image = LoadImageFile(fileName, 1, -1); 
      return image; 
   } 
 
   /// This method loads an image for recognition purposes. 
   public RasterImage LoadRecognitionImage(string fileName) 
   { 
      RasterImage image; 
      if(RecognizeFirstPageOnly) 
         image = LoadRecognitionImageScanner(); 
      else 
         image = LoadRecognitionImageFile(fileName); 
      return image; 
   } 
 
   //=========================================================================== 
   /// This method creates a form attributes for recognition. 
   public FormRecognitionAttributes CreateForm() 
   { 
      FormRecognitionAttributes attributes = RecognitionEngine.CreateForm(null); 
      RecognitionEngine.CloseForm(attributes); 
      return attributes; 
   } 
 
   /// This method adds a page to form attributes for recognition. 
   public void AddPageToForm(RasterImage image, FormRecognitionAttributes attributes) 
   { 
      RecognitionEngine.OpenForm(attributes); 
      RecognitionEngine.AddFormPage(attributes, image, null); 
      RecognitionEngine.CloseForm(attributes); 
   } 
 
   /// This method deletes a page from form attributes for recognition. 
   public void DeletePageFromForm(int pagenumber, FormRecognitionAttributes form) 
   { 
      RecognitionEngine.OpenForm(form); 
      RecognitionEngine.DeleteFormPage(form, pagenumber); 
      RecognitionEngine.CloseForm(form); 
   } 
 
   /// This method creates a form with its pages for recognition. 
   public void CreateFormForRecognition(MyForm form) 
   { 
      form.Attributes = CreateForm(); 
      int saveCurrentPageIndex = form.Image.Page; 
      for(int i = 0; i < form.Image.PageCount; i++) 
      { 
         form.Image.Page = i + 1;//page index is a 1-based starts from 1 not zero 
         AddPageToForm(form.Image, form.Attributes); 
      } 
      form.Image.Page = saveCurrentPageIndex; 
   } 
 
   //=========================================================================== 
   /// This method creates a Master Form recognition attributes. 
   public FormRecognitionAttributes CreateMasterForm(string name) 
   { 
      FormRecognitionAttributes attributes = RecognitionEngine.CreateMasterForm(name, Guid.Empty, null); 
      RecognitionEngine.CloseMasterForm(attributes); 
      return attributes; 
   } 
 
   /// This method adds a page to a Master Form recognition attributes. 
   public void AddPageToMasterForm(RasterImage image, FormRecognitionAttributes attributes) 
   { 
      RecognitionEngine.OpenMasterForm(attributes); 
      RecognitionEngine.AddMasterFormPage(attributes, image, null); 
      RecognitionEngine.CloseMasterForm(attributes); 
   } 
 
   /// This method deletes a page from a Master Form recognition attributes. 
   public void DeletePageFromMasterForm(int pagenumber, FormRecognitionAttributes form) 
   { 
      RecognitionEngine.OpenMasterForm(form); 
      RecognitionEngine.DeleteMasterFormPage(form, pagenumber); 
      RecognitionEngine.CloseMasterForm(form); 
   } 
 
   /// This method creates a Master Form with its pages for recognition purposes. 
   public void CreateMasterFormForRecognition(string name, MasterForm form) 
   { 
      form.Attributes = CreateMasterForm(name); 
      int saveCurrentPageIndex = form.Image.Page; 
      for(int i = 0; i < form.Image.PageCount; i++) 
      { 
         form.Image.Page = i + 1;//page index is a 1-based starts from 1 not zero 
         AddPageToMasterForm(form.Image, form.Attributes); 
      } 
      form.Image.Page = saveCurrentPageIndex; 
   } 
 
   //=========================================================================== 
   ///This method compares a Form to a Master Form using all pages. 
   FormRecognitionResult CompareForm(FormRecognitionAttributes master, FormRecognitionAttributes form) 
   { 
      return RecognitionEngine.CompareForm(master, form, null); 
   } 
 
   ///This method compares the first page of a Form to the first page of a Master Form. 
   FormRecognitionResult CompareFirstPage(FormRecognitionAttributes master, FormRecognitionAttributes form) 
   { 
      PageRecognitionResult resultPage = RecognitionEngine.ComparePage(master, 1, form, 1); 
      FormRecognitionResult result = new FormRecognitionResult(); 
      result.Confidence = resultPage.Confidence; 
      result.LargestConfidencePageNumber = 1; 
      result.PageResults.Add(resultPage); 
      result.Reason = FormRecognitionReason.Success; 
      return result; 
   } 
 
   ///This method identifies the type of the form based on the comparison results. 
   public int IdentefyForm(FormRecognitionResult[] results) 
   { 
      int maxIndex = 0; 
      maxIndex = 0; 
      for(int i = 1; i < results.Length; i++) 
      { 
         if(results[maxIndex].Confidence < results[i].Confidence) 
            maxIndex = i; 
      } 
      if(results[maxIndex].Confidence < 30) 
         maxIndex = -1;//no match 
      return maxIndex; 
   } 
 
   ///This method recognizes the type of the unknown form 
   public void RecognizeForm(MyForm form) 
   { 
      CreateFormForRecognition(form); 
      FormRecognitionResult[] results = new FormRecognitionResult[FormSet.Count]; 
      for(int i = 0; i < FormSet.Count; i++) 
      { 
         if(RecognizeFirstPageOnly) 
            results[i] = CompareFirstPage(FormSet[i].Attributes, form.Attributes); 
         else 
            results[i] = CompareForm(FormSet[i].Attributes, form.Attributes); 
      } 
      int index = IdentefyForm(results); 
      if(index >= 0) 
      { 
         form.Master = FormSet[index]; 
         form.Result = results[index]; 
      } 
      else 
      { 
         form.Master = null; 
         form.Result = null; 
      } 
   } 
   //=========================================================================== 
   ///This method load images for processing purposes if needed. 
   bool LoadProcessingImage(MyForm form) 
   { 
      int count = form.Master.Properties.Pages - form.Image.PageCount; 
      if(count == 0) 
         return false; 
 
      RasterImage image; 
 
      if(LoadFromScanner) 
         image = LoadImageScanner(count); 
      else 
         image = LoadImageFile(form.FileName, form.Image.PageCount + 1, form.Image.PageCount + count); 
 
      form.Image.AddPages(image, 1, count); 
      return true; 
   } 
   //=========================================================================== 
   ///This method calculates the aligmnet for the recognized form. 
   public void AlignForm(MyForm form, bool calculateAlignment) 
   { 
      if(calculateAlignment) 
      { 
         CreateFormForRecognition(form); 
         form.Alignment = RecognitionEngine.GetFormAlignment(form.Master.Attributes, form.Attributes, null); 
      } 
      else 
      { 
         form.Alignment = new List<PageAlignment>(); 
         for(int i = 0; i < form.Result.PageResults.Count; i++) 
            form.Alignment.Add(form.Result.PageResults[i].Alignment); 
      } 
   } 
 
   //=========================================================================== 
   ///This method processes the recognized form. 
   public void ProcessForm(MyForm form) 
   { 
      form.ProcessingPages = form.Master.ProcessingPages; 
      ProcessingEngine.Pages.Clear(); 
      ProcessingEngine.Pages.AddRange(form.ProcessingPages); 
      ProcessingEngine.Process(form.Image, form.Alignment); 
   } 
 
   //=========================================================================== 
   ///This method recognizes the unknown form then processes it. 
   public void RunFormRecognitionAndProcessing(MyForm form) 
   { 
      form.Image = LoadRecognitionImage(form.FileName); 
      RecognizeForm(form); 
      if (form.Master == null) 
      { 
         Console.WriteLine("Unknown form"); 
         return; 
      } 
      bool calculateAlignment = LoadProcessingImage(form); 
      AlignForm(form, calculateAlignment); 
      ProcessForm(form); 
      PrintOutResults(form); 
   } 
 
   //=========================================================================== 
   /// This method loads the specified master form attributes, fields, and image. 
   public MasterForm  LoadMasterForm(string attributesFileName, string fieldsFileName, string imageFileName) 
   { 
      byte[] formData; 
      MasterForm form = new MasterForm(); 
      formData = File.ReadAllBytes(attributesFileName); 
      form.Attributes = new FormRecognitionAttributes(); 
      form.Attributes.SetData(formData); 
      form.Properties = RecognitionEngine.GetFormProperties(form.Attributes); 
      ProcessingEngine.LoadFields(fieldsFileName); 
      form.ProcessingPages = ProcessingEngine.Pages; 
      form.Image = FormsCodec.Load(imageFileName, 1, CodecsLoadByteOrder.Bgr, 1, -1); 
      return form; 
   } 
 
   /// This method saves master form attributes to the specified file name. 
   public void SaveMasterFormAttributes(MasterForm form, string attributesFileName) 
   { 
      byte[] formData = form.Attributes.GetData(); 
      File.WriteAllBytes(attributesFileName, formData); 
   } 
 
   /// This method saves master form fields to the specified file name. 
   public void SaveMasterFormFields(MasterForm form, string fieldsFileName) 
   { 
      ProcessingEngine.Pages.Clear(); 
      ProcessingEngine.Pages.AddRange(form.ProcessingPages); 
      ProcessingEngine.SaveFields(fieldsFileName); 
   } 
 
   //=========================================================================== 
   /// This method builds the Form Set. 
   public void BuildFormsSet() 
   { 
      FormSet = new List<MasterForm>(); 
 
      MasterForm Form107 = LoadMasterForm( 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\FFC-107\Recognition.bin", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\FFC-107\Processing.xml", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\FFC-107\MasterForm.tif"); 
      FormSet.Add(Form107); 
 
      MasterForm Form180 = LoadMasterForm( 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-180\Recognition.bin", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-180\Processing.xml", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-180\MasterForm.tif"); 
      FormSet.Add(Form180); 
 
      MasterForm Form3881 = LoadMasterForm( 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-3881\Recognition.bin", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-3881\Processing.xml", 
         LeadtoolsExamples.Common.ImagesPath.Path +@"Forms\FormsDemo\OCR_ICR\SF-3881\MasterForm.tif"); 
      FormSet.Add(Form3881); 
   } 
 
   //=========================================================================== 
 
   /// This method prints the image field results 
   public void PrintOutImage(ImageFormField field) 
   { 
      Console.WriteLine(); 
      Console.WriteLine("*** Image Field"); 
      ImageFormFieldResult result = field.Result as ImageFormFieldResult; 
      FormsCodec.Save(result.Image,LeadtoolsExamples.Common.ImagesPath.Path +"ImageField.tif", RasterImageFormat.Tif, 1); 
   } 
 
   /// This method prints the barcode field results 
   public void PrintOutBarcode(BarcodeFormField field) 
   { 
      Console.WriteLine(); 
      Console.WriteLine("*** Barcode Field"); 
      BarcodeFormFieldResult result = field.Result as BarcodeFormFieldResult; 
      foreach(BarcodeFieldData barcode in result.BarcodeData) 
      { 
         Console.WriteLine(" *- Barcode Type: " + barcode.SearchCodes); 
         Console.WriteLine("  - Barcode Data: " + barcode.GetData()); 
      } 
   } 
 
   /// This method prints the Omr field results 
   public void PrintOutOmr(OmrFormField field) 
   { 
      Console.WriteLine(); 
      Console.WriteLine("*** Omr Field"); 
      OmrFormFieldResult result = field.Result as OmrFormFieldResult; 
      Console.WriteLine(" Is Checked?[1 = yes, 0 = No] = " + result.Text); 
   } 
 
   /// This method prints the text field results 
   public void PrintOutText(TextFormField field) 
   { 
      Console.WriteLine(); 
      Console.WriteLine("*** Text Field"); 
      TextFormFieldResult result = field.Result as TextFormFieldResult; 
      Console.WriteLine("Text: " + result.Text); 
   } 
 
   /// This method prints the processing results 
   public void PrintOutResults(MyForm form) 
   { 
      if(form.Result.Confidence < 30) 
      { 
         Console.WriteLine("No match found"); 
         return; 
      } 
      Console.WriteLine("=======================Type==========================="); 
      Console.WriteLine("Form Type is: " + form.Master.Properties.Name); 
      Console.WriteLine("Confidance = " + form.Result.Confidence); 
      Console.WriteLine(); 
      Console.WriteLine("====================Fieds Result======================="); 
 
      for(int pageIndex = 0; pageIndex < form.ProcessingPages.Count; pageIndex++) 
      { 
         Console.WriteLine(); 
         Console.WriteLine("=====Page # " + form.ProcessingPages[pageIndex].PageNumber + "====="); 
         Console.WriteLine(); 
         foreach(FormField field in form.ProcessingPages[pageIndex]) 
         { 
            if(field is ImageFormField) 
               PrintOutImage(field as ImageFormField); 
            else if(field is BarcodeFormField) 
               PrintOutBarcode(field as BarcodeFormField); 
            else if(field is OmrFormField) 
               PrintOutOmr(field as OmrFormField); 
            else if(field is TextFormField) 
               PrintOutText(field as TextFormField); 
         } 
      } 
   } 
 
   //=========================================================================== 
   ///This method starts up the Ocr engine 
   private void StartUpOcrEngine() 
   { 
      try 
      { 
         FormsOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false); 
         FormsOcrEngine.Startup(FormsCodec, System.IO.Path.GetTempPath(), ""); 
      } 
      catch(Exception exp) 
      { 
         Console.WriteLine(exp.Message); 
      } 
   } 
 
   ///This method shuts down the Ocr engine 
   private void ShutDownOcrEngine() 
   { 
      FormsOcrEngine.Shutdown(); 
      FormsOcrEngine.Dispose(); 
   } 
 
   ///This method starts up the Barcode engine 
   private void StartUpBarcodeEngine() 
   { 
      try 
      { 
         BarcodeEngine.Startup(BarcodeMajorTypeFlags.Barcodes1d | BarcodeMajorTypeFlags.Barcodes2dRead | BarcodeMajorTypeFlags.BarcodesDatamatrixRead | BarcodeMajorTypeFlags.BarcodesPdfRead | BarcodeMajorTypeFlags.BarcodesQrRead); 
         FormsBarcodeEngine = new BarcodeEngine(); 
      } 
      catch(Exception exp) 
      { 
         Console.WriteLine(exp.Message); 
      } 
   } 
 
   ///This method shuts down the Barcode engine 
   private void ShutDownBarcodeEngine() 
   { 
      BarcodeEngine.Shutdown(); 
   } 
 
   ///This method sets the object managers. 
   public void SetObjectManagers(bool enableDefault, bool enableOcr, bool enableBarcode) 
   { 
      if(RecognitionEngine == null) 
         return; 
      if(enableDefault) 
      { 
         DefaultObjectsManager defaultObjectManager = new DefaultObjectsManager(); 
         RecognitionEngine.ObjectsManagers.Add(defaultObjectManager); 
      } 
 
      if(enableOcr) 
      { 
         OcrObjectsManager ocrObejectManager = new OcrObjectsManager(FormsOcrEngine); 
         ocrObejectManager.Engine = FormsOcrEngine; 
         RecognitionEngine.ObjectsManagers.Add(ocrObejectManager); 
      } 
 
      if(enableBarcode) 
      { 
         BarcodeObjectsManager barcodeObjectManager = new BarcodeObjectsManager(FormsBarcodeEngine); 
         barcodeObjectManager.Engine = FormsBarcodeEngine; 
         RecognitionEngine.ObjectsManagers.Add(barcodeObjectManager); 
      } 
   } 
 
   ///This method starts up all engines. 
   public void StartUpEngines() 
   { 
      StartUpOcrEngine(); 
      StartUpBarcodeEngine(); 
      RecognitionEngine = new FormRecognitionEngine(); 
      SetObjectManagers(false, true, false); 
      ProcessingEngine = new FormProcessingEngine(); 
      ProcessingEngine.OcrEngine = FormsOcrEngine; 
      ProcessingEngine.BarcodeEngine = FormsBarcodeEngine; 
   } 
 
   ///This method shuts down all engines. 
   public void ShutDownEngines() 
   { 
      ShutDownOcrEngine(); 
      ShutDownBarcodeEngine(); 
   } 
 
   ///This method starts up RasterCodec, intialize Form Codec, starts up all engines, and Build the form set 
   public void StartUp() 
   { 
      //1) 
      RasterCodecs.Startup(); 
      FormsCodec = new RasterCodecs(); 
      //2) 
      StartUpEngines(); 
      //3) 
      RecognizeFirstPageOnly = false; 
      LoadFromScanner = false; 
      //4) 
      BuildFormsSet(); 
   } 
 
   ///This method shuts down engines, dispose all images, and shut down the RasterCodec. 
   public void ShutDown() 
   { 
      //1) 
      ShutDownEngines(); 
      //2) 
      foreach(MasterForm master in FormSet) 
         master.Image.Dispose(); 
      //3) 
      FormsCodec.Dispose(); 
      RasterCodecs.Shutdown(); 
   } 
 
 
   /// Here all starts for to recognize and process the unknown form that is stored in a fiel with passed fileName. 
   public void Run(string fileName) 
   { 
      MyForm form = new MyForm(); 
      form.FileName = fileName; 
      RunFormRecognitionAndProcessing(form); 
      if(form.Image != null) 
         form.Image.Dispose(); 
   } 

 
class Program 

   /// The main entry to the program. 
   static void Main(string[] args) 
   { 
      if(args == null || args.Length == 0) 
         return; 
 
      TestForms test = new TestForms(); 
 
      test.StartUp(); 
 
      test.Run(args[0]); 
 
      test.ShutDown(); 
   } 
}

Inheritance Hierarchy

System.Object
   Leadtools.Forms.Processing.FormProcessingEngine

Requirements

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

See Also

Leadtools.Forms requires a Forms Module license and unlock key. For more information, refer to: Imaging Pro/Document/Medical Features