7

How do you enable the front camera on a Webview? I have enable the features in AndroidManifest.xml

 <uses-feature android:name="android.hardware.camera" android:required="true" />
 <uses-feature android:name="android.hardware.camera.front" android:required="true" />

The camera is not going to be used for taking photos or recording, just to switch on the front camera.

When I go to the website using the phone browser the phone camera works once allow the prompt message. How can this work with a webview?

In the html file has a Canvas and Video tag that displays webcam It doesn't record or take pictures it just shows you the camera view.

Here is the html code

 <canvas id="inCanvas"  width="500" height="500" style="display:none"></canvas>
 <video id="inputVideo" width="100" height="100" autoplay loop ></video>

It work with webcam but not with webview in android.

user3210416
  • 784
  • 1
  • 9
  • 19

2 Answers2

4

I didnt quite understand, but i thing there could one of the following two, what you want.

1) access camera and just show the video on the screen(not capturing image):

html:

  <canvas id='canvas' width='100' height='100'></canvas> 

js:

 var onFailSoHard = function(e)
    {
            console.log('failed',e);
    }

    window.URL = window.URL || window.webkitURL ;
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;

    var video = document.querySelector('video');

    if(navigator.getUserMedia)
    {
        navigator.getUserMedia({video: true},function(stream) {
        video.src = window.URL.createObjectURL(stream);
        },onFailSoHard);
    }


        var canvas = document.getElementById('canvas'); 
    var ctx = canvas.getContext('2d');
setInterval(function(){ 
ctx.drawImage(video,0,0);
     }, 100);

        }  

2) capture image from the camera:

here is the doc for that.

navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
    destinationType: Camera.DestinationType.DATA_URL
});

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

function onFail(message) {
    alert('Failed because: ' + message);
}
Naeem Shaikh
  • 14,231
  • 4
  • 43
  • 80
0

I would use something similar to the below as a script to access the phone camera.

<script>
  var errorCallback = function(e) {
    console.log('Rejected!', e);
  };

  // Not showing vendor prefixes.
  navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
    var video = document.querySelector('video');
    video.src = window.URL.createObjectURL(localMediaStream);

    // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
    // See crbug.com/110938.
    video.onloadedmetadata = function(e) {
      // Ready to go. Do some stuff.
    };
  }, errorCallback);
</script>

Used the following tutorial to help me. Hope it sets yuo on the right track :)

Michele La Ferla
  • 6,265
  • 11
  • 44
  • 75