我将文档扫描为文件夹中的.jpg图片,并且我想为该文件夹中的每个文档在C#中连续执行OCR。 到目前为止,香港专业教育学院做到了这一点:如何在C#中使用MODI(Microsoft Office Document Imaging)串行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操作并耗尽所有正在使用的对象?
如果任何人都可以回答上述任何问题,将不胜感激。
你检查这个线程错误尝试? http://stackoverflow.com/questions/6699740/ocr-running-error-when-using-modi-2003-with-c-sharp这是一个通用的错误,这意味着MODI无法识别位图 –
@PanagiotisKanavos是我没有!但这些答案并没有解决我的问题..它识别所有字符和即时通讯使用JPEG文件,并在长时间工作后,我发现了大部分问题,但仍然存在最疯狂的问题。它不让我移动 - 删除那个我有ocr结果的文件。 idk为什么这么做。说该文件仍在使用。更新问题。 –
你得到这个错误的原因是因为你试图一次处理多个图像。实施代码来防止这种情况。 –