/// <summary> /// 批改操作 /// </summary> public AnswerCard DoCorrect(Stream AnserCardFile) { AnswerCard anserCard=new AnswerCard(); try { //RasterImage crrentImages; // Create the Auto Forms Engine AutoFormsEngine autoEngine; RasterCodecs formsCodec=new RasterCodecs(); //Create a collection of OCR Engines IOcrEngine ocrEngine; //Create the repository of master forms DiskMasterFormsRepository formsRepository; //Create an OCR Engine using (ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false)) { ocrEngine.Startup(formsCodec, null,null, null); // Load the first page as RasterImage RasterImage rasterImage = formsCodec.Load(AnserCardFile); //ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose); // Create an OCR page from this image, transform ownership of the RasterImage object //using (IOcrPage ocrPage = ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose)) //{ // Auto-preprocess it //ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.All, null); // Create an OCR document using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) { // Add this image to the document IOcrPage ocrPage = ocrDocument.Pages.AddPage(rasterImage, null); BarcodeEngine barcodeEngine = new BarcodeEngine(); // Auto-preprocess it ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.All, null); // Point repository to directory with existing master forms formsRepository = new DiskMasterFormsRepository(formsCodec, masterFormsFolder); autoEngine = new AutoFormsEngine(formsRepository, ocrEngine, null, AutoFormsRecognitionManager.Default | AutoFormsRecognitionManager.Ocr, 30, 80, true); // Recognize (Classify) the form AutoFormsRunResult runResult = autoEngine.Run(ocrPage.GetRasterImage(OcrPageType.Current), null, null, null); //AutoFormsRunResult runResult = autoEngine.Run(rasterImage, null, null, null); //AutoFormsRunResult runResult = autoEngine.Run(AnserCardFile, null); if (runResult != null) { //Recognition was successful anserCard = AnswerHandle(runResult); } } autoEngine.Dispose(); formsCodec.Dispose(); //rasterImage.Dispose(); if (ocrEngine != null && ocrEngine.IsStarted) ocrEngine.Shutdown(); } return anserCard; } catch (Exception ex) { return null; } }
校正方案可使用
private void DoDeskew(IOcrEngine ocrEngine, string Filename, string Savename) { // Load the first page as RasterImage RasterImage image = ocrEngine.RasterCodecsInstance.Load(Filename, 1); // 准备命令 DeskewCommand command = new DeskewCommand(); //图像的倾斜校正 command.Flags = DeskewCommandFlags.DeskewImage | DeskewCommandFlags.DoNotFillExposedArea; command.Run(image); // 创建一个RasterCodecs类的新实例 RasterCodecs codecs = new RasterCodecs(); codecs.Options.Jpeg2000.Save.UseSopMarker =true; // 将图像保存 codecs.Save( image, Savename, RasterImageFormat.TifLzw, 1, 1, 1, 1, CodecsSavePageMode.Overwrite); image.Dispose(); codecs.Dispose(); }