const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
devicePosition: 'back',
showPhoto: false,
imgUrl: '',
faceInfo: null,
map: {
gender: {
male: '男性',
female: '女性'
},
expression: {
none: '不笑',
smile: '微笑',
laugh: '大笑'
},
glasses: {
none:'无眼镜',
common: '普通眼镜',
sun: '墨镜'
},
emotion: {
angry:'愤怒',
disgust: '厌恶',
fear: '恐惧',
happy: '高兴',
sad: '伤心',
surprise: '惊讶',
neutral: '无表情',
pouty: '撅嘴',
grimace: '鬼脸'
}
}
},
getFaceInfo () {
wx.showLoading({
title: '人脸检测中...',
mask: true
})
const token = app.globalData.token
if (!token) {
return wx.showToast({
title: '获取人脸信息失败',
icon: 'error'
})
}
// 获取图片的base65
const fileManager = wx.getFileSystemManager()
const base64Img = fileManager.readFileSync(this.data.imgUrl, 'base64')
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + token,
method: 'POST',
data: {
image: base64Img,
image_type: 'BASE64',
face_field: 'age,beauty,expression,gender,glasses,emotion,face_type,mask',
face_type: 'LIVE'
},
success: res => {
console.log(res.data);
if (!res.data.result) {
wx.showToast({
title: '检测人脸失败',
})
this.setData({
showPhoto: false,
imgUrl: ''
})
return
}
this.setData({
faceInfo: res.data.result.face_list[0]
})
wx.hideLoading()
}
})
},
rechoose() {
this.setData({
showPhoto: !this.data.showPhoto,
imgUrl: '',
faceInfo: null
})
},
handleReverse() {
this.setData({
devicePosition: this.data.devicePosition === 'back' ? 'front' : 'back'
})
},
handleTakePhoto() {
const context = wx.createCameraContext()
context.takePhoto({
quality: 'normal',
success: res => {
this.setData({
showPhoto: true,
imgUrl: res.tempImagePath
}, this.getFaceInfo)
},
fail: err => {
wx.showToast({
title: err.errMsg,
icon: 'error'
})
this.setData({
showPhoto: false,
imgUrl: '',
faceInfo: null
})
}
})
},
handleAlbum() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album'],
success: res => {
this.setData({
showPhoto: true,
imgUrl: res.tempFiles[0].tempFilePath
}, this.getFaceInfo)
},
fail: err => {
console.log(err.errMsg);
if (err.errMsg === 'chooseMedia:fail cancel') return
wx.showToast({
title: err.errMsg,
icon: 'error'
})
this.setData({
showPhoto: false,
imgUrl: '',
faceInfo: null
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})