基於C#技巧完成身份證辨認功效。本站提示廣大學習愛好者:(基於C#技巧完成身份證辨認功效)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C#技巧完成身份證辨認功效正文
比來研討C#相干的ORC技巧,圖象辨認普通C和C++這類底層說話做的比擬多,C#重要是依托一些封裝好的組件停止挪用,這裡引見三種身份證辨認的辦法。
一:挪用年夜公司API接口,百度、雲脈,文通科技都有相干的API引見。
二:挪用圖象處置類庫,EmguCV是OpenCV的一個跨平台的.Net封裝,該封裝也能夠被編譯到Mono平台和許可在Windows、Mac OS、Android、iPhone、iPad等多個平台上運轉
三:挪用Office2007 組件
1、證件辨認API接口
以聚合數據中的API接口為例,由於官方API沒有供給C#的挪用方法,網址以下:證件辨認接口
/// <summary> /// 上傳圖片 /// </summary> /// <returns></returns> public static string CardUpload() { try { string appkey = "網站本身請求的key"; //設置裝備擺設您請求的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["國民身份號碼"] }; return cardInfo.ToJson(); } catch (Exception ex) { return ex.ToString(); } } /// <summary> /// Post挪用API /// </summary> /// <param name="url">api地址</param> /// <param name="timeOut">拜訪超不時間</param> /// <param name="fileKeyName">文件參數名</param> /// <param name="file">文件流</param> /// <param name="stringDict">參數列表</param> /// <returns>成果集</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"); // 最初的停止符 var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); // 設置屬性 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); } // 寫入最初的停止界限符 memStream.Write(endBoundary, 0, endBoundary.Length); webRequest.ContentLength = memStream.Length; // 結構終了,履行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; }
2、EmguCV類庫挪用
情況搭建
下載地址:EmguCV官網
在File種別下下載這個EXE,停止裝置,裝置後在目次下能找響應組件,還有些運用的案例。
C#停止辨認,需停止圖片二值化處置和OCR挪用相干DLL可在我整頓的地址下載:360雲盤 提取碼:89f4
dll文件夾中的dll援用到C#項目中,x64,x86,tessdata對應OCR辨認的類庫和說話庫,我tessdata中已添加中文說話包,將這三個文件夾放入法式履行文件夾中。
Demo
本身做的小Demo如圖:身份證圖片是百度高低載的
相干代碼以下:
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) { //第一個參數是說話包文件夾的地址,不寫默許在履行文件夾下 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 = "請選擇圖片"; 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> /// 扭轉校訂 /// </summary> /// <param name="imageInput"></param> /// <returns></returns> private Image<Gray, Byte> randon(Image<Gray, Byte> imageInput)//圖象投影扭轉法傾斜校訂子函數界說 { 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度規模內的調劑 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; } } }
3、Office 2007組件
該組件收費並且辨認度比擬高。
情況搭建
Office 2007組件MODI,須要裝置Ofiice2007,且因為兼容性須要裝置補釘,SP1或許SP2都行,補釘下載地址以下:
SP1下載地址 SP2下載地址
裝置後掌握面板-->卸載或更新法式-->選擇Office2007-->選擇更改-->選擇添加或修復功效-->彈出上面界面,運轉響應組件。
將Office對象-->Microsoft Office Document Imaging 下的對象運轉
在C#項目中援用Com組件便可:
假如Office組件運用不是在當地法式而須要安排在IIS上,還需將運用法式的運用池的權限設置為以下圖所示:法式運用池-->高等設置-->標識
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); // 辨認文字類型 for (int i = 0; i < doc.Images.Count; i++) { image = (MODI.Image)doc.Images[i]; layout = image.Layout; sb.Append(layout.Text); }
以上即一些C#停止身份證辨認的辦法,可依據本身項目標分歧需求停止選用。