LEADTOOLS Forms (Leadtools.Forms.Processing assembly)
LEAD Technologies, Inc

FormProcessingEngine Class

Example 





Members 
Class that contains the processing engine used to perform field processing.
Object Model
FormProcessingEngine ClassFormPages ClassFormPage Class
Syntax
public class FormProcessingEngine 
'Declaration
 
Public Class FormProcessingEngine 
'Usage
 
Dim instance As FormProcessingEngine
public sealed class FormProcessingEngine 
function Leadtools.Forms.Processing.FormProcessingEngine()
public ref class FormProcessingEngine 
Example
 
''' 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 filename.
   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 filename.
   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(Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.bin"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.xml"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\FFC-107.tif"))
      FormSet.Add(Form107)

      Dim Form180 As MasterForm = LoadMasterForm(Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.bin"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.xml"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-180.tif"))
      FormSet.Add(Form180)

      Dim Form3881 As MasterForm = LoadMasterForm(Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.bin"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.xml"), Path.Combine(LEAD_VARS.ImagesDir, "Forms\MasterForm Sets\OCR\SF-3881.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, Path.Combine(LEAD_VARS.ImagesDir, "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.Symbology)
         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, Nothing, 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
         FormsBarcodeEngine = New BarcodeEngine()
      Catch exp As Exception
         Console.WriteLine(exp.Message)
      End Try
   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()
   End Sub

   '''This method starts up RasterCodec, intialize Form Codec, starts up all engines, and Build the form set
   Public Sub StartUp()
      '1)
      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()
   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

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
/// This class contains all Master Form information.
class MasterForm
{
   public RasterImage Image;
   public FormRecognitionAttributes Attributes;
   public FormRecognitionProperties Properties;
   public FormPages ProcessingPages;
   /// This constructor initializes all MasterForm members to zero/null/Empty.
   public MasterForm()
   {
      Image = null;
      Attributes = null;
      Properties = FormRecognitionProperties.Empty;
      ProcessingPages = null;
   }
}

/// This class contains all MyForm information. MyForm 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 initializes all MyForm members 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 initialize the 
/// OCR and barcode engines in addition to building the Master Forms set. Next, call the Run method to recognize and process a specific form. Finally, call the ShutDown method 
/// 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 a scanner.
   public RasterImage LoadImageScanner(int count)
   {
      //TODO
      return null;
   }

   /// This method loads an image from a scanner for recognition.
   public RasterImage LoadRecognitionImageScanner()
   {
      //TODO
      return null;
   }

   /// This method loads an image from a file.
   public RasterImage LoadImageFile(string fileName, int firstPage, int lastPage)
   {
      // This method loads an image and verifies that all pages have been loaded.
      RasterImage image = FormsCodec.Load(fileName, 1, CodecsLoadByteOrder.Bgr, firstPage, lastPage);
      return image;
   }
   //===========================================================================
   /// This method loads an image from a file for recognition.
   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.
   public RasterImage LoadRecognitionImage(string fileName)
   {
      RasterImage image;
      if(RecognizeFirstPageOnly)
         image = LoadRecognitionImageScanner();
      else
         image = LoadRecognitionImageFile(fileName);
      return image;
   }

   //===========================================================================
   /// This method creates a form attributes object for recognition.
   public FormRecognitionAttributes CreateForm()
   {
      FormRecognitionAttributes attributes = RecognitionEngine.CreateForm(null);
      RecognitionEngine.CloseForm(attributes);
      return attributes;
   }

   /// This method adds a page to a form attributes object 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 a form attributes object for recognition.
   public void DeletePageFromForm(int pagenumber, FormRecognitionAttributes form)
   {
      RecognitionEngine.OpenForm(form);
      RecognitionEngine.DeleteFormPage(form, pagenumber);
      RecognitionEngine.CloseForm(form);
   }

   /// This method creates a form along 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 index that starts from 1 not zero
         AddPageToForm(form.Image, form.Attributes);
      }
      form.Image.Page = saveCurrentPageIndex;
   }

   //===========================================================================
   /// This method creates a Master Form recognition attributes object.
   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 object.
   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 object.
   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 an 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 loadd 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 alignment 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 object, 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 the master form attributes object to the specified filename.
   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 filename.
   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(
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.bin"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.xml"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\FFC-107.tif"));
      FormSet.Add(Form107);

      MasterForm Form180 = LoadMasterForm(
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.bin"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.xml"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-180.tif"));
      FormSet.Add(Form180);

      MasterForm Form3881 = LoadMasterForm(
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.bin"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.xml"),
         Path.Combine(LEAD_VARS.ImagesDir, @"Forms\MasterForm Sets\OCR\SF-3881.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, Path.Combine(LEAD_VARS.ImagesDir, "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.Symbology);
         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, null, 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
      {
         FormsBarcodeEngine = new BarcodeEngine();
      }
      catch(Exception exp)
      {
         Console.WriteLine(exp.Message);
      }
   }

   ///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();
   }

   ///This method starts up a RasterCodec object, initializes a Form Codec, starts up all engines, and builds the form set.
   public void StartUp()
   {
      //1)
      FormsCodec = new RasterCodecs();
      //2)
      StartUpEngines();
      //3)
      RecognizeFirstPageOnly = false;
      LoadFromScanner = false;
      //4)
      BuildFormsSet();
   }

   ///This method shuts down all engines, dispose of all images, and shuts down the RasterCodec.
   public void ShutDown()
   {
      //1)
      ShutDownEngines();
      //2)
      foreach(MasterForm master in FormSet)
         master.Image.Dispose();
      //3)
      FormsCodec.Dispose();
   }


   /// This method starts the recognition process on the unknown form that is stored in a file with the 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();
   }
}

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

FormProcessingEngine Members
Leadtools.Forms.Processing Namespace

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.

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