您好,登錄后才能下訂單哦!
這篇文章主要介紹“微信小程序怎么調(diào)用攝像頭實現(xiàn)拍照功能”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“微信小程序怎么調(diào)用攝像頭實現(xiàn)拍照功能”文章能幫助大家解決問題。
具體步驟:
1、獲取用戶當前授權(quán)狀態(tài),看是否已經(jīng)授權(quán),如果已經(jīng)授權(quán)直接顯示攝像頭
2、如果用戶還沒有授權(quán),則調(diào)起授權(quán)彈框,用戶允許授權(quán)則顯示攝像頭
3、如果用戶不允許,則提示用戶去設(shè)置頁面打開攝像頭權(quán)限
用戶授權(quán)之后,就可以進行拍攝了,微信的camera組件無法顯示為圓形,我這里是用一張圖片遮蓋了
上代碼:
wxml:
<view class='camera'> <image src="/images/border.png" mode="widthFix"></image> <camera wx:if="{{isAuth}}" device-position="back" flash="off" binderror="error"></camera> </view> <button class="takePhoto" type="primary" bindtap="takePhoto">拍照</button>
wxss:
.camera { width: 430rpx; height: 430rpx; border-radius: 50%; margin: 20px auto 0; position: relative; } .camera image { position: absolute; width: 100%; height: 100%; z-index: 10; } .camera camera { width: 428rpx; height: 428rpx; } button.takePhoto:not([size='mini']) { position: fixed; bottom: 0; left: 0; width: 100vw; height: 90rpx; border-radius: 0; }
js:
Page({ data: { isAuth: false, src: '' }, onLoad() { const _this = this wx.getSetting({ success: res => { if (res.authSetting['scope.camera']) { // 用戶已經(jīng)授權(quán) _this.setData({ isAuth: true }) } else { // 用戶還沒有授權(quán),向用戶發(fā)起授權(quán)請求 wx.authorize({ scope: 'scope.camera', success() { // 用戶同意授權(quán) _this.setData({ isAuth: true }) }, fail() { // 用戶不同意授權(quán) _this.openSetting().then(res => { _this.setData({ isAuth: true }) }) } }) } }, fail: res => { console.log('獲取用戶授權(quán)信息失敗') } }) }, // 打開授權(quán)設(shè)置界面 openSetting() { const _this = this let promise = new Promise((resolve, reject) => { wx.showModal({ title: '授權(quán)', content: '請先授權(quán)獲取攝像頭權(quán)限', success(res) { if (res.confirm) { wx.openSetting({ success(res) { if (res.authSetting['scope.camera']) { // 用戶打開了授權(quán)開關(guān) resolve(true) } else { // 用戶沒有打開授權(quán)開關(guān), 繼續(xù)打開設(shè)置頁面 _this.openSetting().then(res => { resolve(true) }) } }, fail(res) { console.log(res) } }) } else if (res.cancel) { _this.openSetting().then(res => { resolve(true) }) } } }) }) return promise; }, takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) wx.previewImage({ current: res.tempImagePath, // 當前顯示圖片的http鏈接 urls: [res.tempImagePath] // 需要預覽的圖片http鏈接列表 }) } }) } })
關(guān)于“微信小程序怎么調(diào)用攝像頭實現(xiàn)拍照功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。