请教版主,我在进行批量OCR识别时,内存耗尽程序自动退出,监控计算机内存到5G多,大概处理了100多张图片,请您看看我的程序有什么问题,非常感谢!!!
try
{
IOcrEngine _ocrEngine;
// 初始化OCR引擎
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Professional, false);
// 启动OCR引擎
_ocrEngine.Startup(null, null, null, Application.StartupPath.ToString() + "\\License\\OcrProfessionalRuntime");
//将语言设置为中文简体
_ocrEngine.LanguageManager.EnableLanguages(new string[] { "zh-Hans", "en" });//zh-Hans en
DirectoryInfo directoryInfo = new DirectoryInfo(this.textBox1.Text);
string[] myDirectory = System.IO.Directory.GetDirectories(this.textBox1.Text);
int count = myDirectory.Length; int now = 0;
// 加载图像
RasterCodecs codecs = new RasterCodecs();
codecs.ThrowExceptionsOnInvalidImages = true;
RasterImage image;
string mydanghao = ""; string filepath = ""; string content = "";
DirectoryInfo folder; StreamWriter sr;
OcrZone ocrZone = new OcrZone();
ocrZone.ZoneType = OcrZoneType.Text;
foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories())
{
now = now + 1;
mydanghao = childDirectoryInfo.Name.ToString();
filepath = this.textBox2.Text.Trim() + "\\" + mydanghao + ".txt";
// 创建文件
content = "";
folder = new DirectoryInfo(this.textBox1.Text.Trim() + "\\" + mydanghao);
foreach (FileInfo file in folder.GetFiles("*.*"))
{
string filepath1 = this.textBox1.Text.Trim() + "\\" + mydanghao + "\\" + file.Name;
image = codecs.Load(filepath1);
using (IOcrPage ocrPage = _ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
{
// Clear all the zones in the page
ocrPage.Zones.Clear();
// Add our field zone
//Leadtools.Forms.LogicalRectangle
ocrZone.Bounds = new Leadtools.Forms.LogicalRectangle(new LeadRect(0, 0, image.Width, image.Height));
ocrPage.Zones.Add(ocrZone);
// Recognize the page. This will only recognize the zone we added
ocrPage.Recognize(null);
string result = ocrPage.GetText(0).Trim();
if (result.Length > 0)
{
result = result.Replace("\n", "").Replace("\r", "");
content = content + "\r\n" + result;
}
ocrPage.Dispose();
}
image.Dispose();image = null;
}
if (File.Exists(filepath)) File.Delete(filepath);
sr = File.CreateText(filepath);
sr.WriteLine(content);//写入文本文件
sr.Dispose();
sr.Close();
}
MessageBox.Show("已成功完成OCR识别!\r\n应识别文件夹 " + count.ToString() + " 个,实际识别 " + now.ToString() + " 个", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// 关闭OCR引擎
_ocrEngine.Shutdown();
Process.Start(this.textBox2.Text.Trim());
}
catch (Exception ex)//如果出现错误则执行错误处理语句
{
MessageBox.Show(ex.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
|
|