【问题标题】:Enable rear camera with HTML5使用 HTML5 启用后置摄像头
【发布时间】:2023-03-27 08:59:01
【问题描述】:

我正在使用 MVC ASP.Net 4 HTML5 开发一个项目(默认浏览器是 google-chrome v29.0.1547.57)我可以与这些工具交互并拍照,但只能使用前置摄像头,我如何可以启用后置摄像头吗? 平板电脑的特点:三星 Galaxy Tab 2 我希望你能帮助我

【问题讨论】:

标签: android html google-chrome asp.net-mvc-4 android-camera


【解决方案1】:

查看https://simpl.info/getusermedia/sources/,了解如何使用

MediaStreamTrack.getSources(gotSources);

然后您可以选择源并将其作为可选参数传递给 getUserMedia

var constraints = {
  audio: {
    optional: [{sourceId: audioSource}]
  },
  video: {
    optional: [{sourceId: videoSource}]
  }
};
navigator.getUserMedia(constraints, successCallback, errorCallback);

它现在在稳定版 Chrome 和移动版中完全可用(自 v30 起)

【讨论】:

  • Kinlan 我试用了测试版(谷歌浏览器),效果很好,我希望最终版本能尽快更新我的浏览器,谢谢你的提示。
  • 请注意,现在不推荐使用 getSources。 chromestatus.com/feature/4765305641369600
  • @Kinlan 我很难强制 Chrome 使用后置摄像头(在 Nexus5 上测试),即使我对源代码进行了硬编码...... Firefox 和 Opera 会自动询问用户使用哪个摄像头采用。有什么想法吗?
【解决方案2】:

可以在https://webrtc.github.io/samples/src/content/devices/input-output/ 找到演示。这将允许访问前后摄像头。

您会发现许多演示都依赖于已弃用的功能:

MediaStreamTrack.getSources() 

从 Chrome 45 和 FireFox 39 开始,您将需要使用该功能:

MediaDevices.enumerateDevices()

例子:

if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
  console.log("enumerateDevices() not supported.");
  return;
}

// List cameras and microphones.

navigator.mediaDevices.enumerateDevices()
  .then(function(devices) {
    devices.forEach(function(device) {
      console.log(device.kind + ": " + device.label +
        " id = " + device.deviceId);
    });
  })
  .catch(function(e) {
    console.log(e.name + ": " + e.message);
  });

更多文档可以在这里找到:https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices

【讨论】:

  • 还值得一提的是,Chrome 仅在 https 连接上启用相机。
【解决方案3】:

在三星 S8 上的 Chrome 中,我可以使用“面向模式”=“环境”从“后置摄像头”获取视频。默认似乎是“用户”(“前置”摄像头)

在 TypeScript 中:

    const video = document.getElementById("video");
    const constraints = {
        advanced: [{
            facingMode: "environment"
        }]
    };
    navigator.mediaDevices
        .getUserMedia({
            video: constraints
        })
        .then((stream) => {
            video.src = window.URL.createObjectURL(stream);
            video.play();
        });

参考: MediaTrackConstraints/facingMode

【讨论】:

    【解决方案4】:

    上次我开发该代码时,这里是我使用的版本:您在代码中直接调用函数 whichCamera 并指定哪个相机“用户”、“环境”或“计算机”'如果你正在运行一台电脑)

    `//----------------------------------------------------------------------
    //  whichCamera(Type)
    //    For smartphone or tablet :
    //     Start the type={user,environment} camera.
    //    For computer it's simple :
    //      type = "computer".
    //----------------------------------------------------------------------
    var streamSrc, cameraType;
    function whichCamera(type){
    
      var cameraFacing;
      cameraType = type;
      if( type == "user")
        cameraFacing = 0;
      else if( type == "environment")
        cameraFacing = 1;
      else if( type == "computer"){
        cameraFacing = 2;
      }
      console.log(type+" index : "+cameraFacing);
    
      //  Here we list all media devices, in order to choose between
      //  the front and the rear camera.
      //      videoDevices[0] : user Camera
      //      videoDevices[1] : environment Camera
      //  Then set the video resolution.
      navigator.mediaDevices.enumerateDevices()
      .then(devices => {
        var videoDevices, videoDeviceIndex, constraints;
        //  Initialize the array wich will contain all video resources IDs.
        //  Most of devices have two video resources (Front & Rear Camera).
        videoDevices = [0,0];
        //  Simple index to browse the videa resources array (videoDevices).
        videoDeviceIndex = 0;
        //  devices.forEach(), this function will detect all media resources (Audio, Video) of the device
        //  where we run the application.
        devices.forEach(function(device) {
          console.log(device.kind + ": " + device.label +
            " id = " + device.deviceId);
          // If the kind of the media resource is video,
          if (device.kind == "videoinput") {
            //  then we save it on the array videoDevices.
            videoDevices[videoDeviceIndex++] =  device.deviceId;
            console.log(device.deviceId+" = "+videoDevices[videoDeviceIndex-1]);
          }
        });
        console.log("Camera facing ="+cameraFacing+" ID = "+videoDevices[videoDeviceIndex-1]);
    
        // Here we specified which camera we start,
        //  videoDevices[0] : Front Camera
        //  videoDevices[1] : Back Camera
        if( cameraFacing != "computer"){
          constraints = { deviceId: { exact: videoDevices[cameraFacing]  }};
          return navigator.mediaDevices.getUserMedia({ video:
                                                              constraints,
                                                              width: { min: 1280, ideal: 1600, max: 1920 },
                                                              height: { min: 720, ideal: 1200, max: 1080 }
                                                      }
                                                    );
        }else
          return navigator.mediaDevices.getUserMedia({ video: true });
        })
        //  Then we retrieve the link to the video stream.
        .then(stream => {
          if (window.webkitURL) {
            video.src = window.webkitURL.createObjectURL(stream);
            localMediaStream = stream;
            console.log(localMediaStream +" = "+ stream)
          } else if (video.mozSrcObject !== undefined) {
            video.mozSrcObject = stream;
            console.log(video.mozSrcObject +" = "+ stream)
          } else if (video.srcObject !== undefined) {
            video.srcObject = stream;
            console.log(video.srcObject +" = "+ stream)
          } else {
            video.src = stream;
            console.log(video.src +" = "+ stream)
          }
          streamSrc = stream;
        })
        .catch(e => console.error(e));
    
    }
    

    【讨论】:

    【解决方案5】:
            //----------------------------------------------------------------------
            //  Here we list all media devices, in order to choose between
            //  the front and the back camera.
            //      videoDevices[0] : Front Camera
            //      videoDevices[1] : Back Camera
            //  I used an array to save the devices ID 
            //  which i get using devices.forEach()
            //  Then set the video resolution.
            //----------------------------------------------------------------------
            navigator.mediaDevices.enumerateDevices()
            .then(devices => {
              var videoDevices = [0,0];
              var videoDeviceIndex = 0;
              devices.forEach(function(device) {
                console.log(device.kind + ": " + device.label +
                  " id = " + device.deviceId);
                if (device.kind == "videoinput") {  
                  videoDevices[videoDeviceIndex++] =  device.deviceId;    
                }
              });
    
    
              var constraints =  {width: { min: 1024, ideal: 1280, max: 1920 },
              height: { min: 776, ideal: 720, max: 1080 },
              deviceId: { exact: videoDevices[1]  } 
            };
            return navigator.mediaDevices.getUserMedia({ video: constraints });
    
          })
            .then(stream => {
              if (window.webkitURL) {
                video.src = window.webkitURL.createObjectURL(stream);
                localMediaStream = stream;
              } else if (video.mozSrcObject !== undefined) {
                video.mozSrcObject = stream;
              } else if (video.srcObject !== undefined) {
                video.srcObject = stream;
              } else {
                video.src = stream;
              }})
            .catch(e => console.error(e));
    

    【讨论】:

    • 嗨 Lightning2050 我能得到完整的代码如何切换我试过的源,但它不起作用!!提前致谢
    • 嘿!看看这个:link
    • @Nahdiroviç 您是否有任何 html 可以与之搭配? JS 仅在移动 chrome 上对我不起作用。
    • @MajaJelen,你可以看看here,在那里你可以找到适合我的 JS 代码的 HTML 代码。检查我的代码的最新版本“Jul 16 '17 at 15:53”
    猜你喜欢
    • 2019-07-19
    • 2018-02-14
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多