微信小程序-人脸识别
一、使用条件
由于人脸核身功能涉及到用户的敏感、隐私信息,因此调用此接口的小程序,需要满足一定的条件。即:小程序的主体以及类目,需要在限定的类目范围内,且与小程序的业务场景一致。开展的业务也需要是国家相关法规、政策规定的需要“实名办理”的相关业务(其他未在范围内的业务,则暂不支持)。
接入步骤参考链接
https://developers.weixin.qq.com/community/business/doc/000442d352c1202bd498ecb105c00d.
二、代码示例
1.检查设备是否支持人脸检测
faceRecognition: function () {
let that = this
wx.checkIsSupportFacialRecognition({
checkAliveType: 2, // 先检查是否可以屏幕闪烁,不可以则自动为读数字
success: function (res) {
if (res.errCode === 0 || res.errMsg === "checkIsSupportFacialRecognition:ok") {
//调用人脸识别
that.startface(that.data.name, that.data.idCard, ); //身份证名称,身份证号码
return;
}
wx.showToast({
title: '微信版本过低,暂时无法使用此功能,请升级微信最新版本',
icon: 'none',
duration: 2000
})
},
fail: res => {
wx.showToast({
title: '微信版本过低,暂时无法使用此功能,请升级微信最新版本',
icon: 'none',
duration: 2000
})
}
})
},
2.开始人脸识别
startface(name, idcard) {
let that = this
wx.startFacialRecognitionVerify({
name: name, //身份证名称
idCardNumber: idcard, //身份证号码
success: function (res) {
if (res.errCode === 0 || res.errMsg === "startFacialRecognitionVerify:ok") {
var verifyResult = res.verifyResult; //认证结果
console.log(verifyResult, '认证结果')
// 执行与后台交互操作
}
},
checkAliveType: 2, //屏幕闪烁(人脸核验的交互方式,默认0,读数字)
fail: err => {
console.log(err, 'err')
// wx.showToast({
// title: '请保持光线充足,面部正对手机,且无遮挡',
// icon: 'none',
// duration: 2000
// })
}
})
},