예제 #1
0
        private void _btnRecognize_Click(object sender, EventArgs e)
        {
            try
            {
                using (WaitCursor wait = new WaitCursor())
                {
                    DocumentFormat format           = _documentFormatSelector.SelectedFormat;
                    String         documentFileName = _lblImageFileName.Text;

                    documentFileName = string.Concat(documentFileName, ".", DocumentWriter.GetFormatFileExtension(format));

                    _ocrPage.Recognize(null);
                    using (IOcrDocument _document = _ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile))
                    {
                        _document.Pages.Add(_ocrPage);
                        _document.Save(documentFileName, format, null);
                    }

                    // Engine will correct the page deskew before AutoZone or Recognize, so we need to load the page again form the engine.
                    _viewer.Image = _ocrPage.GetRasterImage();
                    _viewer.Refresh();
                    UpdateMyControls();

                    // if the "View Final Document" option is checked then no need to show this message since
                    // it will load the saved document file.
                    if (!_cbViewFinalDocument.Checked)
                    {
                        Messager.ShowInformation(this, String.Format("The output document file was saved at ({0})", documentFileName));
                    }
                    else
                    {
                        if (File.Exists(documentFileName))
                        {
                            try
                            {
                                Process.Start(documentFileName);
                            }
                            catch
                            {
                                Messager.ShowError(this, "Unable to open generated results file with external viewing application");
                            }
                        }
                        else
                        {
                            Messager.ShowError(this, "Unable to open generated results file with external viewing application.\nThe system cannot find the file specified");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex.Message);
            }
        }
예제 #2
0
        private void DoSaveDocument(OcrProgressDialog dlg, Dictionary <string, object> args)
        {
            // Perform load and recognize here

            OcrProgressCallback callback = dlg.OcrProgressCallback;

            try
            {
                string documentFileName = args["documentFileName"] as string;
                bool   viewDocument     = (bool)args["viewDocument"];

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

                    PdfDocumentOptions pdfOptions = _ocrDocument.DocumentWriterInstance.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
                    pdfOptions.Producer = "LEAD Technologies, Inc.";
                    pdfOptions.Creator  = "LEADTOOLS PDFWriter";
                    _ocrDocument.DocumentWriterInstance.SetOptions(DocumentFormat.Pdf, pdfOptions);
                    _ocrDocument.Save(documentFileName, DocumentFormat.Pdf, callback);
                }

                // If it has not been canceled, show the final document (if applicable)
                if (!dlg.IsCanceled && viewDocument)
                {
                    Process.Start(documentFileName);
                }
            }
            catch (Exception ex)
            {
                ShowError(ex);
            }
            finally
            {
                if (callback == null)
                {
                    dlg.EndOperation();
                }
            }
        }
예제 #3
0
        private void SaveText()
        {
            if (_imageViewer.HasImage)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Rich Text File (.rtf)|*.rtf|Text (.txt)|*.txt";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    string         ext          = Path.GetExtension(dlg.FileName);
                    DocumentFormat outputFormat = DocumentFormat.Text;
                    switch (ext)
                    {
                    case ".txt":
                        outputFormat = DocumentFormat.Text;
                        break;

                    case ".rtf":
                        outputFormat = DocumentFormat.Rtf;
                        break;
                    }
                    using (IOcrDocument document = _ocrEngine.DocumentManager.CreateDocument())
                    {
                        document.Pages.AddPage(_imageViewer.Image, null);
                        document.Pages.Recognize(null);
                        RtfDocumentOptions rtfOptions = _ocrEngine.DocumentWriterInstance.GetOptions(DocumentFormat.Rtf) as RtfDocumentOptions;
                        if (rtfOptions != null)
                        {
                            rtfOptions.DropObjects = DocumentDropObjects.None;
                            rtfOptions.TextMode    = DocumentTextMode.Framed;
                            _ocrEngine.DocumentWriterInstance.SetOptions(DocumentFormat.Rtf, rtfOptions);
                        }
                        document.Save(dlg.FileName, outputFormat, null);
                    }
                }
            }
        }
예제 #4
0
        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();
        }
예제 #5
0
        private void ThreadProc(object stateInfo)
        {
            WorkItemData data                = (WorkItemData)stateInfo;
            IOcrEngine   ocrEngine           = null;
            bool         passedCriticalStage = false;

            try
            {
                // See if we have canceled
                lock (_abortedLockObject)
                {
                    if (_aborted)
                    {
                        return;
                    }
                }

                string destinationFile = Path.Combine(data.DestinationDirectory, Path.GetFileName(data.SourceFile));

                ocrEngine = data.OcrEngine;

                lock (_abortedLockObject)
                {
                    if (_aborted)
                    {
                        return;
                    }
                }

                // Convert this image file to a document
                string extension = DocumentWriter.GetFormatFileExtension(data.Format);
                destinationFile = string.Concat(destinationFile, ".", extension);
                if (data.Format == DocumentFormat.Ltd && File.Exists(destinationFile))
                {
                    File.Delete(destinationFile);
                }

                string sourceFile = Path.GetFileName(data.SourceFile);

                try
                {
                    // Create a document and add the pages
                    using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile))
                    {
                        // Get the image number of pages
                        int imagePageCount;

                        RasterCodecs codecs = ocrDocument.RasterCodecsInstance;

                        using (CodecsImageInfo imageInfo = codecs.GetInformation(data.SourceFile, true))
                        {
                            long maximumMemorySize            = 42187;
                            IOcrSettingManager settingManager = ocrEngine.SettingManager;

                            // Get the maximum size of the bitmap from the setting
                            if (settingManager.IsSettingNameSupported("Recognition.MaximumPageConventionalMemorySize"))
                            {
                                int maximumConventionalMemorySize = settingManager.GetIntegerValue("Recognition.MaximumPageConventionalMemorySize");
                                maximumMemorySize = (long)maximumConventionalMemorySize * 1024;
                            }

                            SetRecommendedLoadingOptions(codecs, imageInfo, maximumMemorySize);

                            imagePageCount = imageInfo.TotalPages;
                        }

                        // Set the DocumentWriter options
                        using (MemoryStream ms = new MemoryStream(data.DocumentWriterOptions))
                        {
                            ocrDocument.DocumentWriterInstance.LoadOptions(ms);
                        }

                        passedCriticalStage = true;

                        //recognize and add pages
                        for (int pageNumber = 1; pageNumber <= imagePageCount; pageNumber++)
                        {
                            lock (_abortedLockObject)
                            {
                                if (_aborted)
                                {
                                    return;
                                }
                            }

                            var image = codecs.Load(data.SourceFile, pageNumber);

                            using (var ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
                            {
                                ocrPage.Recognize(null);
                                ocrDocument.Pages.Add(ocrPage);
                            }
                        }

                        // Save
                        ocrDocument.Save(destinationFile, data.Format, null);
                    }
                }
                finally
                {
                }

                OnSuccess(destinationFile);
            }
            catch (Exception ex)
            {
                string message;

                if (passedCriticalStage && data.FirstTry)
                {
                    message = string.Format("Error '{0}' while converting file '{1}' (first time, quarantined)", ex.Message, data.SourceFile);
                    AddToQuarantine(data.SourceFile);
                }
                else if (passedCriticalStage && !data.FirstTry)
                {
                    message = string.Format("Error '{0}' while converting file '{1}' (quarantined error)", ex.Message, data.SourceFile);
                }
                else
                {
                    message = string.Format("Error '{0}' while converting file '{1}'", ex.Message, data.SourceFile);
                }

                OnError(message);
            }
            finally
            {
                if (ocrEngine != null && ocrEngine != data.OcrEngine)
                {
                    ocrEngine.Dispose();
                }

                if (Interlocked.Decrement(ref _workItemCount) == 0)
                {
                    _batchFinishedEvent.Set();
                }
            }
        }
예제 #6
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;
                }
            }
        }