[英]Open Camera in Android WebView using HTML JS?
我想在本机应用程序的Android手机的Web视图中使用HTML JavaScirpt访问摄像机。 我尝试了很多解决方案,但都无济于事。 html字符串如下所示,
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>";
我正在将此字符串加载到我的Web视图中,
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, "");
我总是在日志消息中遇到异常:
“不建议将'capture'属性用作枚举。请将其用作布尔值,并在'accept'属性中指定应接受的媒体类型。” (或)“阻止webkit绘制”
任何帮助将是极大的。 提前致谢
将捕获属性更改为“ capture ='true'”并尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.