最近研究C#相关的ORC技术,图像识别一般C和C++这种底层语言做的比较多,C#主要是依托一些封装好的组件进行调用,这里介绍三种身份证识别的方法。
一:调用大公司API接口,百度、云脉,文通科技都有相关的API介绍。
二:调用图像处理类库,EmguCV是OpenCV的一个跨平台的.Net封装,该封装也可以被编译到Mono平台和允许在Windows、Mac OS、Android、iPhone、iPad等多个平台上运行
三:调用Office2007 组件
一、证件识别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 (); 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, DictionarystringDict) { 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
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 { ImageimageThreshold; 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 imageSource = new Image (bitmap); Image imageGrayscale = imageSource.Convert (); imageGrayscale = randon(imageGrayscale); imageThreshold = imageGrayscale.ThresholdBinary(new Gray(100), new Gray(255)); this.pictureBox2.Image = imageThreshold.ToBitmap(); } /// /// 旋转校正 /// /// ///private Image randon(Image imageInput)//图像投影旋转法倾斜校正子函数定义 { int nwidth = imageInput.Width; int nheight = imageInput.Height; int sum; int SumOfCha; int SumOfChatemp = 0; int[] sumhang = new int[nheight]; Image resultImage = imageInput; Image 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; } } }
三、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#进行身份证识别的方法,可根据自己项目的不同需求进行选用。