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