微信小程序使用前置攝像頭拍照
本文實例為大家分享了微信小程序使用前置攝像頭拍照的具體代碼,供大家參考,具體內容如下
1、拍照頁面:
<template> <view title="拍照"> <camera v-if="openCamera" device-position="front" frame-size="large" class="zipai" @error="error"> </camera> <cover-image src="/static/image/renzheng_zz.png" class="zhezhao"></cover-image> <cover-view class="wenzi fint34">請將正面人臉放在識別框中,進行拍攝</cover-view> <cover-image class="paizhao" src="/static/image/renzheng_pz.png" @click="takePhoto"></cover-image> </view> </template> <script> export default { data() { return { openCamera:true } }, methods: { takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { let tempFilePath = res.tempImagePath uni.navigateTo({ url:'/pages/renzhengwxtu/renzhengwxtu?src='+tempFilePath }) } }) }, //用戶拒絕授權攝像頭 error(e) { this.openCamera=false wx.showModal({ title: '警告', content: '若不授權使用攝像頭,將無法使用拍照功能!', cancelText: '不授權', cancelColor: '#1ba9ba', confirmText: '授權', confirmColor: '#1ba9ba', success:(res)=> { if (res.confirm) {//允許打開授權頁面 //調起客戶端小程序設置界面,返回用戶設置的操作結果 wx.openSetting({ success:(res)=> { res.authSetting = { "scope.camera": true } this.openCamera=true }, }) } else if (res.cancel) {//拒絕打開授權頁面 wx.navigateBack({delta:1}) } } }) }, } } </script>
2、預覽圖片頁面:
<template> <view title="預覽圖片"> <image mode="widthFix" :src="src" class="renlian"></image> <view class="btns"> <text @click="takePhoto">重拍</text> <text @click="usePhoto">使用照片</text> </view> </view> </template> <script> export default { data() { return { src: '', timeId:null, } }, onLoad(option) { this.src=option.src }, onHide() { clearTimeout(this.timeId); this.timeId=null; }, methods: { takePhoto() { uni.navigateBack({delta: 1}); }, usePhoto() { this.$request.uploadFileMinipro(this.src,this.retoRenzheng); }, retoRenzheng(){ this.timeId=setTimeout(()=>{ var pages = getCurrentPages(); var prevPage = pages[pages.length - 3]; //上一個頁面 prevPage.fromTu= true; uni.navigateBack({delta: 2}); },200); }, } } </script>
3、上傳圖片方法:
// uploadFileMinipro function uploadFileMinipro(tempFilePath,callback){ // 1.2 上傳頭像 let uin =common.getGlobalUserInfo().id; let reurl=common.ip; uni.uploadFile({ url: reurl, filePath: tempFilePath, name: "file", formData:{uin:uin}, success:(res)=>{ console.log("res=",res); // 注意,這里獲得是一個string,需要轉換一下 let resData = JSON.parse(res.data); if (resData.status == 1) {//<=0:人工返回的錯誤信息 setErrorMessage("上傳成功"); if (typeof callback === "function"){ callback();//刷新當前頁面 } } else if (resData.status < 1) { setErrorMessage(resData.msg) } else { setErrorMessage() } }, fail:(res)=>{ console.log("上傳失敗"); }, }); }
為大家推薦現(xiàn)在關注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript簡單拖拽實現(xiàn)代碼(鼠標事件 mousedown mousemove mouseup)
javascript簡單拖拽,簡單拖拽實現(xiàn)2012-05-05JavaScript實現(xiàn)兩個Table固定表頭根據頁面大小自行調整
正如標題所言兩個Table固定表頭,可根據頁面大小自行調整使用JavaScript實現(xiàn),具體的示例如下,感興趣的朋友可以參考下2014-01-01小程序封裝wx.request請求并創(chuàng)建接口管理文件的實現(xiàn)
這篇文章主要介紹了小程序封裝wx.request請求并創(chuàng)建接口管理文件2019-04-04JavaScript使用canvas實現(xiàn)flappy bird全流程詳解
這篇文章主要介紹了JavaScript使用canvas實現(xiàn)flappy bird流程,canvas是HTML5提供的一種新標簽,它可以支持JavaScript在上面繪畫,控制每一個像素,它經常被用來制作小游戲,接下來我將用它來模仿制作一款叫flappy bird的小游戲2023-03-03JS小功能(操作Table--動態(tài)添加刪除表格及數據)實現(xiàn)代碼
這篇文章主要介紹了操作Table--動態(tài)添加刪除表格及數據實現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11