从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测试

相关推荐
开开心心就好7 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
沉到海底去吧Go1 天前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
撞南墙者1 天前
如何让AI自己检查全文?使用OCR和LLM实现自动“全文校订”(可DIY校订规则)
人工智能·ocr
沉到海底去吧Go3 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
TextIn智能文档云平台3 天前
从OCR到Document Parsing,AI时代的非结构化数据处理发生了什么改变?
人工智能·自然语言处理·ocr·pdf解析·textin·复杂文档解析
mulannanlu4 天前
免费开源Umi-OCR,离线使用,批量精准!
ocr
凌康ACG4 天前
易语言使用OCR
c++·yolo·c#·ocr·易语言
开开心心就好5 天前
小巧实用,Windows文件夹着色软件推荐
java·开发语言·前端·决策树·c#·ocr·动态规划
郑知鱼5 天前
【拥抱鸿蒙】HarmonyOS NEXT实现双路预览并识别文字
华为·ocr·harmonyos·鸿蒙·移动端·鸿蒙next·ohos
rick_grace8 天前
使用 pytesseract 构建一个简单 OCR demo
ocr