当前位置 博文首页 > RemainderTime:工具方法(二)-java实现腾讯人脸核身功能(bizT
该工具方法主要用于开发者使用腾讯云服务中的人脸核身功能进行人脸与身份证对比认证操作
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
注:目前腾讯云人脸与身份证对比认证由前端实现,但需要一个bizToken参数,需要后端提供
官方参考文档
//腾讯云密匙
private static String secretId = "";
private static String secretKey = "";
private static String bizTokenUrl = "faceid.tencentcloudapi.com";
//人脸核身业务流程RuleID(在人脸核身控制台查看)
private static String ruleId = "";
public static ResponseData getTencentBizToken() {
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(bizTokenUrl);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
FaceidClient client = new FaceidClient(cred, "", clientProfile);
DetectAuthRequest req = new DetectAuthRequest();
req.setRuleId(ruleId);
DetectAuthResponse resp = null;
try {
resp = client.DetectAuth(req);
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
String toJsonString = DetectAuthRequest.toJsonString(resp);
LogUtil.info("腾讯云获取bizToken接口返回值:{}",toJsonString);
JSONObject jsonObject = JSON.parseObject(toJsonString);
return ResponseData.success(jsonObject.getString("BizToken"));
}
- 前端实现用户身份证上传至腾讯云
- 前端上传活体人脸照至腾讯云
- 腾讯云进行人脸对比
- 对比成功则返回对比用户的专属bizToken
可用于前端对比成功调用接口,将用户信息存储到数据库
官方参考文档
/**
*
* @param bizToken 用户专属bizToken
* @return
*/
public static JSONObject getTencentCompare(String bizToken){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("faceid.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
FaceidClient client = new FaceidClient(cred, "", clientProfile);
GetDetectInfoRequest req = new GetDetectInfoRequest();
req.setBizToken(bizToken);
req.setInfoType("0");
req.setRuleId(ruleId );
GetDetectInfoResponse resp = client.GetDetectInfo(req);
String resultInfo = GetDetectInfoResponse.toJsonString(resp);
if(StringUtils.isEmpty(resultInfo)){
log.error("身份信息请求失败--------------");
return null;
}
JSONObject jsonObject = JSONObject.parseObject(resultInfo);
String detectInfo = jsonObject.getString("DetectInfo");
if(org.apache.commons.lang.StringUtils.isBlank(detectInfo)){
log.error("身份信息获取失败--------------");
return null;
}
JSONObject jsonObject2 = JSONObject.parseObject(detectInfo);
if(jsonObject2 == null){
return null;
}
return jsonObject2;
} catch (TencentCloudSDKException e) {
e.printStackTrace();
return null;
}
}
搞定。
cs