public FilledForm()
 {
     _name       = null;
     _attributes = null;
     _master     = null;
     _result     = null;
     _alignment  = null;
 }
示例#2
0
        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;
            }
        }