欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例

 更新時(shí)間:2024年01月15日 09:53:17   作者:小馬甲丫  
本文主要介紹了vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例,主要是使用navigator.mediaDevices.getUserMedia這個(gè)API來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下

一、前言

Vue3 調(diào)用本地?cái)z像頭實(shí)現(xiàn)拍照功能,由于調(diào)用攝像頭有使用權(quán)限,只能在本地運(yùn)行,線上需用 https 域名才可以使用。主要是使用navigator.mediaDevices.getUserMedia這個(gè)API來實(shí)現(xiàn)。

二、文檔

navigator.mediaDevices.getUserMedia,MDN的官方文檔點(diǎn)擊【前往】。

向用戶請(qǐng)求獲得媒體輸入的許可,返回一個(gè)MediaStream,我們可以使用MediaStreamvideo組件綁定輸出攝像頭拍攝的視頻,也能記錄麥克風(fēng)的音頻

三、實(shí)現(xiàn)

3.1、封裝

封裝組件take-photo.vue

/**
 * @Description: 拍照
 * @author 小馬甲丫
 * @date 2024-01-07 12:03:04
*/

<template>
  <a-modal
    :width="800"
    :height="600"
    title="讀身份證"
    @cancel="hideModal"
    v-model:visible="visibleFlag"
  >
    <!-- 畫筆控件 用來拍照 -->
    <canvas style="display: none" ref="canvasDom"></canvas>
    <!-- 播放器,用來播放拍攝的視頻 -->
    <video v-if="!imgurl" class="camera_video" ref="videoDom"></video>
    <!--  顯示照片  -->
    <img v-else :src="imgurl" />
    <template #footer>
      <a-space>
        <a-button @click="hideModal">關(guān)閉</a-button>
        <a-button type="primary" @click="takePhoto">{{ imgurl ? "重拍" : "拍照" }}</a-button>
      </a-space>
    </template>
  </a-modal>
</template>

<script setup>
  import { ref, nextTick } from "vue";
  // canvas控件對(duì)象
  const canvasDom = ref(null);
  // video 控件對(duì)象
  const videoDom = ref(null);
  // 照片路徑
  const imgurl = ref(null);

  const emits = defineEmits(['save']);

  // ------------------ 顯示,關(guān)閉 ------------------
  // 顯示
  const visibleFlag = ref(false);
  function showModal() {
    imgurl.value = ''
    visibleFlag.value = true;
    openCamera();
  }

  // 關(guān)閉
  function hideModal() {
    visibleFlag.value = false;
  }

  const openCamera = () => {
    // 檢測瀏覽器是否支持mediaDevices
    if (navigator.mediaDevices) {
      navigator.mediaDevices
      // 開啟視頻,關(guān)閉音頻
      .getUserMedia({audio: false, video: true})
      .then((stream) => {
        // 將視頻流傳入viedo控件
        videoDom.value.srcObject = stream;
        // 播放
        videoDom.value.play();
      })
      .catch((err) => {
        console.log(err);
      });
    } else {
      window.alert("該瀏覽器不支持開啟攝像頭,請(qǐng)更換最新版瀏覽器");
    }
  };

  // 拍照
  const takePhoto = () => {
    // 如果已經(jīng)拍照了就重新啟動(dòng)攝像頭
    if (imgurl.value) {
      imgurl.value = null;
      openCamera()
      return;
    }

    // 設(shè)置畫布大小與攝像大小一致
    canvasDom.value.width = videoDom.value.videoWidth;
    canvasDom.value.height = videoDom.value.videoHeight;
    // 執(zhí)行畫的操作
    canvasDom.value.getContext("2d").drawImage(videoDom.value, 0, 0);
    // 將結(jié)果轉(zhuǎn)換為可展示的格式
    imgurl.value = canvasDom.value.toDataURL("image/webp");
    // 關(guān)閉攝像頭
    stop();
    nextTick(() => {
      emits('save', imgurl.value)
      hideModal()
    })
  }

  // 關(guān)閉攝像頭
  const stop = () => {
    let stream = videoDom.value.srcObject;
    if (!stream) return;
    let tracks = stream.getTracks();
    tracks.forEach((x) => {
      x.stop();
    });
  };

  // ----------------------- 以下是暴露的方法內(nèi)容 ------------------------
  defineExpose({
    showModal,
    hideModal
  });
</script>

<style lang="less" scoped>
.camera_video {
  width: 100%;
  height: 100%;
  border: 2px black solid;
}
</style>

3.2、使用

/**
 * @Description: 使用
 * @author 小馬甲丫
 * @date 2023-12-20 08:07:47
*/

<template>
  <div>
  	<img :src="photo" />
    <a-button ghost type="primary" @click="readCard">拍照</a-button>
    <TakePhoto ref="photoRef" @save="handlePhoto" />
  </div>
</template>
<script setup>
  import { ref } from 'vue';
  import TakePhoto from './take-photo.vue';

  const photoRef = ref()
  const photo = ref()

  // 拍照
  function readCard() {
    photoRef.value.showModal()
  }

  // 拍照回調(diào)
  function handlePhoto(img) {
  	photo.value = img
  }
</script>

3.3、效果

四、最后

到此這篇關(guān)于vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue3開啟攝像頭拍照內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論