/// <summary> /// Read Text from Image and Store in Text File /// </summary> /// <param name="ImagePath">specify the Image Path</param> /// <param name="StoreTextFilePath">Specify the Store Text File</param> private static void ReadTextFromImage(String ImagePath, String StoreTextFilePath) { try { // Grab Text From Image MODI.Document ModiObj = new MODI.Document(); ModiObj.Create(ImagePath); ModiObj.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); //Retrieve the text gathered from the image MODI.Image ModiImageObj = (MODI.Image)ModiObj.Images[0]; // Store Image Content in Text File FileStream CreateFileObj = new FileStream(StoreTextFilePath, FileMode.Create); //save the image text in the text file StreamWriter WriteFileObj = new StreamWriter(CreateFileObj); WriteFileObj.Write(ModiImageObj.Layout.Text); WriteFileObj.Close(); ModiObj.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }
/* * public SubtitleImage GetImage() * { * Bitmap b = GetBitmap(); * Debugger.Print(Scan()); * return new SubtitleImage(GetBitmap()); * }*/ public string Scan() { string bitmapName = Path.GetTempPath() + "suprip_temp.png"; MODI.Document md = new MODI.Document(); GetBitmap().Save(bitmapName); md.Create(bitmapName); md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); MODI.Image image = (MODI.Image)md.Images[0]; MODI.Layout layout = image.Layout; string scanned = ""; ocrWords = new List <OcrWord>(); for (int j = 0; j < layout.Words.Count; j++) { // Get this word and deal with it. MODI.Word word = (MODI.Word)layout.Words[j]; OcrWord w = new OcrWord(word.RecognitionConfidence, word.Text); ocrWords.Add(w); string text = word.Text; scanned += text + " "; int rc = word.RecognitionConfidence; } md.Close(false); return(scanned); }
private string ReadImage(string imagePath) { string imageText = string.Empty; try { // Grab Text From Image MODI.Document ModiObj = new MODI.Document(); ModiObj.Create(imagePath); ModiObj.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); //Retrieve the text gathered from the image MODI.Image ModiImageObj = (MODI.Image)ModiObj.Images[0]; //System.Console.WriteLine(ModiImageObj.Layout.Text); imageText = ModiImageObj.Layout.Text; ModiObj.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } return imageText; }
public string Analyse() { string texts = ""; if (_MODIDocument == null) { return(texts); } try { // add event handler for progress visualisation //_MODIDocument.OnOCRProgress += new MODI._IDocumentEvents_OnOCRProgressEventHandler(this.ShowProgress); // the MODI call for OCR // _MODIDocument.OCR(_MODIParameters.Language,_MODIParameters.WithAutoRotation,_MODIParameters.WithStraightenImage); _MODIDocument.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); //this.textBox1.Text = (_MODIDocument.Images[0] as MODI.Image).Layout.Text; //MessageBox.Show((_MODIDocument.Images[0] as MODI.Image).Layout.Text); texts = (_MODIDocument.Images[0] as MODI.Image).Layout.Text; //statusBar1.Text = "Ready."; _MODIDocument.Close(false); _MODIDocument = null; } catch (Exception ee) { // simple exception "handling" //MessageBox.Show(@""+ee.Message+@""+ee.ToString()+@""); } return(texts); }
public override string OCR(string filename) { MODI.Document doc = new MODI.Document(); doc.Create(filename); MODI.Image img = (MODI.Image)doc.Images[0]; img.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); var text = img.Layout.Text; doc.Close(); return text; }
public static string getTextBitmap(string nameImage) { string text = null; try { string file = Path.GetFullPath(nameImage + ".jpg"); MODI.Document md = new MODI.Document(); md.Create(file); md.OCR(MODI.MiLANGUAGES.miLANG_SPANISH, false, false); MODI.Image image = (MODI.Image)md.Images[0]; MODI.Layout layout = image.Layout; text = image.Layout.Text.Trim().Replace("\r\n", ""); md.Close(false); }catch(Exception e){ text = null; } return text; }
public static string getTextBitmap(string nameImage) { string text = null; try { string file = Path.GetFullPath(nameImage + ".jpg"); MODI.Document md = new MODI.Document(); md.Create(file); md.OCR(MODI.MiLANGUAGES.miLANG_SPANISH, false, false); MODI.Image image = (MODI.Image)md.Images[0]; MODI.Layout layout = image.Layout; text = image.Layout.Text.Trim().Replace("\r\n", ""); md.Close(false); }catch (Exception e) { text = null; } return(text); }
private static void ReadTextFromImage(String ImagePath) { try { // Grab Text From Image MODI.Document ModiObj = new MODI.Document(); ModiObj.Create(ImagePath); ModiObj.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); //Retrieve the text gathered from the image MODI.Image ModiImageObj = (MODI.Image)ModiObj.Images[0]; System.Console.WriteLine(ModiImageObj.Layout.Text); ModiObj.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } }
private void pickConvertButton_Click(object sender, EventArgs e) { openFileDialog1.FileName = ""; openFileDialog1.Filter = "Image|*.jpg; *.png; *.bmp; *.jpeg; *.PNG;"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { pictureBox1.Image = Image.FromFile(openFileDialog1.FileName.ToString()); } try { MODI.Document doc = new MODI.Document(); doc.Create(openFileDialog1.FileName.ToString()); if (comboBox1.SelectedIndex == 0) { doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); } else if (comboBox1.SelectedIndex == 1) { doc.OCR(MODI.MiLANGUAGES.miLANG_GERMAN, true, true); } else { doc.OCR(MODI.MiLANGUAGES.miLANG_TURKISH, true, true); } foreach (MODI.Image p in doc.Images) { MODI.Layout txt = p.Layout; richTextBox1.Text = txt.Text; } doc.Close(); } catch (Exception errorMes) { MessageBox.Show(errorMes.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 识别率不是很高,微软的 /// </summary> /// <param name="imgfileName"></param> /// <returns></returns> private string OcrFunc(string imgfileName) // 参数为包含文本的图片文件名 { var langs = MODI.MiLANGUAGES.miLANG_ENGLISH; // MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED; 中文含英文 // MODI.MiLANGUAGES.miLANG_JAPANESE; 日文含英文 var doc = new MODI.Document(); var image = default(MODI.Image); var layout = default(MODI.Layout); try { doc.Create(imgfileName); doc.OCR(langs, true, true); var sb = new StringBuilder(); for (int i = 0; i < doc.Images.Count; i++) { image = (MODI.Image)doc.Images[i]; layout = image.Layout; sb.AppendLine(string.Format("{0}, {1}", i, layout.Text)); } doc.Close(false); return(sb.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); return(string.Empty); } finally { layout = null; image = null; doc = null; } }
// Save to MDI / TIFF private static void SaveToDoc(IList<int> pages, string modiFile, string outFile, MODI.MiFILE_FORMAT format, WaitForm wait, string tmpDir, bool closeForm=true) { MODI.Document modiDoc = null; try { wait.Status = "Saving to " + Path.GetFileName(outFile) + "..."; modiDoc = new MODI.Document(); modiDoc.Create(modiFile); // Can't find a way to add images to an empty doc... // doc.Images.Add doesn't like images from other docs // Probably the same bug that doesn't allow access to Image.Picture // This works backwards and removes unselected images from a complete doc for (int page = modiDoc.Images.Count - 1; page >= 0; page--) { if (!pages.Contains(page)) modiDoc.Images.Remove(modiDoc.Images[page]); } modiDoc.SaveAs(outFile, format); //modiDoc.Close(false); // closing still keeps the file locked... see below } catch { wait.MessageSafe("Failed to save " + Path.GetFileName(outFile)); if(closeForm) wait.CloseSafe(); } try { // The file gets locked for some reason until the program exits // or another save is made elsewhere // This unlocks the file on 2 of the 3 machines I tested on ... while (modiDoc.Images.Count > 1) // can't save w/ 0 imgs - leave 1 modiDoc.Images.Remove(modiDoc.Images[0]); // Needed new files for batch processing :/ This. Is. Ugly. // Not to mention slow if (!Directory.Exists(tmpDir)) Directory.CreateDirectory(tmpDir); modiDoc.SaveAs(tmpDir+"\\"+Path.GetRandomFileName()); modiDoc.Close(); if(closeForm) wait.CloseSafe(); } catch { } }
public void Convert(String inputFile, ConversionOptions options) { Object oMissing = Type.Missing; MODI.Document oMdiDoc = null; try { // Create an instance on a MODI document oMdiDoc = new MODI.Document(); // Open the input file oMdiDoc.Create(inputFile); // For some unknown reason I need to do this otherwise pages 2+ // will print tiny, shrunk pages in the top left corner, its either // this or calling SaveAs, this is quicker and easier... for (int i = 0; i < oMdiDoc.Images.Count; i++) { var image = (MODI.Image)oMdiDoc.Images[i]; int tmp = image.PixelHeight; Marshal.ReleaseComObject(image); } using (var printSession = new TiffImagePrinter.PrintSession(options)) { try { oMdiDoc.PrintOut(0, -1, 1, printSession.PrinterName, "", false, MODI.MiPRINT_FITMODES.miPRINT_PAGE); } catch { printSession.Cancel(); throw; } // Wait for the jobs to start spooling to the printer // TODO: make timeout parameterised if (printSession.WaitForJobsSpooling()) { // Now that its started spooling, wait for the job to complete // Don't need to do this as MODI can be closed, this is to ensure the job // completes successfully before returning control to the main thread if (!printSession.WaitForJobsCompleted()) { printSession.Cancel(); throw new TimeoutException("Timeout waiting for print job to complete."); } } else { // The document did not enter the print queue so we need to cancel this // printSession to allow the printer to return to the printer pool. printSession.Cancel(); // Throw a TimeoutException throw new TimeoutException("Timeout waiting for print job to spool."); } } } finally { try { if (oMdiDoc != null) { oMdiDoc.Close(false); oMdiDoc = null; } } catch { // TODO: Log the error here... } } // Force a Garbage Collection GC.Collect(); GC.WaitForPendingFinalizers(); }
/* public SubtitleImage GetImage() { Bitmap b = GetBitmap(); Debugger.Print(Scan()); return new SubtitleImage(GetBitmap()); }*/ public string Scan() { string bitmapName = Path.GetTempPath() + "suprip_temp.png"; MODI.Document md = new MODI.Document(); GetBitmap().Save(bitmapName); md.Create(bitmapName); md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); MODI.Image image = (MODI.Image)md.Images[0]; MODI.Layout layout = image.Layout; string scanned = ""; ocrWords = new List<OcrWord>(); for (int j = 0; j < layout.Words.Count; j++) { // Get this word and deal with it. MODI.Word word = (MODI.Word)layout.Words[j]; OcrWord w = new OcrWord(word.RecognitionConfidence, word.Text); ocrWords.Add(w); string text = word.Text; scanned += text + " "; int rc = word.RecognitionConfidence; } md.Close(false); return scanned; }