1、引入插件
在小程序的app.json中,加入插件。
"plugins": {
"readcard-plugin": {
"version": "2.3.8",
"provider": "wxa2583ebacdb87a6a"
}
}
2、基础库
调试基础库 2.20.3及以上
微信版本:
iOS 暂不支持NFC(微信官方未支持),可使用蓝牙外设读卡器
Android 8.0.6及以上版本,同时支持NFC、蓝牙外设读卡器
3、使用插件功能
3.1、活体检测/人脸比对功能
· 在界面的 **.wxml 添加camera
<camera device-position="front" flash="off" resolution='medium' frame-size='small' style="height: 100%;width: 100%;"/>
界面的其余样式,自行定义
· 在界面 **.js 配置使用插件
const plugin = requirePlugin('readcard-plugin');
var that = this;
Page({
/**
* 页面的初始数据
*/
data: {
type: 1, // 1:仅活体检测 2:活体 + 人脸1:1比对
outTimeMs: 5000, // 超时时间 毫秒(最小3秒)
photoData: null, // (照片数据的字节数组)仅活体检测不需要传比对的照片,人脸人脸1:1比对时,需要传入比对的照片
appid: "", // appid联系我司商务获取
_Setting: {
saveLog: true, // 是否保存日志文件
// 服务器配置
ipPortArray: [{
address: 'test.sfzydq.com',
port: 18181,
canUse: true
}]
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
that = this;
that.authorize();
},
/**
* 向用户申请相机权限
*/
authorize() {
wx.getSetting({
success(res) {
if (!res.authSetting['scope.camera']) {
wx.authorize({
scope: 'scope.camera',
success() {
// 用户已经同意
that.faceLive();
},
fail() {
wx.openSetting({
success(res) {
that.authorize();
}
})
}
})
} else {
that.faceLive();
}
}
})
},
/**
* 开始活体检测
*/
faceLive() {
// 设置APPID (开始读卡之前,必须设置)
plugin.setAppId("appid联系我司商务获取", "", "");
// 设置其他检测参数
plugin.settingFacelive(that.data._Setting);
// 帧数据回调
const context = wx.createCameraContext()
const listener = context.onCameraFrame((frame) => {
plugin.onCameraFrame(frame);
});
// 开始检测
plugin.startFacelive(listener, that.data.type, that.data.outTimeMs, that.data.photoData)
.then((bean) => {
console.log(bean);
var content = "";
if (bean.code != "00") { // 失败
content = bean.msg;
} else {
content = "结果:" + bean.code + "\n活体分值:" + bean.liveGrade;
if (that.data.type == 2) {
content += "\n人脸比对分值:" + bean.faceGrade;
}
}
wx.showModal({
title: '温馨提示',
content: content,
showCancel: false,
success: (res) => {
if (res.confirm) { // 用户点击确定
wx.navigateBack();
}
}
});
}).catch((err) => {
console.log(err);
var content = "";
try {
if (typeof err == "object") {
let bean = err;
content = bean.msg;
content += "\n活体分值:" + bean.liveGrade;
if (that.data.type == 2) {
content += "\n人脸比对分值:" + bean.faceGrade;
}
} else {
content = err;
}
} catch (error) {
content = JSON.stringify(error);
}
wx.showModal({
title: '温馨提示',
content: content,
showCancel: false,
success: (res) => {
if (res.confirm) { // 用户点击确定
wx.navigateBack();
}
}
});
});
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
plugin.stopFacelive();
}
})
4、配置TCP合法域名
必须将参数配置时,配置的 ipPortArray 参数中的所有服务器地址,加入到合法的TCP域名,
否则无法访问到服务器。
如配置:
tcp://yfs4.sfzydq.com
tcp://test.sfzydq.com
tcp://yjm2.sfzydq.com
tcp://yfs3.sfzydq.com