ocr-身份证正反面识别
在阿里云官网,申请一个token
[阿里官方]身份证OCR文字识别_API专区_云市场-阿里云 (aliyun.com)
观察一下post请求body部分json字符串,我们根据这个创建一个java对象
先默认是人像面
public class IdentityBody {public String image;class configure {public String side = "face";public boolean quality_info;}
}
@Autowiredprivate OkHttpClient okHttpClient;@Autowiredprivate ObjectMapper objectMapper;private String cardPath = "D:\\image\\card1.jpg";private String appcode = "c8518f45d5334300b73c638e299820ab";public String getIdentityData() throws IOException {IdentityBody identityBody = new IdentityBody();identityBody.image = ImageToBase64.imageToBase64(cardPath);RequestBody requestBody = RequestBody.create(objectMapper.writeValueAsString(identityBody),MediaType.get("application/json; charset=utf-8"));Request request = new Request.Builder().url("https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json").addHeader("Authorization", "APPCODE " + appcode).addHeader("Content-Type", "application/json; charset=UTF-8").post(requestBody).build();try (Response response = okHttpClient.newCall(request).execute()) {if (!response.isSuccessful()) {String errorBody = response.body().string();throw new IOException("API请求失败:\n" +"状态码: " + response.code() + "\n" +"错误信息: " + response.message() + "\n" +"响应体: " + errorBody);}String ret = response.body().string();System.out.println("识别结果: " + ret);return ret;}}