百度AI身份证识别接口开发

1、准备工作:
创建应用并勾选身份证识别接口,应用创建见百度AI应用的创建以及AK、SK的获取如下:

在这里插入图片描述
API Key、Secret Key见应用列表

在这里插入图片描述
2、实现代码如下:

public static Map<String, Object> Idcard(String accessToken,String fileUrl) {
		// 身份证识别url
		String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
		// 本地图片路径
		//String filePath = "#####本地文件路径#####";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			byte[] imgData = FileUtil.readFileByBytes(fileUrl);
			String imgStr = Base64Util.encode(imgData);
			// 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
			String params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
					+ URLEncoder.encode(imgStr, "UTF-8");
			/**
			 * 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
			 */
			// String accessToken = "#####调用鉴权接口获取的token#####";
			String result = HttpUtil.post(idcardIdentificate, accessToken, params);
			System.out.println(result);
			JsonObject jObject =new JsonParser().parse(result).getAsJsonObject();
			/*normal-识别正常
			reversed_side-身份证正反面颠倒
			non_idcard-上传的图片中不包含身份证
			blurred-身份证模糊
			other_type_card-其他类型证照
			over_exposure-身份证关键字段反光或过曝
			over_dark-身份证欠曝(亮度过低)
			unknown-未知状态*/
			String imageStatus = jObject.get("image_status").getAsString();
			System.out.println("imageStatus:"+imageStatus);
			String message = "";
			if("normal".equals(imageStatus)) {
				message = "识别正常";
				JsonObject resultObject = jObject.getAsJsonObject("words_result");
				String realName = resultObject.getAsJsonObject("姓名").get("words").getAsString();
				String idNumber = resultObject.getAsJsonObject("公民身份号码").get("words").getAsString();
				map.put("realName", realName);
				map.put("idNumber", idNumber);
				map.put("message", message);
			}else if("reversed_side".equals(imageStatus)) {
				message = "身份证正反面颠倒,请重新上传";
			}else if("non_idcard".equals(imageStatus)) {
				message = "上传的图片中不包含身份证,请重新上传";
			}else if("blurred".equals(imageStatus)) {
				message = "身份证模糊,请重新上传";
			}else if("other_type_card".equals(imageStatus)) {
				message = "其他类型证照,请重新上传";
			}else if("over_exposure".equals(imageStatus)) {
				message = "身份证关键字段反光或过曝,请重新上传";
			}else if("over_dark".equals(imageStatus)) {
				message = "身份证欠曝(亮度过低),请重新上传";
			}else if("unknown".equals(imageStatus)) {
				message = "未知状态,请重新上传";
			}
			map.put("message", message);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

3、测试代码如下:

public static void main(String[] args) {
		TestRenLian testRenLian = new TestRenLian();
		String access_token = testRenLian.getAuth();
		// 身份证识别
		String fileUrl = "C:/Users/32249/Desktop/a.jpg";
		Idcard(access_token,fileUrl);
	}

4、token的获取,代码如下:

/**
	 * 获取权限token
	 * 
	 * @return 返回示例: { "access_token":
	 *         "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
	 *         "expires_in": 2592000 }
	 */
	public static String getAuth() {
		// 官网获取的 API Key 更新为你注册的
		String clientId = "*********";
		// 官网获取的 Secret Key 更新为你注册的
		String clientSecret = "**********";
		return getAuth(clientId, clientSecret);
	}

	/**
	 * 获取API访问token 该token有一定的有效期,需要自行管理,当失效时需重新获取.
	 * 
	 * @param ak - 百度云官网获取的 API Key
	 * @param sk - 百度云官网获取的 Securet Key
	 * @return assess_token 示例:
	 *         "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
	 */
	public static String getAuth(String ak, String sk) {
		// 获取token地址
		String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
		String getAccessTokenUrl = authHost
				// 1. grant_type为固定参数
				+ "grant_type=client_credentials"
				// 2. 官网获取的 API Key
				+ "&client_id=" + ak
				// 3. 官网获取的 Secret Key
				+ "&client_secret=" + sk;
		try {
			URL realUrl = new URL(getAccessTokenUrl);
			// 打开和URL之间的连接
			HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			// 获取所有响应头字段
			Map<String, List<String>> map = connection.getHeaderFields();
			// 遍历所有的响应头字段
			for (String key : map.keySet()) {
				System.err.println(key + "--->" + map.get(key));
			}
			// 定义 BufferedReader输入流来读取URL的响应
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String result = "";
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			/**
			 * 返回结果示例
			 */
			System.err.println("result:" + result);
			JSONObject jsonObject = new JSONObject(result);
			String access_token = jsonObject.getString("access_token");
			return access_token;
		} catch (Exception e) {
			System.err.printf("获取token失败!");
			e.printStackTrace(System.err);
		}
		return null;
	}

5、注意事项:百度的身份证识别接口不支持传网图,例如:http://*******:8081//file/a.jpg,只能传本地图片

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值