无快门声的实现方式:
// 识别相关
createListener(){
let num = 0,self = this;
const context = wx.createCameraContext();
listener = context.onCameraFrame(async function (res) {
num++;
if (num === 50) {
num = 0;
self.data2Base64(res)
}
});
listener.start();
},
data2Base64(frame){
var self = this;
const query = wx.createSelectorQuery();
query.select('#helperCanvas')
.fields({node: true, size: true})
.exec((res) => {
const {width,height} = frame;
const data = new Uint8Array(frame.data);
const node = res[0].node
const ctx = node.getContext('2d');
const imgData = node.createImageData(data,width,height);
node.width = imgData.width;
node.height = imgData.height;
ctx.putImageData(imgData,0,0);
wx.canvasToTempFilePath({
canvas: node,
success(res){
self.upload(res.tempFilePath)// 上传图片到识别服务器
}
})
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
canNextRec = true;
if (listener) {
listener.start();
} else {
this.createListener();
}
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
listener && listener.stop();
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
listener && listener.stop();
},
有快门声的实现
timerReco(){
cameraContext = wx.createCameraContext();
timer = setInterval(() => {
this.takePhoto();
},1000);
},
takePhoto() {
if (!canNextRec) return;
canNextRec = false;
var self = this;
cameraContext.takePhoto({
success: (imageInfo) => {
self.upload(imageInfo.tempImagePath);
}
})
},
upload: function (picpath) {
var self = this;
wx.uploadFile({
url: app.globalData.urlHeader + api.identify,
filePath: picpath,
name: 'file',
header: {
'content-type': 'multipart/form-data',
'XSESSIONID': app.globalData.token
},
formData: {
"x": 0,
"y": 0,
"kind": '2'
},
success: function (res) {
if (res.data) {
var info = JSON.parse(res.data);
console.log('识别结果',info);
canNextRec = true;
}
},
fail: (res) => {
canNextRec = true;
wx.hideLoading();
},
complete: function (res) {
}
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.timerReco();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
clearInterval(timer);
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
clearInterval(timer);
},