Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable front camera in webview for android

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.

like image 964
user3210416 Avatar asked Jan 12 '15 18:01

user3210416


People also ask

How do you access the camera from WebView flutter?

To request permissions about the camera and microphone, you can use the permission_handler plugin. Also, it has the androidOnPermissionRequest event for Android, that is an event fired when the WebView is requesting permission to access the specified resources (that is the Android native WebChromeClient.

Is Android WebView deprecated?

Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.


1 Answers

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);
}
like image 168
Naeem Shaikh Avatar answered Oct 14 '22 09:10

Naeem Shaikh