印刷文字识别-身份证OCR识别案例

此案例主要用于对身份证信息的抓取,可用作身份证信息有源比对等场景。我用的是阿里云上申请的接口,首次购买赠送500次还是很实惠的。

如果在找不到也可以在上面搜索框中搜索。

具体代码:

static void Main(string[] args)
        {
            //印刷文字识别-身份证识别OCR
            String url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
            String appcode = "自己AppCode";
            String img_file = @"XX.jpg";//图片本地地址/图片url地址
            
            //如果输入带有inputs, 设置为True,否则设为False
            bool is_old_format = false;

            //如果没有configure字段,config设为''
            //String config = '';
            String config = "{\\\"side\\\":\\\"back\\\"}"; //{ "side":"face"}  
            #身份证正反面类型:face/back        //"image":"图片二进制数据的base64编码/图片url"
            String method = "POST";
            String querys = "";
            FileStream fs = new FileStream(img_file, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            byte[] contentBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
            String base64 = System.Convert.ToBase64String(contentBytes);
            String bodys;
            if (is_old_format)
            {
                bodys = "{\"inputs\" :" +
                                    "[{\"image\" :" +
                                        "{\"dataType\" : 50," +
                                         "\"dataValue\" :\"" + base64 + "\"" +
                                         "}";
                if (config.Length > 0)
                {
                    bodys += ",\"configure\" :" +
                                    "{\"dataType\" : 50," +
                                     "\"dataValue\" : \"" + config + "\"}" +
                                     "}";
                }
                bodys += "]}";
            }
            else
            {
                bodys = "{\"image\":\"" + base64 + "\"";
                if (config.Length > 0)
                {
                    bodys += ",\"configure\" :\"" + config + "\"";
                }
                bodys += "}";
            }
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;
            }

            if (url.Contains("https://"))
            {
                ServicePointManager.ServerCertificateValidationCallback = 
new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            //根据API的要求,定义相对应的Content-Type
            httpRequest.ContentType = "application/json; charset=UTF-8";
            if (0 < bodys.Length)
            {
                byte[] data = Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine("http error code: " + httpResponse.StatusCode);
                Console.WriteLine("error in header: " 
+ httpResponse.GetResponseHeader("X-Ca-Error-Message"));
                Console.WriteLine("error in body: ");
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                Console.WriteLine(reader.ReadToEnd());
            }
            else
            {

                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                string rs = reader.ReadToEnd();
                Console.WriteLine(rs);

            }
            Console.WriteLine("\n");
            Console.ReadKey();
        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, 
X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

返回示例:

正面返回结果:
{
  "address"    : "浙江省杭州市余杭区文一西路969号",   #地址信息
  "config_str" : "{\\\"side\\\":\\\"face\\\"}",    #配置信息,同输入configure
  "face_rect":{       #人脸位置
    "angle": -90,   #angle表示矩形顺时针旋转的度数
    "center":{      #center表示人脸矩形中心坐标
      "x" : 952,
      "y" : 325.5
    },
    "size":{        #size表示人脸矩形长宽
      "height":181.99,
      "width":164.99
    }
  }, 
  "card_region":[  #身份证区域位置,四个顶点表示,顺序是逆时针(左上、左下、右下、右上)
     {"x":165,"y":657},
     {"x":534,"y":658},
     {"x":535,"y":31},
     {"x":165,"y":30}
   ],
  "face_rect_vertices":[  #人脸位置,四个顶点表示
      {  
         "x":1024.6600341796875,
         "y":336.629638671875
      },
      {  
         "x":906.66107177734375,
         "y":336.14801025390625
      },
      {  
         "x":907.1590576171875,
         "y":214.1490478515625
      },
      {  
         "x":1025.157958984375,
         "y":214.63067626953125
      }
    ],
  "name" : "张三",                 #姓名
  "nationality": "汉",            #民族 
  "num" : "1234567890",            #身份证号
  "sex" : "男",                    #性别
  "birth" : "20000101",            #出生日期
  "is_fake": false,                   #是否是复印件
  "success" : true                 #识别结果,true表示成功,false表示失败
}


反面返回结果:
{
    "config_str" : "{\\\"side\\\":\\\"back\\\"}",  #配置信息,同输入configure
    "start_date" : "19700101",       #有效期起始时间
    "end_date" : "19800101",         #有效期结束时间
    "issue" : "杭州市公安局",         #签发机关 
    "is_fake": false,                   #是否是复印件
    "success" : true                 #识别结果,true表示成功,false表示失败
}

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注