vue h5调用摄像头

vue h5调用摄像头

<template>
  <div>
    <video ref="cameraVideo" id="video" preload autoplay loop muted></video>
    <button type="primary" @click="openCamera">打开摄像头</button>
    <button type="primary" @click="closeCamera">关闭摄像头</button>
    <button type="primary" @click="switchCamera">切换摄像头</button>
    {{ logInfo }}
  </div>
</template>
<script>

export default {
  name: "cameraVideo",
  props: {
    videoWidth: {
      type: Number,
      default: 300,
    },
    videoHeight: {
      type: Number,
      default: 300,
    }
  },
  data() {
    return {
      videoEle: null,
      constraints: {
        video: {
          deviceId: {
            exact: "",
          },
          width: this.videoWidth,
          height: this.videoHeight,
        },
        audio: false,
      },
      cameraArray: [],
      cameraIndex: 0,
      openCameraLock: false,
      closeCameraLock: false,
      logInfo: ''
    };
  },
  created() {
    navigator.mediaDevices.getUserMedia({ video: true }).then((MediaStream) => {
      //查找所有可用摄像头 此方式在localhost域下正常,在正式环境需要https的请求方式不然会报错 navigator.mediaDevices is undefined
      navigator.mediaDevices
        .enumerateDevices()
        .then((devices) => {
          this.logInfo += JSON.stringify(devices)
          //console.log(devices);
          this.cameraArray = [];
          devices.forEach((device) => {
            if (device.kind == "videoinput") {
              this.cameraArray.push(device.deviceId);
            }
          });
          if (this.cameraArray.length > 0) {
            this.constraints.video.deviceId.exact = this.cameraArray[0]; //设置默认摄像头
          }
          this.logInfo += JSON.stringify(this.cameraArray)
          this.logInfo += JSON.stringify(this.constraints)
        })
        .catch(function (err) {
          console.log(err.name + ": " + err.message);
          this.logInfo += JSON.stringify(err)
        });
    })
      .catch((PermissionDeniedError) => {
        this.logInfo += JSON.stringify(PermissionDeniedError)
        this.logInfo += 123
      });
  },
  methods: {
    openCamera() {
      if (this.openCameraLock) {
        return;
      }
      this.openCameraLock = true;
      this.closeCamera();
      console.log("打开摄像头");
      if (this.cameraArray.length <= 0) {
        this.$message.warning("没有可用的摄像头");
      }
      this.videoEle = this.$refs.cameraVideo;

      let promise = navigator.mediaDevices.getUserMedia(this.constraints); // h5 新的API

      promise
        .then((MediaStream) => {
          this.logInfo += 'promise.then'
          this.videoEle.srcObject = MediaStream;
          this.videoEle.play();
        })
        .catch((PermissionDeniedError) => {
          this.logInfo += 'promise.catch'
        });
      this.openCameraLock = false;
      console.log("摄像头开启完毕");
    },

    //切换摄像头
    switchCamera() {
      if (
        this.cameraArray.length > 0 &&
        !this.closeCameraLock &&
        !this.openCameraLock
      ) {
        if (
          this.constraints.video.deviceId.exact ===
          this.cameraArray[this.cameraIndex]
        ) {
          this.cameraIndex++;
          if (this.cameraIndex >= this.cameraArray.length) {
            this.cameraIndex = 0;
          }
        }
        this.constraints.video.deviceId.exact = this.cameraArray[
          this.cameraIndex
        ];
        //console.log(this.constraints.video.deviceId.exact);
        this.closeCamera();
        this.openCamera();
        return true;
      }
    },
    closeCamera() {
      if (this.videoEle != null && this.videoEle.srcObject != null) {
        console.log("关闭摄像头");
        this.closeCameraLock = true;
        let stream = this.videoEle.srcObject;
        let tracks = stream.getTracks();
        tracks.forEach((track) => {
          track.stop();
        });
        this.videoEle.pause();
        this.videoEle = null;
        this.closeCameraLock = false;
        console.log("摄像头已关闭");
      }
    },
  },
  destroyed() {
    this.closeCamera();
  },
};
</script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值