1. 程式人生 > >C#圖片文字識別

C#圖片文字識別

har ise com 兼容 匯總 朋友 -1 ocr wid

圖片識別的技術到幾天已經很成熟了,只是相關的資料很少,為了方便在此匯總一下(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

本文出自:錢大寶的博客地,地址:http://www.qiandabao.com/cxrs/1173.html,轉載須註明! 本文出自錢大寶的博客地,本文地址:http://www.qiandabao.com/cxrs/1173.html

C#圖片文字識別