/// <summary> /// Handling of the CaptureTaken "event" from the ICaptureHost /// We do the OCR here! /// </summary> /// <param name="surface">Has the Image and the capture details</param> public string DoOcr(ISurface surface) { var outputSettings = new SurfaceOutputSettings(OutputFormats.bmp, 0, true) { ReduceColors = true, SaveBackgroundOnly = true }; // We only want the background // Force Grayscale output outputSettings.Effects.Add(new GrayscaleEffect()); // Also we need to check the size, resize if needed to 130x130 this is the minimum if (surface.Screenshot.Width < MinWidth || surface.Screenshot.Height < MinHeight) { var addedWidth = MinWidth - surface.Screenshot.Width; if (addedWidth < 0) { addedWidth = 0; } var addedHeight = MinHeight - surface.Screenshot.Height; if (addedHeight < 0) { addedHeight = 0; } IEffect effect = new ResizeCanvasEffect(addedWidth / 2, addedWidth / 2, addedHeight / 2, addedHeight / 2); outputSettings.Effects.Add(effect); } var filePath = ImageOutput.SaveToTmpFile(surface, outputSettings, null); Log.Debug().WriteLine("Saved tmp file to: " + filePath); var text = ""; try { var processStartInfo = new ProcessStartInfo(_ocrCommand, "\"" + filePath + "\" " + _ocrConfiguration.Language + " " + _ocrConfiguration.Orientimage + " " + _ocrConfiguration.StraightenImage) { CreateNoWindow = true, RedirectStandardOutput = true, UseShellExecute = false }; using (var process = Process.Start(processStartInfo)) { if (process != null) { process.WaitForExit(30 * 1000); if (process.ExitCode == 0) { text = process.StandardOutput.ReadToEnd(); } } } } catch (Exception e) { Log.Error().WriteLine(e, "Error while calling Microsoft Office Document Imaging (MODI) to OCR: "); } finally { if (File.Exists(filePath)) { Log.Debug().WriteLine("Cleaning up tmp file: " + filePath); File.Delete(filePath); } } if (string.IsNullOrEmpty(text)) { Log.Info().WriteLine("No text returned"); return(null); } // For for BUG-1884: text = text.Trim(); try { Log.Debug().WriteLine("Pasting OCR Text to Clipboard: {0}", text); // Paste to Clipboard (the Plugin currently doesn't have access to the ClipboardHelper from Greenshot IDataObject ido = new DataObject(); ido.SetData(DataFormats.Text, true, text); Clipboard.SetDataObject(ido, true); } catch (Exception e) { Log.Error().WriteLine(e, "Problem pasting text to clipboard: "); } return(text); }
public string DoOCR(ISurface surface) { string filePath = null; SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(OutputFormat.bmp, 0, true); outputSettings.ReduceColors = true; // We only want the background outputSettings.SaveBackgroundOnly = true; // Force Grayscale output outputSettings.Effects.Add(new GrayscaleEffect()); // Also we need to check the size, resize if needed to 130x130 this is the minimum if (surface.Image.Width < MIN_WIDTH || surface.Image.Height < MIN_HEIGHT) { int addedWidth = MIN_WIDTH - surface.Image.Width; if (addedWidth < 0) { addedWidth = 0; } int addedHeight = MIN_HEIGHT - surface.Image.Height; if (addedHeight < 0) { addedHeight = 0; } IEffect effect = new ResizeCanvasEffect(addedWidth / 2, addedWidth / 2, addedHeight / 2, addedHeight / 2); outputSettings.Effects.Add(effect); } filePath = ImageOutput.SaveToTmpFile(surface, outputSettings, null); LOG.Debug("Saved tmp file to: " + filePath); string text = ""; try { ProcessStartInfo processStartInfo = new ProcessStartInfo(OCR_COMMAND, "\"" + filePath + "\" " + config.Language + " " + config.Orientimage + " " + config.StraightenImage); processStartInfo.CreateNoWindow = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.UseShellExecute = false; using (Process process = Process.Start(processStartInfo)) { process.WaitForExit(30 * 1000); if (process.ExitCode == 0) { text = process.StandardOutput.ReadToEnd(); } } } catch (Exception e) { LOG.Error("Error while calling Microsoft Office Document Imaging (MODI) to OCR: ", e); } finally { if (File.Exists(filePath)) { LOG.Debug("Cleaning up tmp file: " + filePath); File.Delete(filePath); } } if (text == null || text.Trim().Length == 0) { LOG.Info("No text returned"); return(null); } try { LOG.DebugFormat("Pasting OCR Text to Clipboard: {0}", text); // Paste to Clipboard (the Plugin currently doesn't have access to the ClipboardHelper from Greenshot IDataObject ido = new DataObject(); ido.SetData(DataFormats.Text, true, text); Clipboard.SetDataObject(ido, true); } catch (Exception e) { LOG.Error("Problem pasting text to clipboard: ", e); } return(text); }