207 lines
4.6 KiB
JavaScript
207 lines
4.6 KiB
JavaScript
// pages/home/camera/index.js
|
||
import {
|
||
paddlejs
|
||
} from '@paddlejs/paddlejs-core';
|
||
import {
|
||
Paddlejs
|
||
} from '../../../services/_utils/ocr';
|
||
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
isAuth: false,
|
||
src: ''
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad(options) {
|
||
const _this = this
|
||
wx.getSetting({
|
||
success: res => {
|
||
if (res.authSetting['scope.camera']) {
|
||
// 用户已经授权
|
||
_this.setData({
|
||
isAuth: true
|
||
})
|
||
} else {
|
||
// 用户还没有授权,向用户发起授权请求
|
||
wx.authorize({
|
||
scope: 'scope.camera',
|
||
success() { // 用户同意授权
|
||
_this.setData({
|
||
isAuth: true
|
||
})
|
||
},
|
||
fail() { // 用户不同意授权
|
||
_this.openSetting().then(res => {
|
||
_this.setData({
|
||
isAuth: true
|
||
})
|
||
})
|
||
}
|
||
})
|
||
}
|
||
},
|
||
fail: res => {
|
||
console.log('获取用户授权信息失败')
|
||
}
|
||
})
|
||
},
|
||
|
||
openSetting() {
|
||
const _this = this
|
||
let promise = new Promise((resolve, reject) => {
|
||
wx.showModal({
|
||
title: '授权',
|
||
content: '请先授权获取摄像头权限',
|
||
success(res) {
|
||
if (res.confirm) {
|
||
wx.openSetting({
|
||
success(res) {
|
||
if (res.authSetting['scope.camera']) { // 用户打开了授权开关
|
||
resolve(true)
|
||
} else { // 用户没有打开授权开关, 继续打开设置页面
|
||
_this.openSetting().then(res => {
|
||
resolve(true)
|
||
})
|
||
}
|
||
},
|
||
fail(res) {
|
||
console.log(res)
|
||
}
|
||
})
|
||
} else if (res.cancel) {
|
||
_this.openSetting().then(res => {
|
||
resolve(true)
|
||
})
|
||
}
|
||
}
|
||
})
|
||
})
|
||
return promise;
|
||
},
|
||
|
||
takePhoto() {
|
||
console.log("开始识别图片");
|
||
const ctx = wx.createCameraContext()
|
||
ctx.takePhoto({
|
||
quality: 'high',
|
||
success: (res) => {
|
||
console.log(res);
|
||
this.setData({
|
||
src: res.tempImagePath
|
||
})
|
||
// Paddlejs.predict(res.tempImagePath).then(res => {
|
||
// console.log(res);
|
||
// })
|
||
var pic0 = new Image();
|
||
pic0.src = res.tempImagePath;
|
||
const this_ = this
|
||
pic0.onload = () => {
|
||
var img0 = tf.browser.fromPixels(pic0);
|
||
this_.predict(img0);
|
||
}
|
||
|
||
// 获取到图片的像素信息
|
||
// wx.getImageInfo({
|
||
// src: res.tempImagePath,
|
||
// success: (imgInfo) => {
|
||
// const {
|
||
// width,
|
||
// height,
|
||
// path
|
||
// } = imgInfo;
|
||
|
||
// const canvasId = 'myCanvas';
|
||
// const me = this;
|
||
// // 获取页面中的canvas上下文,tips:canvas设置的宽高要大于选择的图片宽高,canvas位置可以绝对定位到视口不可以见
|
||
// let ctx = canvas.getContext(canvasId);
|
||
// ctx.drawImage(path, 0, 0, width, height);
|
||
// ctx.draw(false, () => {
|
||
// // API 1.9.0 获取图像数据
|
||
// wx.canvasGetImageData({
|
||
// canvasId: canvasId,
|
||
// x: 0,
|
||
// y: 0,
|
||
// width: width,
|
||
// height: height,
|
||
// success(res) {
|
||
// console.log(res);
|
||
// me.predict({
|
||
// data: res.data,
|
||
// width: width,
|
||
// height: height
|
||
// });
|
||
// }
|
||
// });
|
||
// });
|
||
// }
|
||
// });
|
||
}
|
||
})
|
||
},
|
||
|
||
predict(imgObj) {
|
||
// 4. 在线预测计算
|
||
const me = this;
|
||
Paddlejs.predict(imgObj, function (data) {
|
||
// 5. 对预测结果进行后处理
|
||
const maxItem = pdjs.utils.getMaxItem(data);
|
||
console.log(maxItem);
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom() {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage() {
|
||
|
||
}
|
||
}) |