javascript - 使用 HTML JS 在 Android WebView 中打开相机?

标签 javascript android html webview camera

我想在我的 native 应用程序中的 Android 手机的 Web View 中使用 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, "");

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

"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"

任何帮助都会非常感激。提前致谢

最佳答案

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

关于javascript - 使用 HTML JS 在 Android WebView 中打开相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29646366/

相关文章:

Android:以步行模式开始导航

javascript - 在 jQuery 中更改 TD 元素

php - 使用 PHP 脚本从下拉列表更新 SQL 数据库

android - 运行我的 android 程序时发现 java.io.IOException 服务不可用

php - 当授权 key 是我的服务器 key 时,Firebase MismatchSenderID

php - 将某人重定向到浏览器定义的搜索引擎

javascript - 闭包的全局对象

javascript - 关于这段代码片段执行顺序的问题

php - Ajax responseText 和 echo 损坏,返回头文件内容

javascript - 验证和 ajax 其中之一不起作用