private void Startup() { Properties.Settings settings = new Properties.Settings(); string engineType = settings.OcrEngineType; // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { if (dlg.ShowDialog(this) == DialogResult.OK) { _ocrEngine = dlg.OcrEngine; _ocrEngineType = dlg.SelectedOcrEngineType; _ocrEngineSettings.SetEngine(_ocrEngine); // Add the selected engine name to the demo caption Text = Text + " [" + _ocrEngineType.ToString() + " Engine]"; } else { // Close the demo Close(); } } }
private void Startup() { Properties.Settings settings = new Properties.Settings(); string engineType = settings.OcrEngineType; // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { dlg.RasterCodecsInstance = _codecs; if (dlg.ShowDialog(this) == DialogResult.OK) { _ocrEngine = dlg.OcrEngine; _ocrEngineType = dlg.SelectedOcrEngineType; // Add the selected engine name to the demo caption Text = Text + " [" + _ocrEngineType.ToString() + " Engine]"; StartupEngine(); //load default image LoadImage(true); string message = String.Format(@"To use this demo: {0} 1) Load an image with text on it. {0} 2) Draw a rectangle using the mouse around the portion of text you want to recognize.", Environment.NewLine); MessageBox.Show(message, "Instructions"); UpdateMyControls(); } else { // Close the demo Close(); } } }
private void Startup() { Properties.Settings settings = new Properties.Settings(); // Show the OCR engine selection dialog to startup the OCR engine string engineType = settings.OcrEngineType; using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { // Use the same RasterCodecs instance in the OCR engine dlg.RasterCodecsInstance = _rasterCodecs; if (dlg.ShowDialog(this) == DialogResult.OK) { _ocrEngine = dlg.OcrEngine; if (_ocrEngine.SettingManager.IsSettingNameSupported("Recognition.SpaceIsValidCharacter")) { _ocrEngine.SettingManager.SetBooleanValue("Recognition.SpaceIsValidCharacter", false); } Text = string.Format("{0} [{1} Engine]", Messager.Caption, _ocrEngine.EngineType.ToString()); // Load the default document string defaultDocumentFile; if (_ocrEngine.EngineType == OcrEngineType.OmniPageArabic) { defaultDocumentFile = Path.Combine(DemosGlobal.ImagesFolder, "ArabicSample.tif"); } else { defaultDocumentFile = Path.Combine(DemosGlobal.ImagesFolder, "ocr1.tif"); } if (File.Exists(defaultDocumentFile)) { OpenDocument(defaultDocumentFile); } UpdateUIState(); } else { // Close the demo Close(); } } }
private void Startup() { DemoOptions options = DemoOptions.LoadDefault(); // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, options.OcrEngineType.ToString(), false)) { if (dlg.ShowDialog(this) == DialogResult.OK) { _docWriter = new DocumentWriter(); _gatherInformationControl.Init(dlg.SelectedOcrEngineType, _docWriter, options, 1); // Add the selected engine name to the demo caption Text = Text + " [" + dlg.SelectedOcrEngineType.ToString() + " Engine]"; } else { // Close the demo Close(); } } }
private void StartUpOcrEngine() { try { Properties.Settings settings = new Properties.Settings(); string engineType = settings.OcrEngineType; // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, false)) { if (dlg.ShowDialog(this) == DialogResult.OK) { ocrEngine = OcrEngineManager.CreateEngine(dlg.SelectedOcrEngineType, false); ocrEngine.Startup(null, null, null, null); if (ocrEngine.EngineType == OcrEngineType.LEAD) { if (ocrEngine.SettingManager.IsSettingNameSupported("Recognition.RecognitionModuleTradeoff")) { ocrEngine.SettingManager.SetEnumValue("Recognition.RecognitionModuleTradeoff", "Accurate"); } } this.Text = String.Format("{0} [{1} Engine]", this.Text, dlg.SelectedOcrEngineType.ToString()); } else { throw new Exception("No engine selected."); } } } catch (Exception exp) { Messager.ShowError(this, exp); throw; } }
private void Startup() { Properties.Settings settings = new Properties.Settings(); string engineType = settings.OcrEngineType; // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { dlg.RasterCodecsInstance = _codecs; if (dlg.ShowDialog(this) == DialogResult.OK) { _ocrEngine = dlg.OcrEngine; _ocrEngineType = dlg.SelectedOcrEngineType; InitEngines(); } else { // Close the demo Close(); } } }
protected override void OnLoad(EventArgs e) { if (!DesignMode) { _documentFormatSelector = new DocumentFormatSelector(); _documentFormatSelector.Dock = DockStyle.Fill; _documentFormatsSelectorPanel.Controls.Add(_documentFormatSelector); _documentFormatSelector.BringToFront(); _documentFormatSelector.SelectedFormatChanged += new EventHandler <EventArgs>(_documentFormatSelector_SelectedFormatChanged); Text = Messager.Caption; Properties.Settings settings = new Properties.Settings(); // Show the OCR engine selection dialog to startup the OCR engine string engineType = settings.OcrEngineType; if (JobData.OcrEngine == null) { using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { // Use the same RasterCodecs instance in the OCR engine dlg.RasterCodecsInstance = _rasterCodecs; if (dlg.ShowDialog(this) != DialogResult.OK) { // Close the demo DialogResult = DialogResult.Cancel; Close(); return; } else { JobData.OcrEngine = dlg.OcrEngine; } } } Text = string.Format("{0} [{1} Engine]", Messager.Caption, JobData.OcrEngine.EngineType.ToString()); _imageFileNameTextBox.Text = settings.ImageFileName; _imageFirstPageNumber = settings.FirstPageNumber; if (_imageFirstPageNumber < 1) { _imageFirstPageNumber = 1; } _imageLastPageNumber = settings.LastPageNumber; if (_imageLastPageNumber < _imageFirstPageNumber) { _imageLastPageNumber = -1; } InitFormats(settings); _zonesFileNameTextBox.Text = settings.ZonesFileName; try { _maximumThreadsPerJobTextBox.Text = settings.MaximumThreadsPerJob.ToString(); } catch { _maximumThreadsPerJobTextBox.Text = "0"; } try { _maximumPagesBeforeLtdTextBox.Text = settings.MaximumPagesBeforeLtd.ToString(); } catch { _maximumPagesBeforeLtdTextBox.Text = "4"; } if (!JobData.OcrEngine.AutoRecognizeManager.IsMultiThreadedSupported) { _maximumThreadsPerJobLabel.Visible = false; _maximumThreadsPerJobTextBox.Visible = false; _maximumThreadsPerJobInfoLabel.Text = "Multi-threaded is not supported in this engine"; } _preprocessingComboBox.Items.Add("None"); foreach (OcrAutoPreprocessPageCommand command in Enum.GetValues(typeof(OcrAutoPreprocessPageCommand))) { _preprocessingComboBox.Items.Add(command.ToString()); } _preprocessingComboBox.SelectedItem = "None"; if (!string.IsNullOrEmpty(settings.Preprocessing)) { if (string.Compare(settings.Preprocessing, "none", true) == 0) { _preprocessingComboBox.SelectedItem = "None"; } else { try { OcrAutoPreprocessPageCommand command = (OcrAutoPreprocessPageCommand)Enum.Parse(typeof(OcrAutoPreprocessPageCommand), settings.Preprocessing); _preprocessingComboBox.SelectedItem = command.ToString(); } catch { } } } _continueOnErrorCheckBox.Checked = settings.ContinueOnRecoverableErrors; _enableTraceCheckBox.Checked = settings.EnableTrace; _viewFinalDocumentCheckBox.Checked = settings.ViewFinalDocument; UpdateMyControls(); _loadedOk = true; } base.OnLoad(e); }
private bool LoadSettings() { Properties.Settings settings = new Properties.Settings(); string engineType = settings.OcrEngineType; // Show the engine selection dialog using (OcrEngineSelectDialog dlg = new OcrEngineSelectDialog(Messager.Caption, engineType, true)) { if (dlg.ShowDialog(this) == DialogResult.OK) { _ocrEngine = dlg.OcrEngine; _ocrEngineType = dlg.SelectedOcrEngineType; RasterCodecs codecs = _ocrEngine.RasterCodecsInstance; #if !LEADTOOLS_V175_OR_LATER codecs.Options.Pdf.Load.XResolution = 300; codecs.Options.Pdf.Load.YResolution = 300; codecs.Options.RasterizeDocument.Load.Enabled = true; codecs.Options.Load.AutoFixImageResolution = true; #endif #if LEADTOOLS_V16_OR_LATER // Use the new RasterizeDocumentOptions to default loading document files at 300 DPI codecs.Options.RasterizeDocument.Load.XResolution = 300; codecs.Options.RasterizeDocument.Load.YResolution = 300; codecs.Options.Pdf.Load.EnableInterpolate = true; codecs.Options.Load.AutoFixImageResolution = true; #endif // #if LEADTOOLS_V16_OR_LATER } else { return(false); } } UpdateDocumentFormats(); string twainSourceName = settings.TwainSourceName; if (!string.IsNullOrEmpty(twainSourceName)) { try { _twainSession.SelectSource(twainSourceName); } catch { } } _tbFinalDocumentFileName.Text = settings.DocumentFileName.Trim(); if (string.IsNullOrEmpty(_tbFinalDocumentFileName.Text)) { _tbFinalDocumentFileName.Text = Path.Combine(Path.GetFullPath(DemosGlobal.ImagesFolder), "OcrTwainScanningDemo"); } SelectFormatByName(settings.DocumentFormat); _documentFormatSelector_SelectedFormatChanged(null, EventArgs.Empty); return(true); }
private static int Run(string preferencesFileName) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Messager.Caption = "Document Converter Demo"; // Initialize Trace Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Console.WriteLine("LEADTOOLS " + Messager.Caption); DocumentConverterPreferences preferences; ConvertRedactionOptions convertRedactionOptions = null; if (!string.IsNullOrEmpty(preferencesFileName)) { // Load it from the file specified by the user preferences = DocumentConverterPreferences.Load(preferencesFileName); preferences.IsSilentMode = true; } else { // Load the preferences file DocumentConverterPreferences.DemoName = "Document Converter Demo"; DocumentConverterPreferences.XmlFileName = "DocumentConverterDemo"; preferences = DocumentConverterPreferences.Load(); preferences.IsSilentMode = false; } if (!Support.SetLicense(preferences.IsSilentMode)) { if (preferences.IsSilentMode) { throw new Exception("Your license file is missing, invalid or expired."); } return(-1); } // Create the rendering engine try { if (preferences.AnnRenderingEngine == null) { preferences.AnnRenderingEngine = new AnnWinFormsRenderingEngine(); preferences.AnnRenderingEngine.Resources = Tools.LoadResources(); } } catch { } if (!preferences.IsSilentMode) { // Show the OCR engine selection dialog to startup the OCR engine Trace.WriteLine("Starting OCR engine"); var engineType = preferences.OCREngineType; using (var dlg = new OcrEngineSelectDialog(DocumentConverterPreferences.DemoName, engineType.ToString(), true)) { dlg.AllowNoOcr = true; dlg.AllowNoOcrMessage = "The demo runs without OCR functionality but you will not be able to parse text from non-document files such as TIFF or Raster PDF. Click 'Cancel' to start this demo without an OCR engine."; if (dlg.ShowDialog() == DialogResult.OK) { preferences.OcrEngineInstance = dlg.OcrEngine; preferences.OCREngineType = dlg.OcrEngine.EngineType; Trace.WriteLine(string.Format("OCR engine {0} started", preferences.OCREngineType)); } } } else { // Initialize the default OCR engine preferences.OcrEngineInstance = InitOcrEngine(preferences); } // Initialize the RasterCodecs instance var rasterCodecs = new RasterCodecs(); rasterCodecs.Options = DocumentFactory.RasterCodecsTemplate.Options.Clone(); preferences.RasterCodecsInstance = rasterCodecs; if (!string.IsNullOrEmpty(preferences.RasterCodecsOptionsPath)) { preferences.RasterCodecsInstance.LoadOptions(preferences.RasterCodecsOptionsPath); } // Initialize the DocumentWriter instance preferences.DocumentWriterInstance = new DocumentWriter(); if (!string.IsNullOrEmpty(preferences.DocumentWriterOptionsPath)) { preferences.DocumentWriterInstance.LoadOptions(preferences.DocumentWriterOptionsPath); } // Cache to use ObjectCache cache = null; // Initialize the cache if (!string.IsNullOrEmpty(preferences.CacheDirectory)) { var fileCache = new FileCache(); fileCache.CacheDirectory = preferences.CacheDirectory; fileCache.DataSerializationMode = preferences.CacheDataSerializationMode; fileCache.PolicySerializationMode = preferences.CachePolicySerializationMode; cache = fileCache; } // Do conversions var more = true; while (more) { Console.WriteLine("Obtaining conversion options"); if (!preferences.IsSilentMode) { // Collect the options using (var dlg = new DocumentConverterDialog()) { dlg.Preferences = preferences.Clone(); dlg.RedactionOptions = convertRedactionOptions; if (dlg.ShowDialog() == DialogResult.OK) { preferences = dlg.Preferences.Clone(); convertRedactionOptions = dlg.RedactionOptions; } else { more = false; } } } if (more) { try { // Save the preferences if (!preferences.IsSilentMode) { preferences.Save(); } // Run the conversion if (preferences.DocumentId != null) { var loadFromCacheOptions = new LoadFromCacheOptions { Cache = cache, DocumentId = preferences.DocumentId, UserToken = preferences.DocumentUserToken }; using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) { if (document == null) { throw new Exception(string.Format("Could not load document with ID '{0}' from the cache", preferences.DocumentId)); } preferences.Run(cache, document, null, convertRedactionOptions); } } else { preferences.Run(null, null, null, convertRedactionOptions); } } catch (Exception ex) { if (!preferences.IsSilentMode) { Messager.ShowError(null, ex.Message); } else { preferences.ErrorMessage = ex.Message; } } } if (more) { if (!preferences.IsSilentMode) { // Ask if user wants to convert another document more = (Messager.ShowQuestion(null, "Convert more?", MessageBoxButtons.YesNo) == DialogResult.Yes); } else { more = false; } } } if (preferences.OcrEngineInstance != null) { preferences.OcrEngineInstance.Dispose(); } if (preferences.RasterCodecsInstance != null) { preferences.RasterCodecsInstance.Dispose(); } if (preferencesFileName != null) { preferences.Save(preferencesFileName); } if (preferences.ErrorMessage != null) { return(1); } return(0); }
static void Main() { if (!Support.SetLicense()) { return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Messager.Caption = "Document Converter Folder Demo"; // Initialize Trace Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Console.WriteLine("LEADTOOLS " + Messager.Caption); // Load the preferences file DocumentConverterPreferences.DemoName = "Document Converter Folder Demo"; DocumentConverterPreferences.XmlFileName = "DocumentConverterFolderDemo"; var preferences = DocumentConverterPreferences.Load(); preferences.OpenOutputDocumentAllowed = false; MyOptions.XmlFileName = "DocumentConverterFolderOptions"; var myOptions = MyOptions.Load(); var runConversion = false; using (var dlg = new MyOptionsDialog()) { dlg.MyOptions = myOptions.Clone(); if (dlg.ShowDialog() == DialogResult.OK) { runConversion = true; myOptions = dlg.MyOptions.Clone(); myOptions.Save(); } } if (!runConversion) { return; } // Initialize OCR engine // Show the OCR engine selection dialog to startup the OCR engine Trace.WriteLine("Starting OCR engine"); var engineType = preferences.OCREngineType; using (var dlg = new OcrEngineSelectDialog(DocumentConverterPreferences.DemoName, engineType.ToString(), true)) { dlg.AllowNoOcr = true; dlg.AllowNoOcrMessage = "The demo runs without OCR functionality but you will not be able to parse text from non-document files such as TIFF or Raster PDF. Click 'Cancel' to start this demo without an OCR engine."; if (dlg.ShowDialog() == DialogResult.OK) { preferences.OcrEngineInstance = dlg.OcrEngine; preferences.OCREngineType = dlg.OcrEngine.EngineType; Trace.WriteLine(string.Format("OCR engine {0} started", preferences.OCREngineType)); } } _handler += new EventHandler(Handler); SetConsoleCtrlHandler(_handler, true); try { // Initialize the RasterCodecs instance var rasterCodecs = new RasterCodecs(); rasterCodecs.Options = DocumentFactory.RasterCodecsTemplate.Options.Clone(); preferences.RasterCodecsInstance = rasterCodecs; // Initialize the DocumentWriter instance preferences.DocumentWriterInstance = new DocumentWriter(); // Get the options Console.WriteLine("Obtaining conversion options"); // Collect the options using (var dlg = new DocumentConverterDialog()) { // Create a dummy document so the options do not bug us about a input/output files using (var document = DocumentFactory.Create("Raster", new CreateDocumentOptions { MimeType = "image/tiff" })) { dlg.InputDocument = document; dlg.Preferences = preferences.Clone(); dlg.InputDocument = document; if (dlg.ShowDialog() == DialogResult.OK) { preferences = dlg.Preferences.Clone(); // Save the preferences preferences.Save(); } else { runConversion = false; } } } if (runConversion) { // Set the RasterCodecs instance, should go into the DocumentFactory class which will be used to load the document if (preferences.RasterCodecsInstance != null) { DocumentFactory.RasterCodecsTemplate = preferences.RasterCodecsInstance; } DocumentConverter converter = new DocumentConverter(); // Set the OCR engine if (preferences.OcrEngineInstance != null) { converter.SetOcrEngineInstance(preferences.OcrEngineInstance, false); } if (preferences.DocumentWriterInstance != null) { converter.SetDocumentWriterInstance(preferences.DocumentWriterInstance); } // Set pre-processing options converter.Preprocessor.Deskew = preferences.PreprocessingDeskew; converter.Preprocessor.Invert = preferences.PreprocessingInvert; converter.Preprocessor.Orient = preferences.PreprocessingOrient; // Enable trace converter.Diagnostics.EnableTrace = preferences.EnableTrace; // Set options converter.Options.JobErrorMode = preferences.ErrorMode; converter.Options.EnableSvgConversion = preferences.EnableSvgConversion; converter.Options.SvgImagesRecognitionMode = (preferences.OcrEngineInstance != null && preferences.OcrEngineInstance.IsStarted) ? preferences.SvgImagesRecognitionMode : DocumentConverterSvgImagesRecognitionMode.Disabled; converter.Diagnostics.EnableTrace = preferences.EnableTrace; try { RunConversion(converter, preferences, myOptions); } catch (Exception ex) { Trace.WriteLine("Error " + ex.Message); } } if (preferences.OcrEngineInstance != null) { preferences.OcrEngineInstance.Dispose(); } if (preferences.RasterCodecsInstance != null) { preferences.RasterCodecsInstance.Dispose(); } _handler -= new EventHandler(Handler); Console.WriteLine("\nDone, Press and key to close demo."); Console.ReadKey(); } finally { _handler -= new EventHandler(Handler); } }