国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

微信小程序實現點擊拍照長按錄像功能的實現代碼

2020-03-21 15:51:16
字體:
來源:轉載
供稿:網友

代碼里面注釋寫的都很詳細,直接上代碼。官方的組件屬性中有觸摸開始和觸摸結束屬性。本功能依靠這些屬性實現。

.wxml代碼:

<!-- 相機 pages/camera/camera.wxml--><!-- 相機 --><camera wx:if="{{!videoSrc}}" device-position="back" flash="off" binderror="error" style="width: {{cameraWidth}}px; height: {{cameraHeight}}px;"> <!-- 拍完顯示照片 --> <cover-image wx:if="{{image1Src}}" src='{{image1Src}}'></cover-image> <cover-view>  <!-- 拍照按鈕 -->  <button id='btn-photo-video' bindtouchstart="handleTouchStart" bindtouchend="handleTouchEnd" bindlongpress="handleLongPress" bindtap="handleClick">   點擊/長按</button> </cover-view></camera><video wx:if="{{videoSrc}}" src="{{videoSrc}}" controls></video>

.wxss代碼:

/* pages/camera/camera.wxss */cover-image,video { margin-top:100%; position: absolute; width: 200rpx; height: 200rpx;}#btn-photo-video{ /* position: absolute; */ margin-top:100%; width: 242rpx; left: 2%;}

.js代碼:

// pages/camera/camera.jsPage({ /**  * 頁面的初始數據  */ data: {  cameraHeight: '',  cameraWidth: '',  image1Src: '',  videoSrc: '',  num: 0, }, /**  * 生命周期函數--監聽頁面加載  */ onLoad: function(options) {  //調用設置相機大小的方法  this.setCameraSize();  this.ctx = wx.createCameraContext();  console.log(this.lijiajun) }, /**  * 生命周期函數--監聽頁面初次渲染完成  */ onReady: function() { }, /**  * 生命周期函數--監聽頁面顯示  */ onShow: function() { }, /**  * 生命周期函數--監聽頁面隱藏  */ onHide: function() { }, /**  * 生命周期函數--監聽頁面卸載  */ onUnload: function() { }, /**  * 頁面相關事件處理函數--監聽用戶下拉動作  */ onPullDownRefresh: function() { }, /**  * 頁面上拉觸底事件的處理函數  */ onReachBottom: function() { }, /**  * 用戶點擊右上角分享  */ onShareAppMessage: function() { }, /**  * 獲取系統信息 設置相機的大小適應屏幕  */ setCameraSize() {  //獲取設備信息  const res = wx.getSystemInfoSync();  //獲取屏幕的可使用寬高,設置給相機  this.setData({   cameraHeight: res.windowHeight,   cameraWidth: res.windowWidth  })  console.log(res) }, /**  *拍照的方法   */ takePhoto() {  this.ctx.takePhoto({   quality: 'high',   success: (res) => {    this.setData({     image1Src: res.tempImagePath    })   },   fail() {    //拍照失敗    console.log("拍照失敗");   }  }) }, /**  * 開始錄像的方法  */ startShootVideo() {  console.log("========= 調用開始錄像 ===========")  this.ctx.startRecord({   success: (res) => {    wx.showLoading({     title: '正在錄像',    })   },   fail() {    console.log("========= 調用開始錄像失敗 ===========")   }  }) }, /**  * 結束錄像  */ stopShootVideo() {  console.log("========= 調用結束錄像 ===========")  this.ctx.stopRecord({   success: (res) => {    wx.hideLoading();    this.setData({     videoSrc: res.tempVideoPath,    })   },   fail() {    wx.hideLoading();    console.log("========= 調用結束錄像失敗 ===========")   }  }) }, //touch start 手指觸摸開始 handleTouchStart:  function(e) {    this.startTime = e.timeStamp;    console.log(" startTime = " + e.timeStamp);   console.log(" 手指觸摸開始 " , e);   console.log(" this " , this);  }, //touch end 手指觸摸結束 handleTouchEnd:  function(e) {    this.endTime = e.timeStamp;    console.log(" endTime = " + e.timeStamp);   console.log(" 手指觸摸結束 ", e);  //判斷是點擊還是長按 點擊不做任何事件,長按 觸發結束錄像  if (this.endTime - this.startTime > 350) {   //長按操作 調用結束錄像方法   this.stopShootVideo();  } }, /**  * 點擊按鈕 - 拍照  */ handleClick:  function(e) {    console.log("endTime - startTime = " + (this.endTime - this.startTime));      if (this.endTime - this.startTime < 350) {     console.log("點擊");   //調用拍照方法   this.takePhoto();    } }, /**  * 長按按鈕 - 錄像  */ handleLongPress:  function(e) {  console.log("endTime - startTime = " + (this.endTime - this.startTime));  console.log("長按");  // 長按方法觸發,調用開始錄像方法  this.startShootVideo(); },})

總結

以上所述是小編給大家介紹的微信小程序實現點擊拍照長按錄像功能的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巍山| 葵青区| 柳林县| 姜堰市| 衡水市| 武穴市| 花垣县| 南溪县| 怀化市| 高要市| 平山县| 通河县| 政和县| 孝昌县| 瑞安市| 山阴县| 客服| 安仁县| 普兰县| 马关县| 长兴县| 观塘区| 山西省| 伊川县| 错那县| 乌审旗| 高唐县| 防城港市| 阳城县| 泰来县| 乐东| 舟曲县| 白河县| 盐津县| 长寿区| 珲春市| 台江县| 盖州市| 彭泽县| 淮北市| 都江堰市|