Enable rear camera with HTML5

rar*_*bar 30 html5 android google-chrome android-camera asp.net-mvc-4

I'm working on a project with MVC ASP.Net 4 HTML5 (default browser is google-chrome v29.0.1547.57) I can interact with these tools and take photographs but only with front camera, How I could enable rear camera? the characteristic of the Tablet: Samsung Galaxy Tab 2 I hope you can help me

Kin*_*lan 48

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

MediaStreamTrack.getSources(gotSources);
Run Code Online (Sandbox Code Playgroud)

然后,您可以选择源并将其作为可选项传入getUserMedia

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

它现在完全可用于稳定的Chrome和移动设备(截至v30)

  • 请注意,现在不推荐使用getSources.https://www.chromestatus.com/feature/4765305641369600 (3认同)

小智 25

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

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

MediaStreamTrack.getSources() 
Run Code Online (Sandbox Code Playgroud)

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

MediaDevices.enumerateDevices()
Run Code Online (Sandbox Code Playgroud)

例:

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);
  });
Run Code Online (Sandbox Code Playgroud)

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

  • 还值得一提的是,Chrome 仅在 https 连接上启用相机。 (2认同)

Sea*_*ean 7

在我的三星S8 Chrome中,我可以使用"facingMode"="environment"从"后置摄像头"拍摄视频.默认似乎是"用户"('前'摄像头)

在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();
        });
Run Code Online (Sandbox Code Playgroud)

ref: MediaTrackConstraints/facingMode