←Select platform

LoadFields(Stream) Method

Summary
Loads field data from the specified stream.

Syntax
C#
C++/CLI
Java
Python
public void LoadFields( 
   Stream stream 
) 
public void loadFields( 
   leadtools.ILeadStream iLeadStream 
) throws java.lang.Exception; 
public: 
void LoadFields(  
   Stream^ stream 
)  
def LoadFields(self,stream): 

Parameters

stream
A System.IO.Stream containing the field data to be loaded.

Example
C#
using Leadtools; 
using Leadtools.Barcode; 
using Leadtools.Codecs; 
using Leadtools.Forms.Common; 
using Leadtools.Ocr; 
using Leadtools.Forms.Processing; 
using Leadtools.Forms.Recognition; 
using Leadtools.Forms.Recognition.Barcode; 
using Leadtools.Forms.Recognition.Ocr; 
 
/// This class contains all Master Form information. 
class MasterForm 
{ 
   public RasterImage Image; 
   public FormRecognitionAttributes Attributes; 
   public FormRecognitionProperties Properties; 
   public FormPages ProcessingPages; 
   public FormRecognitionEngine recognitionEngine = new FormRecognitionEngine(); 
 
   /// This constructor initializes all MasterForm members to zero/null/Empty. 
   public MasterForm() 
   { 
      Image = null; 
      Attributes = recognitionEngine.CreateMasterForm("MasterFormExample", Guid.Empty, 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 = new FormRecognitionAttributes(); 
      Master = new MasterForm(); 
      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 IdentifyForm(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 = IdentifyForm(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 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 = new FormPages(); 
      form.ProcessingPages.AddRange(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("Confidence = " + form.Result.Confidence); 
      Console.WriteLine(); 
      Console.WriteLine("====================Fields 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.LEAD); 
         FormsOcrEngine.Startup(FormsCodec, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
      } 
      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:\LEADTOOLS23\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Forms.Processing Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.