我有一个应用程序,我正在使用MODI 2007来OCR几个多页的tiff文件。我发现,当我在一个包含几个好的tiffs,但也有一些无法在Windows图片和传真查看器中打开的tiffs的目录上启动时,MODI也无法OCR这些“坏”的tiffs。当这种情况发生时,应用程序无法回收MODI用于OCR这些tiffs的任何内存。在工具尝试OCR太多这样的“坏”口角之后,机器就会耗尽内存,应用程序就会崩溃。我已经尝试了网络上的几个代码修复程序,它们应该可以修复任何MODI内存泄漏,但到目前为止还没有一个对我有效。我粘贴了以下代码的一部分,它执行OCRing:
StringBuilder strRecText = new StringBuilder(10000);
MODI.Document doc1 = new MODI.Document();
doc1.Create(name);
try
{
doc1.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); // this will ocr all pages of a multi-page tiff file
}
catch (Exception e)
{
doc1.Close(false); // clean up
if (doc1 != null)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
doc1 = null;
}
}
MODI.Images images = doc1.Images;
for (int imageCounter = 0; imageCounter < images.Count; imageCounter++)
{
if (imageCounter > 0)
{
if (!noPageBreakFlag)
{
strRecText.Append((char)pageBreakChar);
}
}
MODI.Image image = (MODI.Image)images[imageCounter];
MODI.Layout layout = image.Layout;
strRecText.Append(layout.Text);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
if (layout != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(layout);
layout = null;
}
if (image != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(image);
image = null;
}
}
File.AppendAllText(ocrFile, strRecText.ToString()); // write the OCR file out to disk
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
if (images != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(images);
images = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
doc1.Close(false); // clean up
if (doc1 != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
doc1 = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();发布于 2010-11-12 10:37:02
在过去的几个月里,我一直在使用MODI做一个项目。到目前为止,MODI是我尝试过的最准确的OCR引擎,但它存在一些释放资源和崩溃的主要问题。
我最终构建了一个命令行应用程序,它将图像的路径作为命令行参数,然后将生成的文本保存到文件中并退出。然后,我通过任何需要modi功能的软件来使用这个命令行应用程序。这听起来像是一个奇怪的解决方案,但它是解决MODI内存泄漏问题的一种非常简单和直接的方法,因为当命令行进程存在时,它的内存会被操作系统释放,所以您不必担心应用程序崩溃或资源未被清理。我发现启动命令行exe然后读取它创建的文件所花费的时间与实际OCR图像所需的时间相比微不足道,因此您实际上不会在性能方面损失太多。
https://stackoverflow.com/questions/2693421
复制相似问题