這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)調(diào)用相機(jī)自動(dòng)拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了微信小程序定時(shí)拍照的具體代碼,供大家參考,具體內(nèi)容如下
在某些進(jìn)行簽到的場(chǎng)景,為了防止用戶選擇相冊(cè)的照片或者不實(shí)時(shí)拍照,設(shè)置相機(jī)倒計(jì)時(shí)自動(dòng)拍照。
一、首先是視圖層index.wxml,視圖層主要負(fù)責(zé)顯示組件和圖片。
<!--index.wxml--> <view class="userinfo-login"> <view class="page-body"> <view class="page-body-wrapper"> <view wx:if="{{src}}"></view> <!-- 如果存在已經(jīng)拍好的照片就不再顯示調(diào)用攝像頭的組件--> <view wx:else> <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 200px;"></camera> <!-- 調(diào)用攝像頭的組件--> </view> <image wx:if="{{src}}" mode="widthFix" src="{{src}}"></image> <!-- 顯示拍好的照片--> </view> </view> </view>
登錄后復(fù)制
二、邏輯層index.js,調(diào)用倒計(jì)時(shí)函數(shù)并且調(diào)用攝像頭拍照并保存圖片。
//index.js const app = getApp() Page({ data: { userInfo: {}, counting: false//倒計(jì)時(shí) }, onLoad: function () { this.daojishi();//一進(jìn)來(lái)就拍照倒計(jì)時(shí) this.ctx = wx.createCameraContext()//創(chuàng)建攝像頭對(duì)象 }, //倒計(jì)時(shí) daojishi: function () { var that = this; if (!that.data.counting) { //開(kāi)始倒計(jì)時(shí)5秒 countDown(that, 5); } } }) //倒計(jì)時(shí)函數(shù) 在page外 function countDown(that, count) { if (count == 0) { //等于0時(shí)拍照 that.ctx.takePhoto({ quality: 'high', success: (res) => { that.setData({ src: res.tempImagePath }) wx.showToast({ title: '拍照完成', }) } }) that.setData({ counting: false }) return; } wx.showLoading({//加載時(shí)顯示倒計(jì)時(shí) title: '拍照倒計(jì)時(shí)'+count+'秒', }) setTimeout(function () { wx.hideLoading() }, 1000) that.setData({ counting: true, }) setTimeout(function () { count--; countDown(that, count); }, 1000); }
登錄后復(fù)制
主要實(shí)現(xiàn)就是這樣。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)!
相關(guān)推薦:
以上就是微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)調(diào)用相機(jī)自動(dòng)拍照功能的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注有卡有網(wǎng)。