3

I want do a application with HTML5 and include a take a photo system. I have just include a code to show a camera :

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

But always show a frontal camera and i need a back camera. Can you help me, please? Thx. :)

A. M.
  • 31
  • 5
  • Sorry as I don't have an answer to your question. But I would appreciate if you could show me how to make the camera works. Could you visit my question on : http://stackoverflow.com/q/40659198/2135363 .. thanks – McLan Nov 17 '16 at 16:00

2 Answers2

3

Use MediaDevices.enumerateDevices() to determine back camera:

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.
  });
});

Also please take a look to MediaTrackConstraints.facingMode.

Tested on following devices:

  • 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
Valentin Podkamennyi
  • 7,161
  • 4
  • 29
  • 44
0

I have defined toOpenBackCamera which determines if to open a back camera or front camera. You can connect your own logic to this variable.

let backVideoDesc = {
  facingMode: "environment"
}

let frontVideoDesc = {
  facingMode: "user"
}

let toOpenBackCamera = true;

navigator.getUserMedia({
   audio: false,
   video: toOpenBackCamera ? backVideoDesc : frontVideoDesc
});

I hope this will help someone. Source: How To Change to Back Camera in Webview (Android)

Brijesh Lakkad
  • 611
  • 10
  • 13