使用阿里云身份证扫描识别接口案例——CSDN博客

/**
	 * 
	* @Title: idcard
	* @Description: 该方法的主要作用:扫描身份证
	* @param  @return 设定文件  
	* @return  返回类型:String   
	* @throws
	 */
	public void idcard(){
		String host = "https://dm-51.data.aliyun.com";
	    String path = "/rest/160601/ocr/ocr_idcard.json";
	    String method = "POST";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    headers.put("Content-Type", "application/json; charset=UTF-8");
	    Map<String, String> querys = new HashMap<String, String>();
	    String bodys = "{\"inputs\": [{\"image\": {\"dataType\": 50,\"dataValue\":\""+img+"\"},\"configure\": {\"dataType\": 50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}";
	    try {
	    	
	    	HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	//System.out.println(EntityUtils.toString(response.getEntity()));
	    	String result = EntityUtils.toString(response.getEntity());
	    	System.out.println(result);
	    	 JSONObject jObject =  JSONObject.fromObject(result);
	    	 JSONArray jarray = jObject.optJSONArray("outputs");
	    	 for (int i = 0; i < jarray.size(); i++) {
				JSONObject subject =  jarray.getJSONObject(i);
				String outputValue = subject.get("outputValue").toString();
				/*outputValue={
						  "dataType": 50,
						  "dataValue": {
						    "address": "山西省xxxx041",
						    "birth": "19980908",
						    "config_str": "{\"side\":\"face\"}",
						    "face_rect": {
						      "angle": -90,
						      "center": {
						        "x": 446,
						        "y": 210
						      },
						      "size": {
						        "height": 92,
						        "width": 84
						      }
						    },
						    "name": "xxx",
						    "nationality": "汉",
						    "num": "1411251998xxxxxxX",
						    "request_id": "20170806200141_c5338b2c4fd601ea7f00c4dcc5c8ee0e",
						    "sex": "男",
						    "success": true
						  }
						}*/
				JSONObject j_outputValue = JSONObject.fromObject(outputValue);
				String dataValue = j_outputValue.getString("dataValue");
				JSONObject j_dataValue = JSONObject.fromObject(dataValue);
				customerInfo = new CustomerInfo();
				customerInfo.setName(j_dataValue.getString("name").toString());  //姓名
				String date = j_dataValue.getString("birth").toString();
				customerInfo.setBorn(date);
				SimpleDateFormat format = new SimpleDateFormat(date);
				Date birth = format.parse(date);
				customerInfo.setBirth(birth);//生日
				
				customerInfo.setAddress(j_dataValue.getString("address").toString());
				customerInfo.setNationality(j_dataValue.getString("nationality").toString());
				customerInfo.setNum(j_dataValue.getString("num").toString());
				customerInfo.setSex(j_dataValue.getString("sex").toString());
				
			}
	    	System.out.println(customerInfo.getName());
	    	String jsonc = JSON.toJSONString(customerInfo);
	    	
			HttpServletResponse response1 = ServletActionContext.getResponse();
			response1.setContentType("text/html;charset=utf-8");
			PrintWriter out = response1.getWriter();
			out.print(jsonc);
			
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
		
	}

posted @ 2017-08-20 22:13  穆雄雄  阅读(1437)  评论(0编辑  收藏  举报