gpt4 book ai didi

c# - 如何在 C# 中使用 MODI (Microsoft Office Document Imaging) 连续进行 OCR

转载 作者:太空狗 更新时间:2023-10-29 23:33:18 24 4
gpt4 key购买 nike

我在一个文件夹中将文档扫描为 .jpg 图片,我想在 C# 中对该文件夹中的每个文档连续执行 OCR。到目前为止,我已经完成了:

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;
}

但是 MODI 给出了 OCR running!Cant reach file.File is in use. 错误..

视情况而定:

  • 如何避免这些错误?

  • 有没有办法停止 OCR 操作并耗尽所有正在使用的对象?

如果有人能回答上述任何问题,我们将不胜感激。

最佳答案

这是完整的工作代码!感谢@Ramhound

下面的代码只是指定了一个装满图片的文件夹,并一张一张地对它们进行OCR扫描。

    /// <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;
}

关于c# - 如何在 C# 中使用 MODI (Microsoft Office Document Imaging) 连续进行 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com