微信小程序使用前置攝像頭拍照
本文實(shí)例為大家分享了微信小程序使用前置攝像頭拍照的具體代碼,供大家參考,具體內(nèi)容如下

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">請(qǐng)將正面人臉放在識(shí)別框中,進(jìn)行拍攝</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
})
}
})
},
//用戶拒絕授權(quán)攝像頭
error(e) {
this.openCamera=false
wx.showModal({
title: '警告',
content: '若不授權(quán)使用攝像頭,將無法使用拍照功能!',
cancelText: '不授權(quán)',
cancelColor: '#1ba9ba',
confirmText: '授權(quán)',
confirmColor: '#1ba9ba',
success:(res)=> {
if (res.confirm) {//允許打開授權(quán)頁面
//調(diào)起客戶端小程序設(shè)置界面,返回用戶設(shè)置的操作結(jié)果
wx.openSetting({
success:(res)=> {
res.authSetting = {
"scope.camera": true
}
this.openCamera=true
},
})
} else if (res.cancel) {//拒絕打開授權(quán)頁面
wx.navigateBack({delta:1})
}
}
})
},
}
}
</script>
2、預(yù)覽圖片頁面:
<template>
<view title="預(yù)覽圖片">
<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]; //上一個(gè)頁面
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);
// 注意,這里獲得是一個(gè)string,需要轉(zhuǎn)換一下
let resData = JSON.parse(res.data);
if (resData.status == 1) {//<=0:人工返回的錯(cuò)誤信息
setErrorMessage("上傳成功");
if (typeof callback === "function"){
callback();//刷新當(dāng)前頁面
}
} else if (resData.status < 1) {
setErrorMessage(resData.msg)
} else {
setErrorMessage()
}
},
fail:(res)=>{
console.log("上傳失敗");
},
});
}
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript對(duì)IE操作的經(jīng)典代碼(推薦)
本篇文章主要是對(duì)JavaScript對(duì)IE操作的經(jīng)典代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03
javascript簡(jiǎn)單拖拽實(shí)現(xiàn)代碼(鼠標(biāo)事件 mousedown mousemove mouseup)
javascript簡(jiǎn)單拖拽,簡(jiǎn)單拖拽實(shí)現(xiàn)2012-05-05
火狐下input焦點(diǎn)無法重復(fù)獲取問題的解決方法
input輸入框顯示的時(shí)候,需要自動(dòng)獲取焦點(diǎn),用focus可以輕松搞定,但在火狐下input無法獲取焦點(diǎn),下面與大家分享下不錯(cuò)的解決方法2014-06-06
JavaScript實(shí)現(xiàn)兩個(gè)Table固定表頭根據(jù)頁面大小自行調(diào)整
正如標(biāo)題所言兩個(gè)Table固定表頭,可根據(jù)頁面大小自行調(diào)整使用JavaScript實(shí)現(xiàn),具體的示例如下,感興趣的朋友可以參考下2014-01-01
JS?new操作原理及手寫函數(shù)模擬實(shí)現(xiàn)示例
這篇文章主要為大家介紹了JS?new操作原理及手寫函數(shù)模擬實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件的實(shí)現(xiàn)
這篇文章主要介紹了小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件2019-04-04
JavaScript使用canvas實(shí)現(xiàn)flappy bird全流程詳解
這篇文章主要介紹了JavaScript使用canvas實(shí)現(xiàn)flappy bird流程,canvas是HTML5提供的一種新標(biāo)簽,它可以支持JavaScript在上面繪畫,控制每一個(gè)像素,它經(jīng)常被用來制作小游戲,接下來我將用它來模仿制作一款叫flappy bird的小游戲2023-03-03
JS小功能(操作Table--動(dòng)態(tài)添加刪除表格及數(shù)據(jù))實(shí)現(xiàn)代碼
這篇文章主要介紹了操作Table--動(dòng)態(tài)添加刪除表格及數(shù)據(jù)實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-11-11

