中软高科微信小程序NFC读卡插件,支持多种证卡识读与活体识别
海飞思NFC身份证云解码,由中软高科出品,可以对身份证、外国人永居证、护照、港澳台通行证等证件进行识读,也可以进行活体检测
1、引入插件
在小程序的app.json中,加入插件。
"plugins": {
"readcard-plugin": {
"version": "2.3.2",
"provider": "wxa2583ebacdb87a6a"
}
}
2、基础库
调试基础库 2.18.1及以上
微信版本:
iOS 暂不支持NFC(微信官方未支持),可使用蓝牙外设读卡器
Android 8.0.6及以上版本,同时支持NFC、蓝牙外设读卡器
3、使用插件功能
3.1、读取身份证件信息功能
在需要使用身份证读卡的页面中,使用插件示例。
const plugin = requirePlugin('readcard-plugin');
var StatusCode = null;
var that = this;
var initSuccess = false;
Page({
data: {
idCardInfo: null,
},
onLoad() {
that = this;
StatusCode = plugin.StatusCodeEnum();
console.log("对照状态码:", StatusCode);
// 设置APPID (开始读卡之前,必须设置)
plugin.setAppId("appid联系我司商务获取");
// 设置读卡SDK参数
that.readSetting();
// 初始化读卡
that.initRead();
},
// 设置读卡SDK参数
readSetting(){
// 日志保存目录
// 默认在 Android/data/com.tencent.mm/MicroMsg/wxanewfiles目录下 搜索 zrgk_mini_log
// 不需要日志时,可以不设置
plugin.setShowLog(wx.getFileSystemManager(), wx.env.USER_DATA_PATH);
// 读卡参数设置,根据需要自行修改参数值,参数名称及类型不可修改。
var _Setting = {
decodeImageType: "dn1", // dn0: 无照片 dn1: 平台解码照片
readCardType: 2, // 2:NFC 7:蓝牙外设读卡器 9: NFC读护照/通行证
saveLog: true, // 是否保存日志文件。设置true时,必须调用以上的 plugin.setShowLog(); 设置日志保存的目录
openLocalCache: true, // 是否开启本地缓存
// 解码服务器配置。
// 默认第1个为主服务器,优先使用。
// 其余皆为备用服务器,主服务器异常时自动切换启用
ipPortArray: [{
address: 'yfs4.sfzydq.com',
port: 9999,
canUse: true
}, {
address: 'test.sfzydq.com',
port: 18180,
canUse: true
}]
};
// 将参数设置给插件
plugin.readSetting(_Setting);
},
// 初始化 读卡示例代码
initRead() {
if (initSuccess == true) {
wx.showToast({
title: '已经初始化过了',
icon: 'none'
});
return;
}
// 初始化并开始读卡
plugin.startReadCard(function (code, msg, value, cardType) {
let code_msg = "code:" + code + "n" + "msg:" + msg;
that.setData({
msg: code_msg,
});
switch (code) {
case StatusCode.ININ_ING.code: // 初始化中
wx.showLoading({
title: '初始化中...',
mask: true
});
break;
case StatusCode.ININ_OK.code: // 初始化成功
initSuccess = true;
wx.hideLoading();
wx.showToast({
title: '初始化成功',
icon: 'success'
})
break;
case StatusCode.ININ_FAILE.code: // 初始化失败
initSuccess = false;
wx.hideLoading();
if (msg.indexOf("13000") != -1) {
wx.showModal({
title: '温馨提示',
content: '设备不支持NFC',
complete: (res) => {}
});
} else if (msg.indexOf("13001") != -1) {
wx.showModal({
title: '温馨提示',
content: '系统NFC开关未打开',
complete: (res) => {}
});
} else {
wx.showToast({
title: msg,
icon: 'error'
})
}
break;
case StatusCode.FIND_CARD_START.code: // 开始寻卡
wx.showToast({
title: '开始寻卡',
icon: 'success'
})
break;
case StatusCode.FIND_CARD_SUCCESS.code: // 寻卡成功
allCount += 1;
wx.showToast({
title: '寻卡成功',
icon: 'success'
})
break;
case StatusCode.READCARD_START.code: // 开始解码
wx.showLoading({
title: '请勿移动卡片',
mask: true
})
break;
case StatusCode.READCARD_SUCCESS.code: // 解码成功
wx.hideLoading();
wx.showToast({
title: '解码成功',
icon: 'success'
})
switch (cardType) {
case StatusCode.CARD_IC.code: // IC卡
console.log("IC卡:" , value);
break;
case StatusCode.CARD_LCT_STUDENT.code: // 绿城通学生卡
console.log("绿城通学生卡:" , value);
break;
case StatusCode.CARD_LCT_NORMAL.code: // 绿城通普通卡
console.log("绿城通普通卡:" , value);
break;
case StatusCode.CARD_LCT_OLD.code: // 绿城通老年卡
console.log("绿城通老年卡:" , value);
break;
case StatusCode.CARD_IDCARD.code: // 身份证
// 读取到的身份证信息
// 详细字段说明,详见文档下发的附录
var idCardInfo = JSON.parse(value);
console.log("身份证信息:" , idCardInfo);
// 当设置平台解码身份证照片时,
// idCardInfo.image字段,为base64编码的照片字符串,可直接用于显示
// idCardInfo.type=1080 中国居民身份证
// idCardInfo.type=1081 新版外国永久居住证
// idCardInfo.type=1082 港澳台居住证
// idCardInfo.type=1083 旧版外国永久居住证
break;
case StatusCode.CARD_EPASSPORT.code: // 护照/通行证
// 读取到的护照/通行证信息
// 详细字段说明,详见文档下发的附录
var cardInfo = JSON.parse(value);
console.log("证件信息:" , cardInfo);
// cardInfo.image字段,为base64编码的照片字符串,可直接用于显示
break;
}
break;
case StatusCode.READCARD_FAILE.code: // 解码失败
wx.hideLoading();
wx.showToast({
title: '解码失败' + msg,
icon: 'error'
})
that.readFa();
break;
}
});
},
onUnload() {
console.log("onUnload", "界面销毁,需要停止读卡");
plugin.stopReadCard();
}
})
3.2、活体检测/人脸比对功能
在界面的 **.wxml 添加camera
界面的其余样式,自行定义
在界面 **.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(that.data.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
5、附录
附录
作者声明本文存在利益相关性,请大家尊重作者及分享的内容,友善沟通,理性决策~