阅读700 返回首页    go 手机大全


身份证识别__API介绍_文字识别_人工智能图像类-阿里云

本页面主要介绍服务对应的接口和返回结果中的关键字段的含义,请在阅读本页面之前,了解请求数据格式介绍,了解输入输出的通用数据格式。 此外,在本页最后,附上了身份证服务调用的程序示例,以供参考。

请求接口

  • 云市场接口

    • 请求方法: POST
    • 请求url: 在云市场搜索印刷文字识别-身份证识别,在API接口中找到调用地址
  • 数加接口

    • 请求方法: POST
    • 请求url: 打开管理控制台,接口名称选择ocr_idcard,查看请求地址

请求参数

参数名称 参数类型 描述 默认值
image string dataType为50, dataValue是base64编码后的图像数据 空字符串
configure string dataType为50, dataValue是json字符串,其中的具体参数字段参考后面 空字符串

configure 参数的具体字段如下:

参数名称 参数类型 描述 默认值
side string 表示上传图片是身份证正面/反面图像, “face” 表示正面,”back”表示反面

请求参数的POST请求body如下:

  1. {
  2. "inputs": [
  3. {
  4. "image": {
  5. "dataType": 50, #50表示image的数据类型为字符串
  6. "dataValue": "base64_image_string" #图片以base64编码的string
  7. },
  8. "configure": {
  9. "dataType": 50,
  10. "dataValue": "{
  11. "side": "face" #身份证正反面类型: face/back
  12. }"
  13. }
  14. }]
  15. }

返回结果格式

返回结果分为正面和反面两种格式,正面返回结果格式如下:

  1. {
  2. "outputs": [
  3. {
  4. "outputLabel": "ocr_id",
  5. "outputMulti": {},
  6. "outputValue": {
  7. "dataType": 50,
  8. "dataValue": "{
  9. "address" : "浙江省杭州市余杭区文一西路969号", #地址信息
  10. "config_str" : "{\"side\":\"face\"}", #配置信息,同输入configure
  11. "name" : "张三", #姓名
  12. "num" : "1234567890", #身份证号
  13. "sex" : "男", #性别
  14. "birth" : "20000101", #出生日期
  15. "nationality" : "汉", #民族
  16. "success" : true #识别结果,true表示成功,false表示失败
  17. }"
  18. }
  19. }]
  20. }

每一个请求返回的结果都是一个json字符串,由dataValue关键词可以索引到,主要有7个字段:

  • address: 识别的地址信息, 类型为字符串
  • config_str: 表示发送请求时候的配置字符串,为json字符串格式,表示输入时候的配置参数, 类型为字符串
  • name: 识别的身份证姓名, 类型为字符串
  • num: 识别的身份证号码, 类型为字符串
  • sex: 识别的性别, 类型为字符串
  • birth: 识别的出生日期, 类型为字符串
  • nationality : 识别的民族, 类型为字符串
  • success: 识别流程是否出现异常, false表示识别失败,true表示识别成功, 类型为布尔型

反面输出结果格式如下:

  1. {
  2. "outputs": [
  3. {
  4. "outputLabel": "ocr_id",
  5. "outputMulti": {},
  6. "outputValue": {
  7. "dataType": 50,
  8. "dataValue": "{
  9. "config_str" :
  10. "{\"side\":\"back\"}",#配置信息,同输入configure
  11. "start_date" : "19700101", #有效期起始时间
  12. "end_date" : "19800101", #有效期结束时间
  13. "issue" : "杭州市公安局", #签发机关
  14. "success" : true #识别结果,true表示成功,false表示失败
  15. }"
  16. }
  17. }]
  18. }

每个返回结果同样存储在以dataValue为关键词所对应的键值对中,其中有5个字段:

  • config_str: 和正面字段含义一样, 类型为字符串
  • issue: 身份证签发机关,类型为字符串
  • start_date: 身份证有效期起始时间,类型为字符串
  • end_date: 身份证有效期结束时间,类型为字符串
  • success: 识别流程是否成功,类型为布尔型

程序示例

云市场

在云市场搜索印刷文字识别-身份证识别,在API接口中找到请求示例

数加平台

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import org.apache.commons.codec.binary.Base64;
  5. import org.json.JSONObject;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. public class IDcard {
  9. /*
  10. * 获取参数的json对象
  11. */
  12. public static JSONObject getParam(int type, JSONObject dataValue) {
  13. JSONObject obj = new JSONObject();
  14. try {
  15. obj.put("dataType", type);
  16. obj.put("dataValue", dataValue);
  17. } catch (JSONException e) {
  18. e.printStackTrace();
  19. }
  20. return obj;
  21. }
  22. /*
  23. * 获取参数的json对象
  24. */
  25. public static JSONObject getParam(int type, String dataValue) {
  26. JSONObject obj = new JSONObject();
  27. try {
  28. obj.put("dataType", type);
  29. obj.put("dataValue", dataValue);
  30. } catch (JSONException e) {
  31. e.printStackTrace();
  32. }
  33. return obj;
  34. }
  35. public static void main(String[] args) {
  36. String imgFile = "idcard.jpg";
  37. String serviceURL = "your-service-url";
  38. String akID = "your access key id";
  39. String akSecret = "your access key secret";
  40. // 对图像进行base64编码
  41. String imgBase64 = "";
  42. try {
  43. File file = new File(imgFile);
  44. byte[] content = new byte[(int) file.length()];
  45. FileInputStream finputstream = new FileInputStream(file);
  46. finputstream.read(content);
  47. finputstream.close();
  48. imgBase64 = new String(Base64.encodeBase64(content));
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. return;
  52. }
  53. // 拼装请求body的json字符串
  54. JSONObject requestObj = new JSONObject();
  55. try {
  56. JSONObject configObj = new JSONObject();
  57. JSONObject obj = new JSONObject();
  58. JSONArray inputArray = new JSONArray();
  59. configObj.put("side", configStr);
  60. obj.put("image", getParam(50, imgBase64));
  61. obj.put("configure", getParam(50, configObj.toString()));
  62. inputArray.put(obj);
  63. requestObj.put("inputs", inputArray);
  64. } catch (JSONException e) {
  65. e.printStackTrace();
  66. }
  67. String body = requestObj.toString();
  68. //Sender代码参考 https://help.aliyun.com/document_detail/shujia/OCR/ocr-api/sender.html
  69. String result = Sender.sendPost(serviceURL, body, akID, akSecret);
  70. System.out.println(result);
  71. // 解析请求结果
  72. try {
  73. JSONObject resultObj = new JSONObject(result);
  74. JSONArray outputArray = resultObj.getJSONArray("outputs");
  75. String output = outputArray.getJSONObject(0).getJSONObject("outputValue").getString("dataValue"); // 取出结果json字符串
  76. JSONObject out = new JSONObject(output);
  77. if (out.getBoolean("success")) {
  78. String addr = out.getString("address"); // 获取地址
  79. String name = out.getString("name"); // 获取名字
  80. String num = out.getString("num"); // 获取身份证号
  81. System.out.printf(" name : %s n num : %sn address : %sn", name, num, addr);
  82. } else {
  83. System.out.println("predict error");
  84. }
  85. } catch (JSONException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }

最后更新:2016-11-24 11:23:48

  上一篇:go 数据格式__API介绍_文字识别_人工智能图像类-阿里云
  下一篇:go 驾驶证识别__API介绍_文字识别_人工智能图像类-阿里云