当前位置: 首页 > news >正文

SpringBoot集成阿里云OCR实现身份证识别

OCR身份证识别

官网地址:https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeidcard
在这里插入图片描述

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

官网地址:https://api.aliyun.com/document/Dytnsapi/2020-02-17/CertNoTwoElementVerification
在这里插入图片描述

代码实现

引入依赖

<!-- 阿里云ocr -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>ocr_api20210707</artifactId>
    <version>3.1.1</version>
</dependency>

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dytnsapi20200217</artifactId>
    <version>2.10.0</version>
</dependency>

工具类

package com.qiangesoft.ocr.utils;

import com.aliyun.dytnsapi20200217.models.*;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

/**
 * 阿里云ocr识别
 *
 * @author qiangesoft
 * @date 2025-03-27
 */
public class AliyunOcr {

    private static final String ACCESS_KEY_ID = "xxx";

    private static final String ACCESS_KEY_SECRET = "xxx";

    /**
     * 调用OCR识别身份证信息
     * https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeidcard
     *
     * @param imgUrl
     * @return
     * @throws Exception
     */
    public static RecognizeIdcardResponse recognizeIDCard(String imgUrl) throws Exception {
        Config config = new Config()
                .setAccessKeyId(System.getenv(ACCESS_KEY_ID))
                .setAccessKeySecret(System.getenv(ACCESS_KEY_SECRET));
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);

        RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest();
        recognizeIdcardRequest.setUrl(imgUrl);
        RuntimeOptions runtime = new RuntimeOptions();
        return client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
    }

    /**
     * 调用身份信息核验API
     * https://api.aliyun.com/document/Dytnsapi/2020-02-17/CertNoTwoElementVerification
     *
     * authCode从这里获取:https://dytns.console.aliyun.com/analysis/square?spm=api-workbench.api_explorer.0.0.14c610915R4RUL
     *
     * @param name
     * @param idCard
     * @return
     * @throws TencentCloudSDKException
     */
    public static CertNoTwoElementVerificationResponse idCardVerification(String name, String idCard) throws Exception {
        Config config = new Config()
                .setAccessKeyId(System.getenv(ACCESS_KEY_ID))
                .setAccessKeySecret(System.getenv(ACCESS_KEY_SECRET));
        config.endpoint = "dytnsapi.aliyuncs.com";
        com.aliyun.dytnsapi20200217.Client client = new com.aliyun.dytnsapi20200217.Client(config);

        CertNoTwoElementVerificationRequest certNoTwoElementVerificationRequest = new CertNoTwoElementVerificationRequest()
                .setAuthCode("your_value")
                .setCertName(name)
                .setCertNo(idCard);
        return client.certNoTwoElementVerificationWithOptions(certNoTwoElementVerificationRequest, new RuntimeOptions());
    }

}

相关文章:

  • 23种设计模式-桥接(Bridge)设计模式
  • 为AI聊天工具添加一个知识系统 之152 当今AI模型和AI工具应用中的核心矛盾
  • osgVerse学习:1、使用GPU烘焙处理倾斜摄影顶层合并时的接缝问题
  • js 对象深拷贝的五种方法
  • java-IO流笔记
  • android 一步完成 aab 安装到手机
  • 【Qt】QByteArray详解
  • 【HTML 基础教程】HTML 属性
  • antd-vue Table组件翻页后保留上一页已选的数据
  • 比特币等虚拟货币实时价格使用说明,数字货币价格获取,k线获取,实时价格获取
  • Java 8-17核心特性全景解析之Java12
  • Spring IOC/DI的依赖注入方式及示例
  • 索引(重点)
  • 地理信息可视化技术大全【WebGIS 技术文档大全】
  • 科大讯飞语音转文字STT--unity
  • mysql死锁排查解决
  • Mysql 回表查询,什么是回表查询,如何拒绝sql查询时的回表问题
  • 【Django】教程-3-数据库相关介绍
  • C++中的判断与循环
  • Python 魔术方法功能分类指南
  • wordpress怎么增加页面/短视频入口seo
  • 做网站广告送报纸广告/google浏览器官网下载
  • 做网站页面设计报价/百度链接
  • 虎门做英文网站/网络销售公司
  • 广州网站推广策划/北京网站优化培训
  • 做网站界面需要注意什么/seo优化方案