1. 程式人生 > >OCR Java華為雲身份證識別

OCR Java華為雲身份證識別

之前寫了關於百度雲的身份證識別,現在再附上關於華為雲的身份證識別:

controller層:

/**
	 * 讀取身份證資訊
	 * 
	 * @param file
	 * @return
	 */
	@ApiOperation(value ="讀取身份證資訊", notes ="")
	@PostMapping("/getIdInfo")
	public BaseResult getIdInfo(@RequestParam("file") MultipartFile file) {
		try {
			return commitSingleService.getIdInfo(file);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult(Constants.EXCEPTION_CODE, Constants.EXCEPTION_MSG);
		}
	}

實現層:

@Override
	public BaseResult getIdInfo(MultipartFile file) throws Exception {
		// 獲取華為雲token
		JSONObject tokenJson = generateJson();
		//huaTokenUrl=https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens
		String huaToken = HttpUtils.getTokenPost(env.getProperty("huaTokenUrl"), tokenJson, "UTF-8");
		// 呼叫身份證識別介面
		JSONObject json = new JSONObject();
		json.put("image", new BASE64Encoder().encode(file.getBytes()));
		json.put("side", "front");
		//huaIdCardUrl=https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/id-card
		String result = HttpUtils.getIdCardPost(env.getProperty("huaIdCardUrl"), huaToken, json.toString(), "UTF-8");
		JSONObject resultJson = JSON.parseObject(result);
		if (resultJson.containsKey("error_code")) {
			logger.info("華為雲身份證識別介面提示資訊:" + resultJson.getString("error_msg"));
			return new BaseResult(Constants.ERROR_CODE, Constants.ERROR_MSG);
		}
		return new BaseResult(Constants.SUCCESS_CODE, Constants.SUCCESS_MSG, resultJson.getJSONObject("result"));
	}
/**
	 * 組裝華為token的json串
	 * 
	 * @return
	 */
	private JSONObject generateJson() {
		JSONObject json = new JSONObject();
		JSONObject auth = new JSONObject();

		JSONObject project = new JSONObject();
		project.put("name", "cn-north-1");
		JSONObject scope = new JSONObject();
		scope.put("project", project);
		auth.put("scope", scope);

		JSONObject identity = new JSONObject();
		JSONObject domain = new JSONObject();
		domain.put("name", "***");
		JSONObject user = new JSONObject();
		user.put("name", "***");
		user.put("password", "*******");
		user.put("domain", domain);
		JSONObject password = new JSONObject();
		password.put("user", user);
		identity.put("password", password);
		List<String> str = new ArrayList<String>();
		str.add("password");
		identity.put("methods", str);
		auth.put("identity", identity);

		json.put("auth", auth);
		return json;
	}

HttpUtils工具類中的程式碼:

/**
	 * https請求 華為雲token
	 * @param url
	 * @param jsonObject
	 * @param charset
	 * @return
	 */
	public static String getTokenPost(String url, JSONObject jsonObject, String charset) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			httpPost.addHeader("Content-Type", "application/json");
			StringEntity entity = new StringEntity(jsonObject.toString(), charset);
			entity.setContentType("application/json");
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			if (response != null) {
				result = response.getFirstHeader("X-Subject-Token").getValue();
//				HttpEntity resEntity = response.getEntity();
//				if (resEntity != null) {
//					result = EntityUtils.toString(resEntity, charset);
//				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	/**
	 * https 華為身份證識別介面
	 * @param url
	 * @param token
	 * @param jsonParam
	 * @param charset
	 * @return
	 */
	public static String getIdCardPost(String url,String token ,String jsonParam, String charset) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			httpPost.addHeader("Content-Type", "application/json");
			httpPost.addHeader("X-Auth-Token", token);
			StringEntity entity = new StringEntity(jsonParam, charset);
			entity.setContentType("application/json");
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}