1

I want to access camera using HTML JavaScirpt in a webview in Android phone within my native application. I tried a lot of solutions none were helpfull. The html string is as follows,

String html =
        "<!DOCTYPE html>"+
"<html>"+
"<head>"+
"<meta charset='utf-8'>"+
"<title>Display Webcam Stream</title>"+
"<style>"+
"#container {"+
"    margin: 0px auto;"+
 "   width: 500px;"+
 "   height: 375px;"+
 "   border: 10px #333 solid;"+
"}"+
"#videoElement {"+
"    width: 500px;"+
"    height: 375px;"+
"    background-color: #666;"+
"}"+
"#canvas {"+
"    width: 500px;"+
"    height: 375px;"+
"    background-color: #CCC;"+
"}"+
"</style>"+
"</head>"+
"<body>"+
"<input id='fileselect' type='file' accept='image/*' capture='camera'>"+
"<div id='container'>"+
 "   <video autoplay id='videoElement'>"+
    "</video>"+
"</div>"+
"<canvas id='canvas' width='500' height='375'></canvas>"+
"<input type='button' value='Save' id='save' />"+
"<img id='imgtag' src='' width='500' height='375' alt='capture' /> "+
"<script>"+
"var video = document.querySelector('#videoElement');"+
"navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;"+  
"if (navigator.getUserMedia) {       "+
"    navigator.getUserMedia({video: true}, handleVideo, videoError);"+
"}   "+
"function handleVideo(stream) {"+
"    video.src = window.URL.createObjectURL(stream);"+
"}"+
"function videoError(e) {"+
"}"+
"var v,canvas,context,w,h;"+
"var imgtag = document.getElementById('imgtag');"+
"var sel = document.getElementById('fileselect');"+
"document.addEventListener('DOMContentLoaded', function(){"+
"    v = document.getElementById('videoElement');"+
"    canvas = document.getElementById('canvas');"+
"    context = canvas.getContext('2d');"+
"   w = canvas.width;"+
"    h = canvas.height;"+
"},false);"+
"function draw(v,c,w,h) {"+
 "   if(v.paused || v.ended) return false;"+
  "  context.drawImage(v,0,0,w,h);"+
   "var uri = canvas.toDataURL('image/png');"+
   "imgtag.src = uri;"+
"}"+
"document.getElementById('save').addEventListener('click',function(e){"+
"   draw(v,context,w,h);"+
"});"+
"var fr;"+
"sel.addEventListener('change',function(e){"+
"   var f = sel.files[0];"+
"   fr = new FileReader();"+
"   fr.onload = receivedText;"+
"   fr.readAsDataURL(f);"+
"})"+
"function receivedText() {"+        
"   imgtag.src = fr.result;"+
"} "+
"</script>"+
"</body>"+
"</html>";

I am loading this string into my webview,

WebView browser;
    browser=(WebView)findViewById(R.id.WebView01);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
    browser.addJavascriptInterface(new WebAppInterface(this), "Android");
    browser.loadDataWithBaseURL("", html, mimeType, encoding, "");

I always get an exception in the log messages:

"Using the 'capture' attribute as an enum is deprecated. Please use it as a boolean and specify the media types that should be accepted in the 'accept' attribute." (or) "block webkit draw"

Any help will be greatfull. Thanks in advance

Bharath Keshava
  • 62
  • 3
  • 12

1 Answers1

0

change the capture attribute as "capture='true'" and try it.

venky
  • 175
  • 1
  • 3
  • 13
  • 1
    According to this link http://stackoverflow.com/questions/7387978/access-from-the-browser-to-camera, this will work with android default browser. – venky Apr 15 '15 at 11:32
  • That link said it will work with android default browser only. – venky Apr 15 '15 at 11:35