【问题标题】:Open Camera in Android WebView using HTML JS?使用 HTML JS 在 Android WebView 中打开相机?
【发布时间】:2015-04-15 09:21:19
【问题描述】:

我想在我的原生应用程序的 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>";

我正在将此字符串加载到我的 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, "");

我总是在日志消息中收到异常:

“不推荐使用 'capture' 属性作为枚举。请将其用作布尔值并在 'accept' 属性中指定应接受的媒体类型。” (或)“阻止 webkit 绘制”

任何帮助都会非常有用。提前致谢

【问题讨论】:

    标签: javascript android html webview camera


    【解决方案1】:

    将捕获属性更改为“capture='true'”并尝试一下。

    【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2018-01-24
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多