private void DeletePageFromMasterForm(int pagenumber, FormRecognitionAttributes form) { FormRecognitionEngine recognitionEngine = SetupRecognitionEngine(); recognitionEngine.OpenMasterForm(form); recognitionEngine.DeleteMasterFormPage(form, pagenumber); recognitionEngine.CloseMasterForm(form); }
public FormRecognitionAttributes CreateMasterForm(string name) { FormRecognitionOptions options = new FormRecognitionOptions(); FormRecognitionEngine recognitionEngine = SetupRecognitionEngine(); FormRecognitionAttributes attributes = recognitionEngine.CreateMasterForm(name, new Guid(), options); recognitionEngine.CloseMasterForm(attributes); return(attributes); }
internal DiskMasterFormsCategoryExample(IMasterFormsRepository repository, string name, string path, IMasterFormsCategory parent) { _repository = repository; _name = name; _parent = parent; _path = path; _recognitionEngine = new FormRecognitionEngine(); _childCategories = new MasterFormsCategoryCollection(); _masterForms = new MasterFormCollection(); }
public void AddPageToMasterForm(RasterImage image, FormRecognitionAttributes attributes, int pageIndex, PageRecognitionOptions pageOptions) { try { FormRecognitionEngine recognitionEngine = SetupRecognitionEngine(); recognitionEngine.OpenMasterForm(attributes); recognitionEngine.InsertMasterFormPage(pageIndex, attributes, image, pageOptions, null); recognitionEngine.CloseMasterForm(attributes); } catch (Exception ex) { throw; } }
private FormRecognitionEngine SetupRecognitionEngine() { FormRecognitionEngine recognitionEngine = null; IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false); ocrEngine.Startup(null, null, null, null); try { if (recognitionEngine == null) { recognitionEngine = new FormRecognitionEngine(); } //Add appropriate object managers to recognition engine recognitionEngine.ObjectsManagers.Clear(); //if (_menuItemDefaultManager.Checked) //{ // DefaultObjectsManager defaultObjectManager = new DefaultObjectsManager(); // recognitionEngine.ObjectsManagers.Add(defaultObjectManager); //} //if (_menuItemOCRManager.Checked && ocrEngine.IsStarted) //{ OcrObjectsManager ocrObejectManager = new OcrObjectsManager(ocrEngine); ocrObejectManager.Engine = ocrEngine; recognitionEngine.ObjectsManagers.Add(ocrObejectManager); //} //if (_menuItemBarcodeManager.Checked && barcodeEngine != null) //{ // BarcodeObjectsManager barcodeObjectManager = new BarcodeObjectsManager(barcodeEngine); // barcodeObjectManager.Engine = barcodeEngine; // recognitionEngine.ObjectsManagers.Add(barcodeObjectManager); //} } catch (Exception exp) { //Messager.ShowError(this, exp); throw; } return(recognitionEngine); }
private bool StartUpEngines() { try { RecognitionEngine = new FormRecognitionEngine(); ProcessingEngine = new FormProcessingEngine(); StartUpRasterCodecs(); StartUpOcrEngine(); //StartUpBarcodeEngine(); //StartupTwain(); FilledChar = OcrEngine.ZoneManager.OmrOptions.GetStateRecognitionCharacter(OcrOmrZoneState.Filled); UnfilledChar = OcrEngine.ZoneManager.OmrOptions.GetStateRecognitionCharacter(OcrOmrZoneState.Unfilled); return(true); } catch (Exception ex) { logger.Error(ex, $"Error starting engines"); return(false); } }
private void AddMasterFormPages(RasterImage imagesToAdd, DiskMasterForm currentform, string folderName) { try { DiskMasterForm currentMasterForm = currentform; FormRecognitionAttributes attributes = currentMasterForm.ReadAttributes(); FormPages formPages = currentMasterForm.ReadFields(); RasterImage formImage = currentMasterForm.ReadForm(); for (int i = 0; i < imagesToAdd.PageCount; i++) { //Add each new page to the masterform by creating attributes for each page imagesToAdd.Page = i + 1; AddPageToMasterForm(imagesToAdd.Clone(), attributes, -1, null); } //Add image if (formImage != null) { formImage.AddPages(imagesToAdd.CloneAll(), 1, imagesToAdd.PageCount); } else { formImage = imagesToAdd.CloneAll(); } //Only add processing pages for the new pages if (formPages != null) { for (int i = 0; i < imagesToAdd.PageCount; i++) { formPages.Add(new FormPage(formPages.Count + 1, imagesToAdd.XResolution, imagesToAdd.YResolution)); } } else { //No processing pages exist so we must create them FormRecognitionEngine recognitionEngine = SetupRecognitionEngine(); FormProcessingEngine tempProcessingEngine = new FormProcessingEngine(); tempProcessingEngine.OcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false); //tempProcessingEngine.BarcodeEngine = barcodeEngine; for (int i = 0; i < recognitionEngine.GetFormProperties(attributes).Pages; i++) { tempProcessingEngine.Pages.Add(new FormPage(i + 1, imagesToAdd.XResolution, imagesToAdd.YResolution)); } formPages = tempProcessingEngine.Pages; } //FormField newField = null; //AnnHiliteObject newObject = new AnnHiliteObject(); //newField = new TextFormField(); //newField.Name = "test"; //newField.Bounds = new LogicalRectangle(50, 50, 50, 50, LogicalUnit.Pixel); //FormField newField1 = null; //AnnHiliteObject newObject1 = new AnnHiliteObject(); //newField1 = new OmrFormField(); //newField1.Name = "test1"; //newField1.Bounds = new LogicalRectangle(50, 50, 50, 50, LogicalUnit.Pixel); //newObject.Tag = newField; //newObject1.Tag = newField1; //FormField currentField = newObject.Tag as FormField; //FormField currentField1 = newObject1.Tag as FormField; //formPages[0].Add(currentField); //formPages[0].Add(currentField1); currentMasterForm.WriteForm(formImage); currentMasterForm.WriteAttributes(attributes); currentMasterForm.WriteFields(formPages); DBHelper.UpdateTifPageCount(formImage.PageCount.ToString(), folderName); } catch (Exception ex) { throw; } }
public MasterForm(string name, RasterImage image, FormRecognitionAttributes attr, FormRecognitionEngine engine) { _image = image; _attributes = attr; attr.Image = Image; _properties = engine.GetFormProperties(attr); _properties.Name = name; _processingPages = null; _isDirty = false; }
public static void TestOcr2() { string BaseFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); try { string[] masterFileNames = Directory.GetFiles(BaseFolder, "2*.tif", SearchOption.AllDirectories); //Get master form filenames //You may need to update the below path to point to the "Leadtools Images\Forms\MasterForm Sets\OCR" directory. FormRecognitionEngine recognitionEngine = new FormRecognitionEngine(); RasterCodecs codecs; //Create the OCR Engine to use in the recognition IOcrEngine formsOCREngine; codecs = new RasterCodecs(); //Create a LEADTOOLS OCR Module - LEAD Engine and start it formsOCREngine = OcrEngineManager.CreateEngine(OcrEngineType.OmniPage, false); //formsOCREngine.Startup(codecs, null, null, @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"); formsOCREngine.Startup(codecs, null, null, null); //Add an OCRObjectManager to the recognition engines //ObjectManager collection OcrObjectsManager ocrObjectsManager = new OcrObjectsManager(formsOCREngine); ocrObjectsManager.Engine = formsOCREngine; recognitionEngine.ObjectsManagers.Add(ocrObjectsManager); var binFiles = new List <string>(); foreach (string masterFileName in masterFileNames) { string formName = Path.GetFileNameWithoutExtension(masterFileName); //Load the master form image RasterImage image = codecs.Load(masterFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); //Create a new master form FormRecognitionAttributes masterFormAttributes = recognitionEngine.CreateMasterForm(formName, Guid.Empty, null); for (int i = 0; i < image.PageCount; i++) { image.Page = i + 1; //Add the master form page to the recognition engine recognitionEngine.AddMasterFormPage(masterFormAttributes, image, null); } //Close the master form and save it's attributes recognitionEngine.CloseMasterForm(masterFormAttributes); var binFile = formName + "_runtime.bin"; File.WriteAllBytes(binFile, masterFormAttributes.GetData()); binFiles.Add(binFile); } logger.Info("Master Form Processing Complete {0}", "Complete"); //For this tutorial, we will use the sample W9 filled form. //You may need to update the below path to point to "\LEADTOOLS Images\Forms\Forms to be Recognized\OCR\W9_OCR_Filled.tif". if (true) { string formToRecognize = Path.Combine(BaseFolder, "13984799_02.png"); RasterImage image = codecs.Load(formToRecognize, 0, CodecsLoadByteOrder.BgrOrGray, 1, -1); //Load the image to recognize FormRecognitionAttributes filledFormAttributes = recognitionEngine.CreateForm(null); for (int i = 0; i < image.PageCount; i++) { image.Page = i + 1; //Add each page of the filled form to the recognition engine recognitionEngine.AddFormPage(filledFormAttributes, image, null); } recognitionEngine.CloseForm(filledFormAttributes); string resultMessage = "The form could not be recognized"; //Compare the attributes of each master form to the attributes of the filled form foreach (string masterFileName in binFiles) { FormRecognitionAttributes masterFormAttributes = new FormRecognitionAttributes(); masterFormAttributes.SetData(File.ReadAllBytes(masterFileName)); FormRecognitionResult recognitionResult = recognitionEngine.CompareForm(masterFormAttributes, filledFormAttributes, null); //In this example, we consider a confidence equal to or greater //than 90 to be a match if (recognitionResult.Confidence >= 90) { resultMessage = String.Format("This form has been recognized as a {0}", Path.GetFileNameWithoutExtension(masterFileName)); break; } } logger.Info(resultMessage, "Recognition Results"); } } catch (Exception ex) { logger.Info(ex.Message); throw; } }