Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

js 如何调用指定的本地摄像头

本地有两个摄像头,请问如何指定某个摄像头打开?

调用摄像头的代码如下:

<video id="videoCamera" :width="videoWidth" :height="videoHeight" autoplay></video>
<canvas style="display:none;" id="canvasCamera" :width="videoWidth" :height="videoHeight"></canvas>
          
//拍照相关方法
    // 调用权限(打开摄像头功能)
    getCompetence(canvasCamera,videoCamera) {
      vm.$nextTick(() => {
      vm.openVideo = true;
      vm.isPhoto = true
      vm.thisCancas = document.getElementById(canvasCamera);
      vm.thisContext = vm.thisCancas.getContext("2d");
      vm.thisVideo = document.getElementById(videoCamera);
      vm.thisVideo.style.display = 'block';
      // 获取媒体属性,旧版本浏览器可能不支持mediaDevices,我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 一些浏览器实现了部分mediaDevices,我们不能只分配一个对象
      // 使用getUserMedia,因为它会覆盖现有的属性。
      // 这里,如果缺少getUserMedia属性,就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function (constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia;
          // 有些浏览器不支持,会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {//不存在则报错
            return Promise.reject(
              new Error("getUserMedia is not implemented in this browser")
            );
          }
          // 否则,使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function (resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject);
          });
        };
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: "scaleX(-1)"
        }
      };
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function (stream) {
          // 旧的浏览器可能没有srcObject
          if ("srcObject" in vm.thisVideo) {
              vm.thisVideo.srcObject = stream;
          } else {
              // 避免在新的浏览器中使用它,因为它正在被弃用。
              vm.thisVideo.src = window.URL.createObjectURL(stream);
          }
          vm.thisVideo.onloadedmetadata = function () {
              vm.thisVideo.play();
          };
        })
        .catch(err => {
          console.log(err);
        });

      })
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

`
var enumeratorPromise = navigator.mediaDevices.enumerateDevices();
`

MediaDevices 的方法 enumerateDevices() 请求一个可用的媒体输入和输出设备的列表,例如麦克风,摄像机,耳机设备等。 返回的 Promise 完成时,会带有一个描述设备的 MediaDeviceInfo 的数组。

`

navigator.mediaDevices
       .getUserMedia({
           video: {
               sourceId  // 把对应的 摄像头ID 放到这里
           }
       })

`


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...