閱讀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介紹_文字識別_人工智能圖像類-阿裏雲