C# 圖片識別(支持21種語言)

原文鏈接
http://www.cnblogs.com/stone_w/archive/2011/10/08/2202397.html#2472415

圖片識別的技術到幾天已經很成熟了,只是相關的資料很少,爲了方便在此彙總一下(C#實現),方便需要的朋友查閱,也給自己做個記號。

圖片識別的用途:很多人用它去破解網站的驗證碼,用於達到自動刷票或者是批量註冊的目的,但我覺得它最吸引我的地方是可以讓一些書寫的東西,自動識別成電腦上的文字,比如說手擬的合同,修改過的書面論文或者是文檔,每月的花費發票需要在電腦上錄入或者是彙總信息,日記本上的文章要轉移到電腦上等等,我們現在就不用再頭痛把它們在電腦上敲寫一遍了。
這裏寫圖片描述

本文介紹兩種比較主流和成熟的識別方式:
方式一、Asprise-OCR實現。
方式二、Microsoft Office Document Imaging(Office 2007) 組件實現。

方式一、Asprise-OCR的使用。
Asprise-OCR下載地址:
http://asprise.com/product/ocr/download.php?lang=csharp
其中需要使用的3個dll是AspriseOCR.dll、DevIL.dll、ILU.dll。
需要注意的是這幾個.dll是vc寫的引用要在程序中用DllImport引用,關鍵代碼:
[DllImport(“AspriseOCR.dll”, EntryPoint = “OCR”, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OCR(string file, int type);
[DllImport(“AspriseOCR.dll”, EntryPoint = “OCRpart”, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr OCRpart(string file, int type, int startX, int startY, int width, int height);
[DllImport(“AspriseOCR.dll”, EntryPoint = “OCRBarCodes”, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr OCRBarCodes(string file, int type);
[DllImport(“AspriseOCR.dll”, EntryPoint = “OCRpartBarCodes”, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr OCRpartBarCodes(string file, int type, int startX, int startY, int width, int height);

調用代碼很簡單隻有一句:
MessageBox.Show(Marshal.PtrToStringAnsi(OCRpart(img_path, -1, startX, startY, width, height)));
其中img_path:爲圖片路徑,startX、startY座標均爲0即可,width、height圖片的寬和高。

方式二、Microsoft Office Document Imaging(Office 2007) 組件實現。
在使用之前需要給大家說的是Imaging 組件的兼容性不是很好,使用win 7 office 2007的時必須打上office 2007 sp1或者sp2補丁,讀取中文才行。
sp1補丁地址(226M) :
http://download.microsoft.com/download/1/6/5/1659d607-8696-4001-8072-efaedd70dd30/office2007sp1-kb936982-fullfile-zh-cn.exe
sp2補丁地址(301 MB):
http://download.microsoft.com/download/A/3/9/A39E919E-AFA8-4128-9249-51629206C70F/office2007sp2-kb953195-fullfile-zh-cn.exe

給項目添加組件引用,如圖:
這裏寫圖片描述

使用代碼:
MODI.Document doc = new MODI.Document();
doc.Create(img_Path);
MODI.Image image;
MODI.Layout layout;
doc.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, true, true); // 識別簡體中文
for (int i = 0; i < doc.Images.Count; i++)
{
image = (MODI.Image)doc.Images[i];
layout = image.Layout;
sb.Append(layout.Text);
}
MessageBox.Show(sb.ToString());
其中img_Path爲圖片路徑,MODI.MiLANGUAGES爲讀取圖片的文字類型枚舉。

本文源碼下載地址:http://files.cnblogs.com/stone_w/OCR.rar

漢王 PDF OCR V8.1簡體中文版下載地址(已經使用了,感覺不錯推薦一下):http://xiazai.zol.com.cn/detail/34/335364.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章