我想用HTML5做一个应用程序,并包括一个照相系统。我只包含了一个显示摄像机的代码:

mWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onPermissionRequest(final PermissionRequest request) {
                    KioskActivity.this.runOnUiThread(new Runnable(){
                        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                        @Override
                        public void run() {
                            request.grant(request.getResources());
                        }// run
                    });// MainActivity
                }// onPermissionRequest
            });// setWebChromeClient


但始终要显示前置摄像头,而我需要后置摄像头。你能帮我吗?谢谢。 :)

最佳答案

使用MediaDevices.enumerateDevices()确定后置摄像头:

navigator.mediaDevices.enumerateDevices().then(function(devices) {
  var cameras = [];
  devices.forEach(function(device) {
    'videoinput' === device.kind && cameras.push(device.deviceId);
  });
  // On my devices:
  // - cameras[0] - front camera;
  // - cameras[1] - back camera;
  var constraints = {video: {deviceId: {exact: cameras[1]}}};
  navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
    // Do something with stream.
  });
});


另外,请查看MediaTrackConstraints.facingMode

在以下设备上测试:


Mozilla / 5.0(Linux; Android 7.1.1; Pixel Build / NOF27B; wv)AppleWebKit / 537.36(KHTML,like Gecko)Version / 4.0 Chrome / 56.0.2924.87 Mobile Safari / 537.36
Mozilla / 5.0(Linux; Android 6.0.1; MotoG3 Build / MPI24.107-55-2; wv)AppleWebKit / 537.36(KHTML,like Gecko)Version / 4.0 Chrome / 56.0.2924.87 Mobile Safari / 537.36
Mozilla / 5.0(Linux; Android 6.0.1; Moto G Play Build / MPI24.241-2.35-1; wv)AppleWebKit / 537.36(KHTML,like Gecko)Version / 4.0 Chrome / 56.0.2924.87 Mobile Safari / 537.36
Mozilla / 5.0(Linux; Android 6.0.1; ONE E1005 Build / MMB29M; wv)AppleWebKit / 537.36(KHTML,like Gecko)Version / 4.0 Chrome / 55.0.2883.91 Mobile Safari / 537.36

10-08 17:28