从0-1,使用腾讯OCR进行身份证识别

目录

1.申请腾讯OCR权限

2.代码思路

3.Postman测试​


1.申请腾讯OCR权限

获取 secretId 和 secretKey,见上文
从0到1,申请cos服务器并上传图片到cos文件服务器-CSDN博客https://blog.csdn.net/m0_55627541/article/details/133902798

2.代码思路

入参有两个值,第一个为图片的云服务器路径,第二个为版面(正面/反面)

controller

java 复制代码
 /**
     * 身份证识别
     * @param path
     * @return
     */
    @PostMapping("/IDCardOCR")
    public Result IDCardOCR(String path,Integer cardSide) {
        IDCardResponse idCardResponse = idCardOCRService.identifyIDCardByUrl(path, cardSide);
        return Result.success("校验成功",idCardResponse);
    }

serviceImpl

java 复制代码
package com.zsp.quartz.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
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.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import com.zsp.quartz.Exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 腾讯身份证识别接口
 */
@Service
@Slf4j
public class TencentIDCardOCRServiceImpl implements IDCardOCRService {

    private static String secretId = "xxx";
    private static String secretKey = "xxx";
    private static String regionName = "ap-shanghai";
    @Override
    public IDCardResponse identifyIDCardByUrl(String imgUrl, Integer cardSide) {
        IDCardOCRRequest req = new IDCardOCRRequest();
        req.setImageUrl(imgUrl);
        if(cardSide != null) {
            if (cardSide == 1) {
                req.setCardSide("FRONT");
            } else if (cardSide == 2) {
                req.setCardSide("BACK");
            }
        }
        return idCardAction(req);
    }

    private IDCardResponse idCardAction(IDCardOCRRequest req) {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        OcrClient client = new OcrClient(cred, regionName,clientProfile);
        IDCardOCRResponse res = null;
        try {
            res = client.IDCardOCR(req);
        } catch (TencentCloudSDKException e) {
            throw new CustomException("校验失败");
        }

        if(res != null) {
            IDCardResponse idCardResponse = new IDCardResponse();
            idCardResponse.setAddress(res.getAddress());
            idCardResponse.setAuthority(res.getAuthority());
            if(!StrUtil.isEmpty(res.getBirth())) {
                idCardResponse.setBirth(DateUtil.parseDate(res.getBirth()));
            }
            idCardResponse.setIdNum(res.getIdNum());
            idCardResponse.setName(res.getName());
            idCardResponse.setNation(res.getNation());
            if("男".equals(res.getSex())) {
                idCardResponse.setSex(1);
            } else if("女".equals(res.getSex())) {
                idCardResponse.setSex(2);
            } else {
                idCardResponse.setSex(0);
            }
            idCardResponse.setValidDate(res.getValidDate());
            return idCardResponse;
        }
        return null;
    }

}

VO层

java 复制代码
package com.zsp.quartz.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class IDCardResponse {

    /**
     * 姓名(人像面)
     */
    @JSONField(name = "Name")
    private String name;
    /**
     * 性别(人像面)
     */
    @JSONField(name = "Sex")
    private Integer sex;
    /**
     * 民族(人像面)
     */
    @JSONField(name = "Nation")
    private String nation;
    /**
     * 出生日期(人像面)
     */
    @JSONField(name = "Birth")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birth;
    /**
     * 地址(人像面)
     */
    @JSONField(name = "Address")
    private String address;
    /**
     * 身份证号(人像面)
     */
    @JSONField(name = "IdNum")
    private String idNum;
    /**
     * 发证机关(国徽面)
     */
    @JSONField(name = "Authority")
    private String authority;
    /**
     * 证件有效期(国徽面)
     */
    @JSONField(name = "ValidDate")
    private String validDate;
}

3.Postman测试

相关推荐
私人珍藏库4 小时前
[Windows] 天若ocr开源版6.1-增加openai兼容接口
ocr
li三河1 天前
paddlepaddle-gpu3.0.0进行ocr训练
人工智能·ocr·paddlepaddle
SYC_MORE3 天前
无需 OCR,多模态大模型如何“读懂” PDF?——基于 GLM-4V-Flash 的智能文档解析原理剖析
人工智能·pdf·ocr
小娜老师3 天前
Linux(Centos7)下进行OCR识别-pytesseract
flask·ocr
文档伴侣3 天前
体验访答:我的私有知识库新选择
人工智能·aigc·ocr
AI人工智能+3 天前
大模型如何革新银行流水信息抽取
大模型·ocr·文本信息抽取
TG:@yunlaoda360 云老大3 天前
华为云国际站代理商OCR的多语种识别能力可以应用于哪些行业?
人工智能·华为云·ocr
TG:@yunlaoda360 云老大3 天前
华为云国际站代理商OCR的多语种识别能力可以应用于哪些场景?
服务器·华为云·ocr
TTGGGFF3 天前
实用代码工具:Python打造PDF选区OCR / 截图批量处理工具(支持手动/全自动模式)
python·pdf·ocr
真·skysys4 天前
【技术报告解读】DeepSeek-OCR: Contexts Optical Compression
ocr·多模态·deepseek·上下文工程·deepseek-ocr·上下文压缩·上下文光学压缩