會(huì)創(chuàng)建小程序項(xiàng)目和小程序頁面
知道小程序頁面的組成部分及各自的作用
會(huì)用 button,image,view 等小程序組件
會(huì)使用 wx.request() 發(fā)起網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求
箭頭函數(shù)
let,const 等
<camera style="height: {{wh}}px; width: 100%;" flash="off"></camera>
在 data 中定義 wh
data: { // 窗口可用的高度 wh: 0}
動(dòng)態(tài)獲取頁面可用高度
/*** 生命周期函數(shù)--監(jiān)聽頁面加載*/onLoad: function(options) { const sysInfo = wx.getSystemInfoSync() this.setData({ wh: sysInfo.windowHeight })}
在 app.json 的 window 節(jié)點(diǎn)中,新增如下配置:
{ "pages": [ "pages/home/home" ], "window": { // ... 省略其他配置 "navigationStyle": "custom" }, "sitemapLocation": "sitemap.json"}
<camera style="height: {{wh}}px; width: 100%;" flash="off"> <cover-view class='btn-box'> <!-- 切換攝像頭 --> <cover-image src='/images/icon/reverse.png'></cover-image> <!-- 拍照 --> <cover-image src='/images/icon/camera.png'></cover-image> <!-- 從相冊(cè)選取照片 --> <cover-image src='/images/icon/album.png'></cover-image> </cover-view></camera>
.btn-box { display: flex; justify-content: space-around; position: absolute; bottom: 50px; width: 100%;}.btn-box cover-image { width: 50px; height: 50px; opacity: 0.7;}
data: { // 攝像頭的朝向 front back position: 'front'}
<!-- 切換攝像頭 --><cover-image src='/images/icon/reverse.png' bindtap='reverseCamera'></cover-image>
// 點(diǎn)擊按鈕,切換攝像頭reverseCamera() { const newPosition = this.data.position === 'front' ? 'back' : 'front' this.setData({ position: newPosition })}
device-position
<camera style="height: {{wh}}px; width: 100%;" flash="off" device-position='{{position}}'></camera>
data: { // 照片的路徑 src: ''}
<!-- 拍照 --><cover-image src='/images/icon/camera.png' bindtap='takePhoto'></cover-image>
// 拍照takePhoto() { // 創(chuàng)建相機(jī)的實(shí)例對(duì)象 const ctx = wx.createCameraContext() // ctx.takePhoto 實(shí)現(xiàn)拍照 ctx.takePhoto({ quality: 'high', success: (res) => { // console.log(res.tempImagePath) this.setData({ src: res.tempImagePath, isShowPic: true }, () => { this.getFaceInfo() }) }, fail: () => { console.log('拍照失敗!') this.setData({ src: '' }) } })}
<!-- 從相冊(cè)選取照片 --><cover-image src='/images/icon/album.png' bindtap='choosePhoto'></cover-image>
// 從相冊(cè)選取照片 choosePhoto() { wx.chooseImage({ count: 1, sizeType: ['original'], sourceType: ['album'], success: (res) => { // console.log(res) if (res.tempFilePaths.length > 0) { this.setData({ src: res.tempFilePaths[0], isShowPic: true }, () => { this.getFaceInfo() }) } }, fail: () => { console.log('選擇照片失敗!') this.setData({ src: '' }) } }) }
<view wx:else> <image src='{{src}}' style='width: 100%; height: {{wh}}px; display: block;' mode='aspectFill'></image></view>
<button type='warn' class='reChoose' bindtap='reChoose'>重選照片</button>
// 重新選擇照片 reChoose() { this.setData({ isShowPic: false, src: '' }) }
http://ai.baidu.com/
API Key
和 Secret Key
// this.globalData.access_token = 'aaa' wx.request({ method: 'POST', url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=自己的ID&client_secret=自己的KEY', success: (res) => { this.globalData.access_token = res.data.access_token }, fail: () => { wx.showToast({ title: '鑒權(quán)失敗!', }) } })
const fileManager = wx.getFileSystemManager()const fileStr = fileManager.readFileSync(this.data.src, 'base64')
wx.request({ method: 'POST', url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' token, header: { 'Content-Type': 'application/json' }, data: { image_type: 'BASE64', image: fileStr, // 年齡,顏值分?jǐn)?shù),表情,性別,是否戴眼鏡,情緒 face_field: 'age,beauty,expression,gender,glasses,emotion' }, success: (res) => { console.log(res) if (res.data.result.face_num <= 0) { return wx.showToast({ title: '未檢測(cè)到人臉!', }) } this.setData({ faceInfo: res.data.result.face_list[0], isShowBox: true }) }, fail: () => { wx.showToast({ title: '顏值檢測(cè)失敗!', }) }, complete: () => { wx.hideLoading() } })
定義映射關(guān)系:
data: { // 映射關(guān)系 map: { gender: { male: '男', female: '女' }, expression: { none: '不笑', smile: '微笑', laugh: '大笑' }, glasses: { none: '無眼鏡',common: '普通眼鏡',sun: '墨鏡' }, emotion: { angry: '憤怒', disgust: '厭惡', fear: '恐懼', happy: '高興', sad: '傷心', surprise: '驚訝', neutral: '無情緒' } }}
修改UI結(jié)構(gòu):
<view class='faceinfo_box' wx:if="{{isShowBox}}"> <view class='face_row'> <text>年齡:{{faceInfo.age}}歲</text> <text>性別:{{map.gender[faceInfo.gender.type]}}</text> </view> <view class='face_row'> <text>顏值:{{faceInfo.beauty}}分</text> <text>表情:{{map.expression[faceInfo.expression.type]}}</text> </view> <view class='face_row'> <text>眼鏡:{{map.glasses[faceInfo.glasses.type]}}</text> <text>情緒:{{map.emotion[faceInfo.emotion.type]}}</text> </view></view>
聯(lián)系客服