天天看點

C# 圖檔識别(支援21種語言)

圖檔識别的技術到幾天已經很成熟了,隻是相關的資料很少,為了友善在此彙總一下(C#實作),友善需要的朋友查閱,也給自己做個記号。

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

C# 圖檔識别(支援21種語言)

本文介紹兩種比較主流和成熟的識别方式:

方式一、Asprise-OCR實作。

方式二、Microsoft Office Document Imaging(Office 2007) 元件實作。

方式一、Asprise-OCR的使用。

Asprise-OCR下載下傳位址:

其中需要使用的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圖檔的寬和高。

在使用之前需要給大家說的是Imaging 元件的相容性不是很好,使用win 7 office 2007的時必須打上office 2007 sp1或者sp2更新檔,讀取中文才行。 

sp1更新檔位址(226M) :

<a href="http://download.microsoft.com/download/1/6/5/1659d607-8696-4001-8072-efaedd70dd30/office2007sp1-kb936982-fullfile-zh-cn.exe">http://download.microsoft.com/download/1/6/5/1659d607-8696-4001-8072-efaedd70dd30/office2007sp1-kb936982-fullfile-zh-cn.exe</a>

sp2更新檔位址(301 MB):

給項目添加元件引用,如圖:

C# 圖檔識别(支援21種語言)

使用代碼:

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 &lt; 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://www.cnblogs.com/vipstone/archive/2011/10/08/2202397.html,如需轉載請自行聯系原作者