基于C#技術(shù)實(shí)現(xiàn)身份證識(shí)別功能
最近研究C#相關(guān)的ORC技術(shù),圖像識(shí)別一般C和C++這種底層語言做的比較多,C#主要是依托一些封裝好的組件進(jìn)行調(diào)用,這里介紹三種身份證識(shí)別的方法。
一:調(diào)用大公司API接口,百度、云脈,文通科技都有相關(guān)的API介紹。
二:調(diào)用圖像處理類庫(kù),EmguCV是OpenCV的一個(gè)跨平臺(tái)的.Net封裝,該封裝也可以被編譯到Mono平臺(tái)和允許在Windows、Mac OS、Android、iPhone、iPad等多個(gè)平臺(tái)上運(yùn)行
三:調(diào)用Office2007 組件
一、證件識(shí)別API接口
以聚合數(shù)據(jù)中的API接口為例,因?yàn)楣俜紸PI沒有提供C#的調(diào)用方式,網(wǎng)址如下:證件識(shí)別接口
/// <summary> /// 上傳圖片 /// </summary> /// <returns></returns> public static string CardUpload() { try { string appkey = "網(wǎng)站自己申請(qǐng)的key"; //配置您申請(qǐng)的appkey HttpPostedFile file = HttpContext.Current.Request.Files[0]; string url = "http://api2.juheapi.com/cardrecon/upload"; var parameters = new Dictionary<string, string>(); parameters.Add("key", appkey); parameters.Add("cardType", "2"); string result = HttpPostData(url, 60000, "pic", file.InputStream, parameters); JObject info = JObject.Parse(JObject.Parse(result)["result"].ToString()); var cardInfo = new { name = info["姓名"], card = info["公民身份號(hào)碼"] }; return cardInfo.ToJson(); } catch (Exception ex) { return ex.ToString(); } } /// <summary> /// Post調(diào)用API /// </summary> /// <param name="url">api地址</param> /// <param name="timeOut">訪問超時(shí)時(shí)間</param> /// <param name="fileKeyName">文件參數(shù)名</param> /// <param name="file">文件流</param> /// <param name="stringDict">參數(shù)列表</param> /// <returns>結(jié)果集</returns> private static string HttpPostData(string url, int timeOut, string fileKeyName, Stream file, Dictionary<string, string> stringDict) { string responseContent; var memStream = new MemoryStream(); var webRequest = (HttpWebRequest)WebRequest.Create(url); // 邊界符 var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); // 邊界符 var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); // 最后的結(jié)束符 var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); // 設(shè)置屬性 webRequest.Method = "POST"; webRequest.Timeout = timeOut; webRequest.ContentType = "multipart/form-data; boundary=" + boundary; //寫入開始邊界符 memStream.Write(beginBoundary, 0, beginBoundary.Length); // 寫入文件 const string filePartHeader = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n"; var header = string.Format(filePartHeader, fileKeyName, "card.jpg"); var headerbytes = Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); file.CopyTo(memStream); // 寫入字符串的Key var stringKeyHeader = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}\r\n"; foreach (byte[] formitembytes in from string key in stringDict.Keys select string.Format(stringKeyHeader, key, stringDict[key]) into formitem select Encoding.UTF8.GetBytes(formitem)) { memStream.Write(formitembytes, 0, formitembytes.Length); } // 寫入最后的結(jié)束邊界符 memStream.Write(endBoundary, 0, endBoundary.Length); webRequest.ContentLength = memStream.Length; // 構(gòu)造完畢,執(zhí)行POST方法 var requestStream = webRequest.GetRequestStream(); memStream.Position = 0; var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); var httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { responseContent = httpStreamReader.ReadToEnd(); } httpWebResponse.Close(); webRequest.Abort(); return responseContent; }
二、EmguCV類庫(kù)調(diào)用
環(huán)境搭建
下載地址:EmguCV官網(wǎng)
在File類別下下載這個(gè)EXE,進(jìn)行安裝,安裝后在目錄下能找相應(yīng)組件,還有些應(yīng)用的案例。
C#進(jìn)行識(shí)別,需進(jìn)行圖片二值化處理和OCR調(diào)用相關(guān)DLL可在我整理的地址下載:360云盤 提取碼:89f4
dll文件夾中的dll引用到C#項(xiàng)目中,x64,x86,tessdata對(duì)應(yīng)OCR識(shí)別的類庫(kù)和語言庫(kù),我tessdata中已添加中文語言包,將這三個(gè)文件夾放入程序執(zhí)行文件夾中。
Demo
自己做的小Demo如圖:身份證圖片是百度上下載的
相關(guān)代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.OCR; using Emgu.CV.Structure; using System.IO; namespace ImageManage { public partial class Form1 : Form { Image<Gray, Byte> imageThreshold; public Form1() { InitializeComponent(); } private void btn_convert_Click(object sender, EventArgs e) { //第一個(gè)參數(shù)是語言包文件夾的地址,不寫默認(rèn)在執(zhí)行文件夾下 Tesseract _ocr = new Tesseract("", "chi_sim", OcrEngineMode.TesseractOnly); _ocr.Recognize(imageThreshold); String text = _ocr.GetText(); this.textBox1.Text = text; } private void pictureBox1_Click(object sender, EventArgs e) { OpenFileDialog of = new OpenFileDialog(); of.Title = "請(qǐng)選擇圖片"; if (of.ShowDialog() == DialogResult.OK) { string file = of.FileName; Image img = Image.FromFile(file); pictureBox1.Image = img; } Bitmap bitmap = (Bitmap)this.pictureBox1.Image; Image<Bgr, Byte> imageSource = new Image<Bgr, byte>(bitmap); Image<Gray, Byte> imageGrayscale = imageSource.Convert<Gray, Byte>(); imageGrayscale = randon(imageGrayscale); imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255)); this.pictureBox2.Image = imageThreshold.ToBitmap(); } /// <summary> /// 旋轉(zhuǎn)校正 /// </summary> /// <param name="imageInput"></param> /// <returns></returns> private Image<Gray, Byte> randon(Image<Gray, Byte> imageInput)//圖像投影旋轉(zhuǎn)法傾斜校正子函數(shù)定義 { int nwidth = imageInput.Width; int nheight = imageInput.Height; int sum; int SumOfCha; int SumOfChatemp = 0; int[] sumhang = new int[nheight]; Image<Gray, Byte> resultImage = imageInput; Image<Gray, Byte> ImrotaImage; //20度范圍內(nèi)的調(diào)整 for (int ang = -20; ang < 20; ang = ang + 1) { ImrotaImage = imageInput.Rotate(ang, new Gray(1)); for (int i = 0; i < nheight; i++) { sum = 0; for (int j = 0; j < nwidth; j++) { sum += ImrotaImage.Data[i, j, 0]; } sumhang[i] = sum; } SumOfCha = 0; for (int k = 0; k < nheight - 1; k++) { SumOfCha = SumOfCha + (Math.Abs(sumhang[k] - sumhang[k + 1])); } if (SumOfCha > SumOfChatemp) { resultImage = ImrotaImage; SumOfChatemp = SumOfCha; } } return resultImage; } } }
三、Office 2007組件
該組件免費(fèi)而且識(shí)別度比較高。
環(huán)境搭建
Office 2007組件MODI,需要安裝Ofiice2007,且由于兼容性需要安裝補(bǔ)丁,SP1或者SP2都行,補(bǔ)丁下載地址如下:
SP1下載地址 SP2下載地址
安裝后控制面板-->卸載或更新程序-->選擇Office2007-->選擇更改-->選擇添加或修復(fù)功能-->彈出下面界面,運(yùn)行相應(yīng)組件。
將Office工具-->Microsoft Office Document Imaging 下的工具運(yùn)行
在C#項(xiàng)目中引用Com組件即可:
如果Office組件應(yīng)用不是在本地程序而需要部署在IIS上,還需將應(yīng)用程序的應(yīng)用池的權(quán)限設(shè)置為如下圖所示:程序應(yīng)用池-->高級(jí)設(shè)置-->標(biāo)識(shí)
Demo
StringBuilder sb = new StringBuilder(); MODI.Document doc = new MODI.Document(); doc.Create(fullFileName); MODI.Image image; MODI.Layout layout; doc.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, true, true); // 識(shí)別文字類型 for (int i = 0; i < doc.Images.Count; i++) { image = (MODI.Image)doc.Images[i]; layout = image.Layout; sb.Append(layout.Text); }
以上即一些C#進(jìn)行身份證識(shí)別的方法,可根據(jù)自己項(xiàng)目的不同需求進(jìn)行選用。
相關(guān)文章
OpenCvSharp實(shí)現(xiàn)Mat對(duì)象簡(jiǎn)單的像素操作
這篇文章主要介紹了OpenCvSharp實(shí)現(xiàn)Mat對(duì)象簡(jiǎn)單的像素操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11C#如何Task執(zhí)行任務(wù),等待任務(wù)完成
這篇文章主要介紹了C#如何Task執(zhí)行任務(wù),等待任務(wù)完成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06C# wpf Brush轉(zhuǎn)Hex字符串的實(shí)例代碼
這篇文章主要介紹了C# wpf Brush轉(zhuǎn)Hex字符串的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01WPF中鼠標(biāo)/鍵盤/拖拽事件以及用行為封裝事件詳解
這篇文章主要為大家詳細(xì)介紹了WPF中常用的鼠標(biāo)事件、鍵盤事件以及注意事項(xiàng),同時(shí)使用一個(gè)案例講解了拓展事件,感興趣的小伙伴可以了解一下2023-03-03