示例#1
0
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            // Clean up

            // Save the last setting
            Properties.Settings settings = new Properties.Settings();
            if (_ocrEngine != null)
            {
                settings.OcrEngineType = _ocrEngine.EngineType.ToString();
            }

            settings.Save();

            if (_ocrDocument != null)
            {
                _ocrDocument.Dispose();
                _ocrDocument = null;
            }

            // Dispose the OCR engine (this will call Shutdown as well)
            if (_ocrEngine != null)
            {
                _ocrEngine.Dispose();
                _ocrEngine = null;
            }

            if (_rasterCodecs != null)
            {
                _rasterCodecs.Dispose();
                _rasterCodecs = null;
            }

            base.OnFormClosed(e);
        }
        static void Main(string[] args)
        {
            string licenseFilePath = @"LEADTOOLS.lic";
            string developerKey    = @"mcxvXsdTqZbnbQrDM9FSk5+RAsBJLhAIot2m3qdpoDO8oK7YMWOw1z6YpXqhCnFE";

            RasterSupport.SetLicense(licenseFilePath, developerKey);

            // Assuming you added "using Leadtools.Codecs;", "using Leadtools.Forms.Ocr;" and "using Leadtools.Forms.DocumentWriters;" at the beginning of this class
            // *** Step 1: Select the engine type and create an instance of the IOcrEngine interface.

            // We will use the LEADTOOLS OCR Advantage engine and use it in the same process
            IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false);

            // *** Step 2: Startup the engine.

            // Use the default parameters
            ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrProfessionalRuntime64");

            // *** Step 3: Create an OCR document with one or more pages.

            IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument();

            // Add all the pages of a multi-page TIF image to the document
            ocrDocument.Pages.AddPages(@"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.tif", 1, -1, null);

            // *** Step 4: Establish zones on the page(s), either manually or automatically

            // Automatic zoning
            ocrDocument.Pages.AutoZone(null);

            // *** Step 5: (Optional) Set the active languages to be used by the OCR engine

            // Enable English and German languages
            ocrEngine.LanguageManager.EnableLanguages(new string[] { "en", "de" });

            // *** Step 6: (Optional) Set the spell checking engine

            // Enable the spell checking system
            ocrEngine.SpellCheckManager.SpellCheckEngine = OcrSpellCheckEngine.Native;

            // *** Step 7: (Optional) Set any special recognition module options

            // Change the zone method for the first zone in the first page to be Graphics so it will not be recognized
            OcrZone ocrZone = ocrDocument.Pages[0].Zones[0];

            ocrZone.ZoneType = OcrZoneType.Text;
            ocrDocument.Pages[0].Zones[0] = ocrZone;

            // *** Step 8: Recognize

            ocrDocument.Pages.Recognize(null);

            // *** Step 9: Save recognition results

            // Save the results to a PDF file
            ocrDocument.Save(@"C:\Users\Public\Documents\LEADTOOLS Images\Document.pdf", DocumentFormat.Pdf, null);
            ocrDocument.Dispose();

            // *** Step 10: Shut down the OCR engine when finished
            ocrEngine.Shutdown();
            ocrEngine.Dispose();
        }
示例#3
0
        private void DoLoadAndRecognizeDocument(OcrProgressDialog dlg, Dictionary <string, object> args)
        {
            // Perform load and recognize here

            OcrProgressCallback callback    = dlg.OcrProgressCallback;
            IOcrDocument        ocrDocument = null;

            try
            {
                string documentFileName = args["documentFileName"] as string;

                ocrDocument = _ocrEngine.DocumentManager.CreateDocument("", OcrCreateDocumentOptions.InMemory);

                IOcrPage ocrPage = null;

                if (!dlg.IsCanceled)
                {
                    // If we are not using a progress bar, update the description text
                    if (callback == null)
                    {
                        dlg.UpdateDescription("Loading the document (first page only)...");
                    }

                    ocrPage = ocrDocument.Pages.AddPage(documentFileName, callback);
                }

                if (!dlg.IsCanceled)
                {
                    // If we are not using a progress bar, update the description text
                    if (callback == null)
                    {
                        dlg.UpdateDescription("Recognizing the page(s) of the document...");
                    }

                    ocrPage.Recognize(callback);
                }

                if (!dlg.IsCanceled)
                {
                    // We did not cancel, use this document
                    SetDocument(ocrDocument, documentFileName);
                    ocrDocument = null;
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                if (callback == null)
                {
                    dlg.EndOperation();
                }

                // Clean up
                if (ocrDocument != null)
                {
                    ocrDocument.Dispose();
                }
            }
        }
示例#4
0
        private void DoWork()
        {
            if (!DemosGlobal.CheckKnown3rdPartyTwainIssues(this, _twainSession.SelectedSourceName()))
            {
                _canceled    = true;
                DialogResult = DialogResult.Cancel;
                return;
            }

            // Create an OCR document
            // Acquire the page(s)
            // Deskew the page
            // Add the pages to the engine
            // Recognize
            // Save to final document

            _lblProcessing.Text = "Acquiring a page...";

            _canceled = false;

            _twainSession.AcquirePage += new EventHandler <TwainAcquirePageEventArgs>(_twainSession_AcquirePage);

            try
            {
                if (!_canceled)
                {
                    DialogResult res = _twainSession.Acquire(TwainUserInterfaceFlags.Show);
                    if (res != DialogResult.OK && _document.Pages.Count <= 0)
                    {
                        _canceled = true;
                    }
                }

                if (_document.Pages.Count > 0)
                {
                    // We have the pages in the OCR engine, recognize them
                    if (!_canceled)
                    {
                        _document.Pages.Recognize(new OcrProgressCallback(OcrProgress));
                    }
                    if (!_canceled)
                    {
                        _document.Save(_documentFileName, _format, new OcrProgressCallback(OcrProgress));
                    }

                    // Show the final document
                    if (!_canceled && File.Exists(_documentFileName))
                    {
                        Process.Start(_documentFileName);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                // Unhook from the twain events
                _twainSession.AcquirePage -= new EventHandler <TwainAcquirePageEventArgs>(_twainSession_AcquirePage);

                // Remove all the pages from the document
                _document.Pages.Clear();
                _document.Dispose();

                if (!_canceled)
                {
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
        }