private Dictionary<string, string> OcrRecognizes()
{
try
{
//识别的结果放在dic里,key是文件名,value是识别的结果
Dictionary<string, string> dic = new Dictionary<string, string>();
//初始化
IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
//这里的ocradvantagerruntime路径需要改为设置的路径
ocrEngine.Startup(null, null, null, _ocrAdvantageRuntimePath);
ocrEngine.LanguageManager.EnableLanguages(new string[] { "zh-Hans", "en" });
//提取自定义识别区域
string ocrsetfilename = Path.Combine(System.Windows.Forms.Application.StartupPath, "ocrSetup.xml");
if(!File.Exists(ocrsetfilename))
{
MessageBox.Show("您没有设置ocr选区!");
return dic;
}
OcrSetRWXML ocrsetrwXml = new OcrSetRWXML(ocrsetfilename);
Dictionary<string, object> ocrDic = ocrsetrwXml.reader();
if (ocrDic.Count <= 0)
return dic;
string zonefilename = (string)ocrDic["ZoneFileName"];
if (zonefilename.Equals(string.Empty))
return dic;
if (!File.Exists(zonefilename))
return dic;
using (WaitCursor waitCursor = new WaitCursor())
{
if (Directory.Exists(_tempFilePath))
{
DirectoryInfo dir = new DirectoryInfo(_tempFilePath);
FileInfo[] files = dir.GetFiles();
SortAsFileName(ref files);
if (files.Count() > 0)
{
for (int i = 0; i < files.Count(); i++)
{
#region 1
string name = files.FullName;
RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(name, 1);
IOcrPage ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose);
ocrPage.LoadZones(zonefilename);
ocrPage.Recognize(null);
IOcrPageCharacters pageCharacters = ocrPage.GetRecognizedCharacters();
string text = "";
if (pageCharacters.Count > 0)
{
IOcrZoneCharacters _zoneCharacters1 = pageCharacters[0];
text = ocrPage.GetText(0);
if (string.IsNullOrEmpty(text))
text = "";
}
//将识别结果放到dic中去
dic[name] = text;
//释放
rasterImage.Dispose();
ocrPage.Dispose();
#endregion
}
}
}
}
//完毕释放
ocrEngine.Shutdown();
ocrEngine.Dispose();
return dic;
}
catch (OcrException ex)
{
MessageBox.Show(ex.Message);
return new Dictionary<string, string>();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return new Dictionary<string, string>();
}
}
这个进行ocr识别的函数。处理黑白图片的时候可以正常识别通过。但是一处理彩色图片就提示内存不足
|