当前位置: 首页 > article >正文

Vue 调用电脑摄像头拍照 返回base64格式图片 简单例子

调用电脑摄像头拍照的简单例子,延伸使用可参考往期帖子 戳我传送

<template>
  <div>
    <el-button @click="isShow = !isShow">{{
      isShow ? "显示" : "隐藏"
    }}</el-button>
    <el-button @click="OpenCamera" v-show="!isOpen">打开摄像头</el-button>
    <el-button @click="CloseCamera" v-show="isOpen">关闭摄像头</el-button>
    <el-button @click="setImage" v-show="isOpen">拍照</el-button>
    <br />
    <video
      ref="captureVideo"
      id="captureVideo"
      :class="isShow ? 'isShow' : ''"
    ></video>
    <canvas id="canvasCamera" :class="isShow ? 'isShow' : ''"></canvas>
    <div v-if="imgSrc" :class="isShow ? 'isShow' : ''">
      <img :src="imgSrc" class="captureImg" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imgSrc: "", // 拍照图片
      canvas: null,
      context: null,
      videoWidth: 0, //视频实际宽度
      videoHeight: 0, //视频实际高度
      isShow: false,
      isOpen: false,
    };
  },
  mounted() {
    // this.OpenCamera();
  },
  methods: {
    // 调用打开摄像头功能
    OpenCamera(flag) {
      // console.log("flag", flag);
      var me = this;
      // 获取 canvas 画布
      this.canvas = document.getElementById("canvasCamera");
      this.context = this.canvas.getContext("2d");
      // 旧版本浏览器可能根本不支持mediaDevices,我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 正常支持版本
      navigator.mediaDevices
        .getUserMedia({
          video: true,
        })
        .then((stream) => {
          this.isOpen = true;
          // 摄像头开启成功
          this.$refs.captureVideo.srcObject = stream;
          this.$refs.captureVideo.play();
          var video = document.querySelector("#captureVideo");
          video.addEventListener("canplay", function () {
            me.videoWidth = video.videoWidth;
            me.videoHeight = video.videoHeight;
            video.style.width = me.videoWidth + "px";
            video.style.height = me.videoHeight + "px";

            var canvas = document.querySelector("canvas");
            canvas.width = me.videoWidth;
            canvas.height = me.videoHeight;
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 关闭摄像头
    CloseCamera() {
      console.log("关闭摄像头");
      this.$refs.captureVideo.srcObject.getTracks()[0].stop();
      this.isOpen = false;
      this.imgSrc = null;
      this.context.clearRect(0, 0, this.videoWidth, this.videoHeight);
    },
    // 拍照 绘制图片
    setImage(type) {
      console.log("拍照");
      // 点击canvas画图
      this.context.drawImage(
        this.$refs.captureVideo,
        0,
        0,
        this.videoWidth,
        this.videoHeight
      );
      // 获取图片base64链接
      const image = this.canvas.toDataURL("image/png");
      this.imgSrc = image;
      // 干点啥呢
    },
  },
};
</script>

<style lang="scss" scoped>
#captureVideo {
  width: 100px;
  // display: none;
}
#canvasCamera {
  // display: none;
}
.captureImg {
  // display: none;
  width: 300px;
  height: auto;
}
.isShow {
  display: none;
}
</style>

http://www.kler.cn/news/353493.html

相关文章:

  • Rust学习如何更有信心?
  • 【部署篇】Redis-03主从模式部署(源码方式安装)
  • 6CXX:UICC告诉终端数据长度
  • PyCharm配置Flask开发环境
  • 【数据结构-栈】【位运算优化】力扣3170. 删除星号以后字典序最小的字符串
  • SQL server 存储过程与函数
  • 【数据结构与算法】LeetCode每日一题
  • 机器学习与神经网络:开启物理学的新篇章
  • SEM推广如何进行数据分析
  • Ubuntu:用户不在sudoers文件中
  • 【Java小白图文教程】-01-Java环境安装-变量
  • 计算机是如何输入存储输出汉字、图片、音频、视频的
  • 10:00面试,10:08就出来了,问的问题有点变态。。。
  • Spring Boot框架下的知识管理策略
  • 【Python爬虫】看电影还在用VIP?一个python代码让你实现电影自由!附源码
  • C#使用PdfSharp生成PDF文件实例详解
  • find_library、pkg_check_modules、pkg_search_module的区别
  • 【Flutter】Dart:环境搭建
  • 1.项目初始化
  • 关键词提取技术:TF-IDF 详解