C#身份证OCR识别相关技术
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
最近研究C#相关的OCR技术,图像识别一般C和C++这种底层语言做的比较多,C#主要是依托一些封装好的组件进行调用,这里介绍三种身份证识别的方法。
一、证件识别API接口以聚合数据中的API接口为例,因为官方API没有提供C#的调用方式,网址如下:证件识别接口 /// /// 上传图片 /// /// 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(); } } /// /// Post调用API /// /// api地址 /// 访问超时时间 /// 文件参数名 /// 文件流 /// 参数列表 /// 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; } 二、EmguCV类库调用环境搭建下载地址:EmguCV官网 在File类别下下载这个EXE,进行安装,安装后在目录下能找相应组件,还有些应用的案例。 C#进行识别,需进行图片二值化处理和OCR调用相关DLL可在我整理的地址下载:360云盘 提取码:89f4 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 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 Image imageGrayscale = randon(imageGrayscale); imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255)); this.pictureBox2.Image = imageThreshold.ToBitmap(); } /// /// 旋转校正 /// /// /// 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度范围内的调整 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组件该组件免费而且识别度比较高。 环境搭建Office 2007组件MODI,需要安装Ofiice2007,且由于兼容性需要安装补丁,SP1或者SP2都行,补丁下载地址如下:
将Office工具-->Microsoft Office Document Imaging 下的工具运行 在C#项目中引用Com组件即可: 如果Office组件应用不是在本地程序而需要部署在IIS上,还需将应用程序的应用池的权限设置为如下图所示:程序应用池-->高级设置-->标识 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); // 识别文字类型 for (int i = 0; i
< doc.Images.Count; i++) { image = (MODI.Image)doc.Images[i]; layout = image.Layout; sb.Append(layout.Text); } 以上即一些C#进行身份证识别的方法,可根据自己项目的不同需求进行选用。
该文章在 2021/3/3 11:53:24 编辑过 |
关键字查询
相关文章
正在查询... |