Exemple #1
0
        private static string DetectAndRecognizeMicr(string fileName)
        {
            try
            {
                //Initialize the codecs class and load the image
                using (var codecs = new RasterCodecs())
                    using (var img = codecs.Load(fileName))
                    {
                        //prepare the MICR detector command and run it
                        var micrDetector = new MICRCodeDetectionCommand
                        {
                            SearchingZone = new LeadRect(0, 0, img.Width, img.Height)
                        };
                        micrDetector.Run(img);

                        //See if there is a MICR detected - if not return
                        if (micrDetector.MICRZone == LeadRect.Empty)
                        {
                            return("No MICR detected in this file.");
                        }

                        //if there is a MICR zone detected startup OCR
                        using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false))
                        {
                            ocrEngine.Startup(null, null, null, null);

                            //create the OCR Page
                            var ocrPage = ocrEngine.CreatePage(img, OcrImageSharingMode.None);

                            //Create the OCR zone for the MICR and add to the Ocr Page and recognize
                            var micrZone = new OcrZone
                            {
                                Bounds   = LogicalRectangle.FromRectangle(micrDetector.MICRZone),
                                ZoneType = OcrZoneType.Micr
                            };
                            ocrPage.Zones.Add(micrZone);
                            ocrPage.Recognize(null);

                            //return the MICR text
                            return(ocrPage.GetText(-1));
                        }
                    }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }