I want to access phone's camera using HTML5 on Android. Currently I have Chrome 31.0.1650.59 on Kitkat.
I can confirm my code working in Chrome browser on Android but I can't get it working in WebView. I do not want to issue an intent and use default camera app. I want to display camera feed in WebView.
Here is my HTML code.
<video id="video" width="640" height="480" autoplay></video>
<script>
window.addEventListener("DOMContentLoaded", function() {
var video = document.getElementById("video"),
videoObj = { "video": true },
errBack = function(error) {
console.log("Video capture error: ", error.code);
};
if(navigator.webkitGetUserMedia) {
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
}
}, false);
</script>
Which is pretty standard.
Here is android code which displays this page:
WebView webView = (WebView) rootView.findViewById(R.id.webView);
WebSettings ws = webView.getSettings();
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result){
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
return true;
}
@Override
public boolean onJsPrompt (WebView view, String url, String message, String defaultValue, JsPromptResult result){
return true;
}
});
webView.setWebViewClient(new WebViewClient());
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
ws.setDomStorageEnabled(true);
webView.loadUrl("http://html-page.htm");
Lots of these configs have nothing to do with the problem but desperate are attempts.
HTML5 video accessing the local camera is only supported in Android 5 and newer. I am currently accessing the built-in camera (Front and Back) entirely from HTML5 and some javascript, because our app will only be running on Android 5 and newer. In the back end, there is some code you can use to overrride the webview so that it will automatically accept the security prompt about allowing camera access. So unless you are Android 5 or newer, it won't be possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With