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
c#圖文識別