SpringBoot集成腾讯云OCR实现身份证识别

OCR身份证识别

官网地址:https://cloud.tencent.com/document/product/866/33524
在这里插入图片描述

身份信息认证(二要素核验)

官网地址:https://cloud.tencent.com/document/product/1007/33188
在这里插入图片描述

代码实现

引入依赖

<dependency>
   <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>4.0.11</version>
</dependency>

工具类

package com.qiangesoft.ocr.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;

/**
 * 腾讯云ocr识别
 *
 * @author qiangesoft
 * @date 2025-03-27
 */
public class TencentCloudOcr {

    private static final String SECRET_ID = "xxx";

    private static final String SECRET_KEY = "xxx ";

    private static final String REGION_NAME = "ap-shanghai";

    /**
     * 调用OCR识别身份证信息
     * https://cloud.tencent.com/document/product/866/33524
     *
     * @param imgUrl
     * @return
     * @throws TencentCloudSDKException
     */
    public static IDCardOCRResponse idCardOCR(String imgUrl) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        OcrClient client = new OcrClient(cred, REGION_NAME, clientProfile);

        IDCardOCRRequest req = new IDCardOCRRequest();
        req.setImageUrl(imgUrl);
        req.setCardSide("FRONT");
//        req.setCardSide("BACK");
        return client.IDCardOCR(req);
    }

    /**
     * 调用身份信息核验API
     * https://cloud.tencent.com/document/product/1007/33188
     *
     * @param name
     * @param idCard
     * @return
     * @throws TencentCloudSDKException
     */
    public static IdCardVerificationResponse idCardVerification(String name, String idCard) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("faceid.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        FaceidClient client = new FaceidClient(cred, REGION_NAME, clientProfile);

        IdCardVerificationRequest req = new IdCardVerificationRequest();
        req.setName(name);
        req.setIdCard(idCard);
        return client.IdCardVerification(req);
    }

    public static void main(String[] args) throws TencentCloudSDKException {
        IDCardOCRResponse idCardOCRResponse = idCardOCR("https://www.fsf.com");
        IdCardVerificationResponse idCardVerificationResponse = idCardVerification("xxx", "22222");
    }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Meteor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值