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 str = new ArrayList();
		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;
	}

 

你可能感兴趣的:(OCR识别)