C#OCR圖片識別

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


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


 

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

方式1、Asprise-OCR實現圖片

方式2、Microsoft Office Document Imaging(Office 2007) 組件實現文檔


方式1、Asprise-OCR的使用。get

Asprise-OCR下載地址:string

http://asprise.com/product/ocr/download.php?lang=csharp io

其中須要使用的3個dll是AspriseOCR.dll、DevIL.dll、ILU.dll。驗證碼

須要注意的是這幾個.dll是vc寫的引用要在程序中用DllImport引用,關鍵代碼:class

[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:爲圖片路徑,startXstartY座標均爲0便可,widthheight圖片的寬和高。


方式2、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, truetrue);  // 識別簡體中文

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爲讀取圖片的文字類型枚舉。

相關文章
相關標籤/搜索