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


    目录

    1.申请腾讯OCR权限

    2.代码思路

    3.Postman测试​


    1.申请腾讯OCR权限

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

    2.代码思路

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

    controller

    1. /**
    2. * 身份证识别
    3. * @param path
    4. * @return
    5. */
    6. @PostMapping("/IDCardOCR")
    7. public Result IDCardOCR(String path,Integer cardSide) {
    8. IDCardResponse idCardResponse = idCardOCRService.identifyIDCardByUrl(path, cardSide);
    9. return Result.success("校验成功",idCardResponse);
    10. }

    serviceImpl

    1. package com.zsp.quartz.common;
    2. import cn.hutool.core.date.DateUtil;
    3. import cn.hutool.core.util.StrUtil;
    4. import com.tencentcloudapi.common.Credential;
    5. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
    6. import com.tencentcloudapi.common.profile.ClientProfile;
    7. import com.tencentcloudapi.common.profile.HttpProfile;
    8. import com.tencentcloudapi.ocr.v20181119.OcrClient;
    9. import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
    10. import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
    11. import com.zsp.quartz.Exception.CustomException;
    12. import lombok.extern.slf4j.Slf4j;
    13. import org.springframework.stereotype.Service;
    14. /**
    15. * 腾讯身份证识别接口
    16. */
    17. @Service
    18. @Slf4j
    19. public class TencentIDCardOCRServiceImpl implements IDCardOCRService {
    20. private static String secretId = "xxx";
    21. private static String secretKey = "xxx";
    22. private static String regionName = "ap-shanghai";
    23. @Override
    24. public IDCardResponse identifyIDCardByUrl(String imgUrl, Integer cardSide) {
    25. IDCardOCRRequest req = new IDCardOCRRequest();
    26. req.setImageUrl(imgUrl);
    27. if(cardSide != null) {
    28. if (cardSide == 1) {
    29. req.setCardSide("FRONT");
    30. } else if (cardSide == 2) {
    31. req.setCardSide("BACK");
    32. }
    33. }
    34. return idCardAction(req);
    35. }
    36. private IDCardResponse idCardAction(IDCardOCRRequest req) {
    37. Credential cred = new Credential(secretId, secretKey);
    38. // 实例化一个http选项,可选的,没有特殊需求可以跳过
    39. HttpProfile httpProfile = new HttpProfile();
    40. httpProfile.setEndpoint("ocr.tencentcloudapi.com");
    41. // 实例化一个client选项,可选的,没有特殊需求可以跳过
    42. ClientProfile clientProfile = new ClientProfile();
    43. clientProfile.setHttpProfile(httpProfile);
    44. OcrClient client = new OcrClient(cred, regionName,clientProfile);
    45. IDCardOCRResponse res = null;
    46. try {
    47. res = client.IDCardOCR(req);
    48. } catch (TencentCloudSDKException e) {
    49. throw new CustomException("校验失败");
    50. }
    51. if(res != null) {
    52. IDCardResponse idCardResponse = new IDCardResponse();
    53. idCardResponse.setAddress(res.getAddress());
    54. idCardResponse.setAuthority(res.getAuthority());
    55. if(!StrUtil.isEmpty(res.getBirth())) {
    56. idCardResponse.setBirth(DateUtil.parseDate(res.getBirth()));
    57. }
    58. idCardResponse.setIdNum(res.getIdNum());
    59. idCardResponse.setName(res.getName());
    60. idCardResponse.setNation(res.getNation());
    61. if("男".equals(res.getSex())) {
    62. idCardResponse.setSex(1);
    63. } else if("女".equals(res.getSex())) {
    64. idCardResponse.setSex(2);
    65. } else {
    66. idCardResponse.setSex(0);
    67. }
    68. idCardResponse.setValidDate(res.getValidDate());
    69. return idCardResponse;
    70. }
    71. return null;
    72. }
    73. }

    VO层

    1. package com.zsp.quartz.common;
    2. import com.alibaba.fastjson.annotation.JSONField;
    3. import com.fasterxml.jackson.annotation.JsonFormat;
    4. import lombok.Data;
    5. import java.util.Date;
    6. @Data
    7. public class IDCardResponse {
    8. /**
    9. * 姓名(人像面)
    10. */
    11. @JSONField(name = "Name")
    12. private String name;
    13. /**
    14. * 性别(人像面)
    15. */
    16. @JSONField(name = "Sex")
    17. private Integer sex;
    18. /**
    19. * 民族(人像面)
    20. */
    21. @JSONField(name = "Nation")
    22. private String nation;
    23. /**
    24. * 出生日期(人像面)
    25. */
    26. @JSONField(name = "Birth")
    27. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    28. private Date birth;
    29. /**
    30. * 地址(人像面)
    31. */
    32. @JSONField(name = "Address")
    33. private String address;
    34. /**
    35. * 身份证号(人像面)
    36. */
    37. @JSONField(name = "IdNum")
    38. private String idNum;
    39. /**
    40. * 发证机关(国徽面)
    41. */
    42. @JSONField(name = "Authority")
    43. private String authority;
    44. /**
    45. * 证件有效期(国徽面)
    46. */
    47. @JSONField(name = "ValidDate")
    48. private String validDate;
    49. }

    3.Postman测试

  • 相关阅读:
    【Wins+VSCode】配置C++开发环境
    yaml文件详解
    数据结构-学习-01-线性表之顺序表-初始化、销毁、清理、获取长度、判断为空、获取元素等实现
    常用的Content-Type对比
    上周热点回顾(10.30-11.5)
    [附源码]java毕业设计文档管理系统
    【云原生】镜像构建实战操作(Dockerfile)
    [附源码]java毕业设计课后作业提交系统关键技术研究与系统实现
    用中文编程工具编写的代码实现如图所示效果,请分享一下用你所学的编程语言写下这个代码,大家一起交流学习
    Go 中的类型断言与静态转换
  • 原文地址:https://blog.csdn.net/m0_55627541/article/details/133907578