2012-06-14 16 views
5

Tôi có tài liệu được quét dưới dạng ảnh .jpg trong một thư mục và tôi muốn làm OCR trong C# serially cho từng tài liệu của tôi trong thư mục đó. cho đến nay ive làm điều này:Làm thế nào để OCR serially với MODI (Microsoft Office Document Imaging) trong C#

public string CheckFilesAndDoOCR(string directoryPath) 
{ 
    directoryPath = Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\"; 
    IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator(); 
    string TheTxt = ""; 

    while (files.MoveNext()) 
    { 
     // FileInfo 
     FileInfo nfo = new FileInfo(Convert.ToString(files.Current)); 

     // Get new file name 
     string fileName = AlltoJPG(nfo); 

     // FileInfo (New File) 
     FileInfo foo = new FileInfo(fileName); 

     // Check for JPG File Format 
     if (foo.Extension == ".jpg" || foo.Extension == ".JPG") 
     // or // ImageFormat.Jpeg.ToString() 
     { 
      try 
      { 
       // OCR Operations... 
       MODI.Document md = new MODI.Document(); 
       md.Create(foo.FullName); 
       md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); // OCR(); 
       MODI.Image image = (MODI.Image)md.Images[0]; 
       TheTxt = image.Layout.Text; 
       md.Close(false); 

       // Create text file with the same Image file name 
       FileStream createFile = new FileStream(foo.DirectoryName + "\\" + foo.Name.Replace(foo.Extension,string.Empty) + ".txt", FileMode.CreateNew); 

       // Save the image text in the text file 
       StreamWriter writeFile = new StreamWriter(createFile); 
       writeFile.Write(TheTxt); 
       writeFile.Close(); 
      } 
      catch (Exception ex) 
      { 
       // Expected errors 
       string LogPath = System.Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\OCRInfo.txt"; 
       Logger(LogPath, "| Exception: Source[" + ex.Source + "] Message[" + ex.Message + "] InnerException[" + ex.InnerException + "] StackTrace[" + ex.StackTrace + "] | "); 
       // MessageBox.Show(ex.Message, "OCR Exception", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 
    } 
    return TheTxt; 
} 

nhưng MODI cung cấp cho các OCR running! hoặc Cant reach file.File is in use. lỗi ..

Tùy thuộc vào tình hình:

  • Làm thế nào tôi có thể tránh được những lỗi?

  • Có cách nào để dừng hoạt động OCR và làm cạn kiệt tất cả các đối tượng đang sử dụng không?

Nếu bất kỳ ai có thể trả lời bất kỳ câu hỏi nào ở trên, nó sẽ được đánh giá cao.

+0

Bạn đã kiểm tra chủ đề này ? http://stackoverflow.com/questions/6699740/ocr-running-error-when-using-modi-2003-with-c-sharp Đây là lỗi chung có nghĩa là MODI gặp sự cố khi nhận dạng bitmap –

+0

@PanagiotisKanavos yes i đã làm! nhưng những câu trả lời đó không giải quyết được vấn đề của tôi .. nó nhận ra tất cả các ký tự và sử dụng các tập tin jpeg và sau khi làm việc trên nó trong một thời gian dài tôi phát hiện ra hầu hết các vấn đề nhưng vẫn tồn tại vấn đề điên rồ nhất. Nó không cho phép tôi di chuyển-xóa tập tin mà tôi đã có kết quả ocr. idk tại sao nó làm điều đó. cho biết tệp vẫn đang được sử dụng. câu hỏi cập nhật bệnh. –

+1

Lý do bạn nhận được lỗi này là do bạn đang cố gắng xử lý nhiều hình ảnh cùng một lúc. Triển khai mã để ngăn chặn điều này. –

Trả lời

2

Đây là mã hoạt động hoàn toàn! nhờ @Ramhound

Mã dưới đây chỉ định một thư mục đầy đủ các ảnh và từng cái một để OCR quét chúng.

/// <summary> 
    /// Gets all images inside a Folder 
    /// and triggers OCR on each.. 
    /// </summary> 
    /// <param name="directoryPath"> Path to Folder </param> 
    /// <returns> Text </returns>   
    public string CheckFileAndDoOCR(string directoryPath) 
    { 
     string TheTxt = ""; 
     IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator(); 

     while (files.MoveNext()) 
     { 
      // FileInfo 
      FileInfo foo = new FileInfo(Convert.ToString(files.Current)); 

      // Check for JPG File Format 
      if (foo.Extension == ".jpg" || foo.Extension == ".JPG") 
      // or // ImageFormat.Jpeg.ToString() 
      { 
       // Start OCR Procedure 
       TheTxt = DoOCR(foo.FullName); 
       // Create TXT file next to ImageFile 
       string txtFileName = foo.DirectoryName + "\\" + foo.Name.Replace(foo.Extension,"") + ".txt"; 
       FileStream createFile = new FileStream(txtFileName, FileMode.OpenOrCreate); 
       // Save the text in to TXT file 
       StreamWriter writeFile = new StreamWriter(createFile); 
       writeFile.Write(TheTxt); 
       // Close 
       writeFile.Close(); 
       createFile.Close(); 
      } 

      // Delete used pictures (Optional) 
      /*--------------------------------------------------------------------*/ 
      try 
      { foo.Delete(); } 
      catch (Exception ex) 
      { Logger(LogPath, "| Exception: Source[" + ex.Source + "] Message[" + ex.Message + 
       "] InnerException[" + ex.InnerException + "] StackTrace[" + ex.StackTrace + "] | "); } 
      /*--------------------------------------------------------------------*/ 
     } 
     return TheTxt; 
    } 
    // DoOCR 
    // 
    /// <summary> 
    /// Start an OCR scan on given ImageFile 
    /// </summary> 
    /// <param name="FullPath"> Path to ImageFile </param> 
    /// <returns> Text </returns> 
    public string DoOCR(string FullPath) 
    { 
     string txt; 

     // OCR Operations... 
     MODI.Document md = new MODI.Document(); // Create MODI.Document 
     md.Create(FullPath); // Fill MODI.Document with my file 
     // Showprogress of OCR 
     md.OnOCRProgress += new MODI._IDocumentEvents_OnOCRProgressEventHandler(this.ShowProgress); 
     // Begin OCR 
     md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); // OCR(); 
     // Image from file 
     MODI.Image image = (MODI.Image)md.Images[0]; 
     txt = image.Layout.Text; 
     // Optionally you can get only first word by using word.Text 
     /// Words from Image : 
     // MODI.Word word = image.Layout.Words[0]; 
     /// Text from first Word : 
     // txt = word.Text; 

     // Close OCR 
     word = null; 
     image = null; 
     md.Close(false); 
     md = null; 

     // Finalize 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 

     // Return Text 
     return txt; 
    } 
1

Điều này là do Doc1.OCR() kiểm tra nhiều trang tập tin tiff nếu bạn chèn chỉ tập trang duy nhất sau đó nó sẽ cho thấy lỗi mà hãy thử sử dụng nhiều trang tiff tập tin

+0

rất vui được biết cảm ơn. –

Các vấn đề liên quan