微信小程序調(diào)用攝像頭隱藏式拍照功能
微信小程序最近非?;馃幔【幾罱隽艘粋€(gè)新項(xiàng)目,使用小程序開(kāi)發(fā)考試系統(tǒng),在使用App參加考試的時(shí)候調(diào)用攝像頭抓拍用戶是否作弊,在開(kāi)發(fā)過(guò)程中遇到點(diǎn)問(wèn)題,下面小編把問(wèn)題描述和解決方法分享給大家,具體內(nèi)容如下:
問(wèn)題
今天小編遇到了這么個(gè)問(wèn)題,就是在用戶使用App參加考試的時(shí)候調(diào)用攝像頭抓拍用戶是否作弊,其實(shí)這也沒(méi)什么,關(guān)鍵在于不能打擾用戶考試,不能被用戶發(fā)現(xiàn)什么時(shí)候抓拍的,也不能給用戶查看圖片,只有考完是后才能查看。這系統(tǒng)相當(dāng)于考駕照時(shí)的上機(jī)答題部分。開(kāi)始經(jīng)理的要求是調(diào)用小程序外部的手機(jī)拍攝功能,這要可把我嚇的夠嗆。
解決方法
遇到這種問(wèn)題肯定要先找下官網(wǎng)的幫助文檔,于是找到了調(diào)用攝像頭的這么一個(gè)模塊
相機(jī)組件控制 (wx.createCameraContext)
說(shuō)明:
創(chuàng)建并返回 camera 上下文 cameraContext 對(duì)象,cameraContext 與頁(yè)面的 camera 組件綁定,一個(gè)頁(yè)面只能有一個(gè)camera,通過(guò)它可以操作對(duì)應(yīng)的 <camera/> 組件。 在自定義組件下,第一個(gè)參數(shù)傳入組件實(shí)例this,以操作組件內(nèi) <camera/> 組件
cameraContext 對(duì)象的方法列表:
takePhoto | OBJECT | 拍照,可指定質(zhì)量,成功則返回圖片 |
startRecord | OBJECT | 開(kāi)始錄像 |
stopRecord | OBJECT | 結(jié)束錄像,成功則返回封面與視頻 |
takePhoto 的 OBJECT 參數(shù)列表:
quality | String | 否 | 成像質(zhì)量,值為high, normal, low,默認(rèn)normal |
success | Function | 否 | 接口調(diào)用成功的回調(diào)函數(shù) ,res = { tempImagePath } |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |
這不是有個(gè)拍照功能的模塊嗎,于是去找了一下官網(wǎng)所給的代碼研究了一下:
官網(wǎng)代碼:
wxml代碼:
<view class="page-body"> <view class="page-body-wrapper"> <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> <view class="btn-area"> <button type="primary" bindtap="takePhoto">拍照</button> </view> <view class="btn-area"> <button type="primary" bindtap="startRecord">開(kāi)始錄像</button> </view> <view class="btn-area"> <button type="primary" bindtap="stopRecord">結(jié)束錄像</button> </view> <view class="preview-tips">預(yù)覽</view> <image wx:if="{{src}}" mode="widthFix" src="{{src}}"></image> <video wx:if="{{videoSrc}}" class="video" src="{{videoSrc}}"></video> </view> </view>
js代碼:
Page({ onLoad() { this.ctx = wx.createCameraContext() }, takePhoto() { this.ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) } }) }, startRecord() { this.ctx.startRecord({ success: (res) => { console.log('startRecord') } }) }, stopRecord() { this.ctx.stopRecord({ success: (res) => { this.setData({ src: res.tempThumbPath, videoSrc: res.tempVideoPath }) } }) }, error(e) { console.log(e.detail) } })
wxcss代碼:
.preview-tips { margin: 20rpx 0; } .video { margin: 50px auto; width: 100%; height: 300px; }
這代碼修改還是可以的只是不是我想要的那種,我要的是隱藏沒(méi)有攝像頭的,和自動(dòng)抓拍功能的,
官網(wǎng)代碼分析:
<camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
這是攝像頭模塊,這模塊只要不是隱藏,寬高不為0,都可以正常使用拍照功能
this.ctx = wx.createCameraContext()
這是獲取攝像頭圖像功能
takePhoto() { this.ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) } }) },
這是拍照功能,并沒(méi)有和攝像頭一起使用,只要?jiǎng)?chuàng)建了攝像頭并不隱藏的情況下可以單獨(dú)調(diào)用
就這幾行代碼就差不多可以實(shí)現(xiàn)我想要的功能了,拍照功能單獨(dú)用定時(shí)器來(lái)調(diào)用,只差一個(gè)隱藏?cái)z像頭的功能了,在官網(wǎng)上找找。有沒(méi)有新發(fā)現(xiàn)......
在組件里發(fā)現(xiàn)有這么一個(gè)模塊叫
cover-view
描述:覆蓋在原生組件之上的文本視圖,可覆蓋的原生組件包括map、video、canvas、camera、live-player、live-pusher,只支持嵌套cover-view、cover-image,可在cover-view中使用button。
這模塊的意思是說(shuō)cover-view 可以覆蓋掉camera這控件
camera這模塊也是這么個(gè)奇葩的存在,你用z-index層次不管多大都無(wú)法覆蓋掉,這里就要用到cover-view組件了
一開(kāi)始我也不知道怎么用,于是上網(wǎng)查了查,博友們都是這么說(shuō)的 cover-view 嵌套到 camera 里面去就行了,
也就是這么個(gè)意思
<camera> <cover-view></cover-view> </camera>
這樣效果是可以但太占空間了 于是我就把<camera>寬高設(shè)成了
width: 10rpx;
height: 14rpx;
只要不隱藏不為0都是可以的拍照的清晰度沒(méi)有變化只有寬高比例有變化
同樣<cover-view>也要把攝像機(jī)鋪滿,背景色調(diào)為周圍一樣的顏色這就相當(dāng)于隱藏?cái)z像頭功能了,再加上定時(shí)器抓拍就完成了這項(xiàng)功能。
完成代碼:
wxml代碼:
<view class="page-body"> <view class="page-body-wrapper"> <camera device-position="front" flash="off" binderror="error" class="camera" bindstop='bindstop' binderror='binderror'> <cover-view class='border_writh'></cover-view> </camera> <view class="btn-area"> <button type="primary" bindtap="stoptime">停止</button> </view> <view class="preview-tips">預(yù)覽</view> <image wx:if="{{src}}" mode="widthFix" src="{{src}}"></image> </view> </view>
wxss代碼:
.preview-tips { margin: 20rpx 0; } .video { margin: 50px auto; width: 100%; height: 300rpx; } .border_writh{ width: 30rpx; height: 30rpx; background-color:#1aad19; } .camera{ position: absolute; top: 5rpx; left: 5rpx; width: 10rpx; height: 14rpx; }
js代碼:
var time = null; Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { }, onLoad() { }, //定時(shí)器拍照 setTime: function() { let that = this let ctx = wx.createCameraContext() time = setInterval(function() { if (Math.round(Math.random()) == 1) { console.log('拍照') //拍照 ctx.takePhoto({ quality: 'high', success: (res) => { console.log(res.tempImagePath) that.setData({ src: res.tempImagePath }) that.localhostimgesupdata(res.tempImagePath) } }) } }, 1000 * 10) //循環(huán)間隔 單位ms }, //圖片上傳 localhostimgesupdata: function(imgPath) { console.log('圖片上傳') wx.uploadFile({ url: '', /圖片上傳服務(wù)器真實(shí)的接口地址 filePath: imgPath, name: 'imgFile', success: function(res) { wx.showToast({ title: '圖片上傳成功', icon: 'success', duration: 2000 }) } }) }, stoptime: function() { console.log('定時(shí)停止') clearInterval(time) }, bindstop: function() { console.log('非正常停止') }, binderror: function() { console.log('用戶拒絕授權(quán)') }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示 */ onShow: function() { console.log('顯示') //定時(shí)器 this.setTime(); }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏 */ onHide: function() { console.log('隱藏') clearInterval(time) }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載 */ onUnload: function() { console.log('卸載') clearInterval(time) }, })
總結(jié)
以上所述是小編給大家介紹的微信小程序調(diào)用攝像頭隱藏式拍照功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 微信小程序 wx.request(接口調(diào)用方式)詳解及實(shí)例
- 微信小程序頁(yè)面調(diào)用自定義組件內(nèi)的事件詳解
- 微信小程序如何調(diào)用json數(shù)據(jù)接口并解析
- 微信小程序調(diào)用攝像頭實(shí)現(xiàn)拍照功能
- 微信小程序調(diào)用微信登陸獲取openid及java做為服務(wù)端示例
- 微信小程序調(diào)用支付接口的完整流程記錄
- 使用微信小程序API,調(diào)用微信的各種內(nèi)置能力。
- 微信小程序頁(yè)面與組件之間信息傳遞與函數(shù)調(diào)用
- 微信小程序開(kāi)發(fā)打開(kāi)另一個(gè)小程序的實(shí)現(xiàn)方法
- 微信外喚起微信小程序的方法詳解
相關(guān)文章
JS實(shí)現(xiàn)可縮放、拖動(dòng)、關(guān)閉和最小化的浮動(dòng)窗口完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)可縮放、拖動(dòng)、關(guān)閉和最小化的浮動(dòng)窗口的方法,實(shí)例分析了javascript操作窗口層的技巧,需要的朋友可以參考下2015-03-03JS實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)轉(zhuǎn)盤(pán)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)單的抽獎(jiǎng)轉(zhuǎn)盤(pán)效果,涉及javascript數(shù)值計(jì)算與頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02js get和post請(qǐng)求實(shí)現(xiàn)代碼解析
這篇文章主要介紹了js get和post實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02javascript中的注釋使用與注意事項(xiàng)小結(jié)
在javascript中有兩種注釋方式,單行注釋與多行注釋。2011-09-09JS實(shí)現(xiàn)帶動(dòng)畫(huà)的回到頂部效果
這篇文章主要為大家詳細(xì) 介紹了JS實(shí)現(xiàn)帶動(dòng)畫(huà)的回到頂部效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12