溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何實(shí)現(xiàn)使用前置攝像頭拍照

發(fā)布時(shí)間:2020-10-27 18:26:00 來源:億速云 閱讀:1606 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)微信小程序如何實(shí)現(xiàn)使用前置攝像頭拍照,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

微信小程序使用前置攝像頭拍照的具體代碼,供大家參考,具體內(nèi)容如下

微信小程序如何實(shí)現(xiàn)使用前置攝像頭拍照

1、拍照頁(yè)面:

<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&#63;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)頁(yè)面
    //調(diào)起客戶端小程序設(shè)置界面,返回用戶設(shè)置的操作結(jié)果
    wx.openSetting({
    success:(res)=> {
     res.authSetting = {
     "scope.camera": true
     }
     this.openCamera=true
    },
    })
   } else if (res.cancel) {//拒絕打開授權(quán)頁(yè)面
    wx.navigateBack({delta:1})
   }
   }
  })
  },
  
 }
 }
</script>

2、預(yù)覽圖片頁(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è)頁(yè)面
   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)前頁(yè)面
  }
  
  } else if (resData.status < 1) {
   setErrorMessage(resData.msg)
  } else {
   setErrorMessage()
  }
 },
 fail:(res)=>{
  console.log("上傳失敗");
 },
 });
}

看完上述內(nèi)容,你們對(duì)微信小程序如何實(shí)現(xiàn)使用前置攝像頭拍照有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI