C#身份證OCR識(shí)別相關(guān)技術(shù)
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
閱讀目錄
最近研究C#相關(guān)的OCR技術(shù),圖像識(shí)別一般C和C++這種底層語(yǔ)言做的比較多,C#主要是依托一些封裝好的組件進(jìn)行調(diào)用,這里介紹三種身份證識(shí)別的方法。
一、證件識(shí)別API接口以聚合數(shù)據(jù)中的API接口為例,因?yàn)楣俜紸PI沒(méi)有提供C#的調(diào)用方式,網(wǎng)址如下:證件識(shí)別接口 /// /// 上傳圖片 /// /// 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(); } } /// /// Post調(diào)用API /// /// api地址 /// 訪問(wèn)超時(shí)時(shí)間 /// 文件參數(shù)名 /// 文件流 /// 參數(shù)列表 /// 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; //寫(xiě)入開(kāi)始邊界符 memStream.Write(beginBoundary, 0, beginBoundary.Length); // 寫(xiě)入文件 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); // 寫(xiě)入字符串的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); } // 寫(xiě)入最后的結(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云盤(pán) 提取碼:89f4 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 public Form1() { InitializeComponent(); } private void btn_convert_Click(object sender, EventArgs e) { //第一個(gè)參數(shù)是語(yǔ)言包文件夾的地址,不寫(xiě)默認(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 Image imageGrayscale = randon(imageGrayscale); imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255)); this.pictureBox2.Image = imageThreshold.ToBitmap(); } /// /// 旋轉(zhuǎn)校正 /// /// /// private Image { int nwidth = imageInput.Width; int nheight = imageInput.Height; int sum; int SumOfCha; int SumOfChatemp = 0; int[] sumhang = new int[nheight]; Image Image //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ǔ)丁下載地址如下:
將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í) DemoStringBuilder 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)行選用。
該文章在 2021/3/3 11:53:24 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |