【发布时间】:2012-06-17 15:02:51
【问题描述】:
我在一个文件夹中将文档扫描为 .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 操作并耗尽所有正在使用的对象?
如果有人能回答以上任何问题,将不胜感激。
【问题讨论】:
-
你检查过这个帖子吗? stackoverflow.com/questions/6699740/… 这是一个通用错误,意味着 MODI 无法识别位图
-
@PanagiotisKanavos 是的,我做到了!但是这些答案并不能解决我的问题..它可以识别所有字符,并且我正在使用 jpeg 文件,并且在处理了很长时间之后,我发现了大部分问题,但仍然存在最疯狂的问题。它不让我移动 - 删除我有 ocr 结果的那个文件。不知道为什么这样做。表示该文件仍在使用中。生病更新问题。
-
您收到此错误的原因是您试图一次处理多个图像。实施代码以防止这种情况发生。
-
@Ramhound 啊,废话!你说的对。我不知道我能感谢你多少!您应该将其发布为答案,以便我可以给您代表。
标签: c# error-handling ms-office ocr modi