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;
    }
}
@Autowired
    private OkHttpClient okHttpClient;
    
    @Autowired
    private 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;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值