h5接入腾讯云人脸核身

一.自助接入步骤。 

1.登录腾讯云开通人脸核身服务。 

 2.选择微信h5。

3.填写用户授权信息,选择对比源。

4.在调用实名核身鉴权接口时,必须传入认证人姓名和身份证号。

5.配置结果。

二.时序图

三.后端接口

 service

package com.ynfy.buss.exam.faceverify.service.impl;

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.*;
import com.ynfy.buss.exam.faceverify.service.IFaceVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @Description: 人脸核验
 * @Author: yangfeng
 * @Date: 2024-04-01
 * @Version: V1.0
 */
@Slf4j
@Service
public class FaceVerifyServiceImpl implements IFaceVerifyService {

    @Value("${tencent.cloud.faceApi}")
    private String faceApi;

    @Value("${tencent.cloud.secretId}")
    private String secretId;

    @Value("${tencent.cloud.secretKey}")
    private String secretKey;

    @Value("${tencent.cloud.ruleId}")
    private String ruleId;

    @Value("${tencent.cloud.redirectUrl}")
    private String redirectUrl;

    /**
     * 实名核身鉴权
     *
     * @return
     */
    @Override
    public DetectAuthResponse detectAuth(String examId, String realName, String idCard, String imageBase64) {
        DetectAuthResponse resp = null;
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(faceApi);
            httpProfile.setConnTimeout(10); // 请求连接超时时间,单位为秒(默认60秒)
            httpProfile.setWriteTimeout(10);  // 设置写入超时时间,单位为秒(默认0秒)
            httpProfile.setReadTimeout(10);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient client = new FaceidClient(cred, "", clientProfile);
            DetectAuthRequest req = new DetectAuthRequest();
            req.setRuleId(ruleId);
            req.setIdCard(idCard);
            req.setName(realName);
            req.setImageBase64(imageBase64);
            req.setRedirectUrl(redirectUrl + examId + "&isNeedFaceDetect=" + true);
            resp = client.DetectAuth(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return resp;
    }


    /**
     * 获取实名核身结果信息
     *
     * @param bizToken
     * @return
     */
    @Override
    public GetDetectInfoEnhancedResponse getDetectInfo(String bizToken) {
        GetDetectInfoEnhancedResponse resp = null;
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(faceApi);
            httpProfile.setConnTimeout(10); // 请求连接超时时间,单位为秒(默认60秒)
            httpProfile.setWriteTimeout(10);  // 设置写入超时时间,单位为秒(默认0秒)
            httpProfile.setReadTimeout(10);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient client = new FaceidClient(cred, "", clientProfile);
            GetDetectInfoEnhancedRequest req = new GetDetectInfoEnhancedRequest();
            req.setBizToken(bizToken);
            req.setInfoType("0");
            req.setRuleId(ruleId);
            resp = client.GetDetectInfoEnhanced(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 实名核身是否通过
     *
     * @param bizToken
     * @return
     */
    @Override
    public boolean faceDetectIsPass(String bizToken) {
        GetDetectInfoEnhancedResponse resp = getDetectInfo(bizToken);
        if (!Objects.isNull(resp)) {
            DetectInfoText text = resp.getText();
            return !Objects.isNull(text) && text.getErrCode().intValue() == 0;
        }
        return false;
    }


}

 controller

package com.ynfy.app.api.v1.controller;

import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.buss.exam.faceverify.service.IFaceVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.TokenUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 人脸核验(目前仅适用微信h5)
 * @Author: yangfeng
 * @Date: 2024-04-01
 * @Version: V1.0
 */
@Api(tags = "人脸核验")
@RestController
@RequestMapping("/api/v1/faceVerify")
@Slf4j
public class ApiFaceVerifyController extends ApiBaseController {
    @Autowired
    private IFaceVerifyService faceVerifyService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 实名核身鉴权
     *
     * @return
     */
    @ApiOperation(value = "实名核身鉴权", notes = "实名核身鉴权")
    @GetMapping(value = "/detectAuth")
    public Result<?> detectAuth(@RequestParam String examId) {
        log.info("考试:{},实名核身鉴权开始...", examId);
        SysUser user = sysUserService.getUserByName(TokenUtil.getUserName(TokenUtil.getToken(request)));
        log.info("user:{}", user.toString());
        return Result.OK(faceVerifyService.detectAuth(examId, user.getRealname(), user.getIdCard(), user.getImageBase64()));
    }

    /**
     * 获取实名核身结果信息
     *
     * @param bizToken
     * @return
     */
    @IgnoreAuth
    @ApiOperation(value = "获取实名核身结果信息", notes = "获取实名核身结果信息")
    @GetMapping(value = "/getDetectInfo")
    public Result<?> getDetectInfo(@RequestParam String bizToken) {
        log.info("获取实名核身结果信息,bizToken:{}", bizToken);
        return Result.OK("", faceVerifyService.getDetectInfo(bizToken));
    }

    /**
     * 实名核身是否通过
     *
     * @param bizToken
     * @return
     */
    @IgnoreAuth
    @ApiOperation(value = "实名核身是否通过", notes = "实名核身是否通过")
    @GetMapping(value = "/faceDetectIsPass")
    public Result<?> faceDetectIsPass(@RequestParam String bizToken) {
        log.info("调用实名核身是否通过接口,bizToken:{}", bizToken);
        return Result.OK(faceVerifyService.faceDetectIsPass(bizToken));
    }

}

 四.h5前端。

点击“开始考试”后,读取本次考试 是否需要进行人脸核验。如果需要,调用实名核身鉴权,接口会返回用于发起核身流程的URL。

    async created() {
			this.initFormData();

			// #ifdef H5
			if (this.isNeedFaceDetect && JSON.parse(this.isNeedFaceDetect)) {
				//实名核身是否通过
				const data = await apiFaceDetectIsPass(this.bizToken)
				if (data) {
					//通过的话则加载考试信息
					this.loadExam()
				}
			}
			// #endif 
		},
        //开始考试
			async startExam() {
				//h5需要人脸核身验证后才可以开始
				// #ifdef H5
				//本考试是否支持人脸核身
				if (!!this.form.globalFaceEnable && !!this.form.faceDetectEnable) {
					const data = await apiDetectAuth(this.examId)
					window.location.href = data.url
					this.isNeedFaceDetect = true
				} else {
					this.loadExam()
				}
				// #endif 

				//非h5直接进入考试
				// #ifndef H5
				this.loadExam()
				// #endif 
			},

跳转url核验,核验完成后,会根据实名核身鉴权接口传入的RedirectUrl,

跳转前端页面,监听BizToken参数和自助传入的参数,获取核验结果。如果通过,则可以进行考试。

 五.效果。

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## 使用前准备​ 1. 前往注册: [腾讯云账号注册](https://cloud.tencent.com/register) (详细指引见 [注册腾讯云](https://cloud.tencent.com/document/product/378/9603)) 2. 取得存储桶名称 **BucketName**: 请前往 [创建存储桶](https://cloud.tencent.com/document/product/460/10637) 3. 取得 **APPID**、**SecretId**、**SecretKey**:请前往 [云API密钥](https://console.cloud.tencent.com/cam/capi) ,点击“新建密钥” ## 快速体验 1. 修改文件 src/main/java/com/qcloud/image/demo/Demo.java 的 main() 方法,填入上述申请到的 **APPID**、**SecretId**、**SecretKey**、**BucketName** 2. 导入到 IDE:工程用 Maven 构建,以 Intellij IDEA 为例,导入方式为:Import Project -> 选择工程目录 -> Import project from external model -> Maven 3. 运行:Demo.java 右键,Run Demo.main() ## 使用简介 ### 初始化 ```java ImageClient imageClient = new ImageClient(APPID, SecretId, SecretKey); ``` ### 设置代理 根据实际网络环境,可能要设置代理,例如: ```java Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); imageClient.setProxy(proxy); ``` ### 使用 SDK 提供功能如下: **图像识别**:鉴黄,标签 **文字识别(OCR)**:身份证,名片,通用,驾驶证行驶证,营业执照,银行卡,车牌号 **人脸识别**:人脸检测,五官定位,个体信息管理,人脸验证,人脸对比及人脸检索 **人脸核身**:照片核身(通过照片和身份证信息),获取唇语验证码(用于活体核身),活体核身(通过视频和照片),活体核身(通过视频和身份证信息) ```java // 调用车牌识别API示例 String imageUrl = "http://youtu.qq.com/app/img/experience/char_general/icon_ocr_license_3.jpg"; String result = imageClient.ocrPlate(new OcrPlateRequest("bucketName", imageUrl)); System.out.println(result); ``` 更多例子详情可参见 [Demo.java](https://github.com/tencentyun/image-java-sdk-v2.0/blob/master/src/main/java/com/qcloud/image/demo/Demo.java) 的代码。 ## 集成到你的项目中 ### 获得 SDK jar 文件 1. 直接使用 release/*-with-dependencies.jar 2. 自行编译:在工程根目录下执行命令 `mvn assembly:assembly`,编译结果见 target/*-with-dependencies.jar ### 导入 jar 文件 根据项目具体情况导入 *-with-dependencies.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值