我正在尝试使用 HTML5 访问本机摄像头来为我的 Web 应用程序捕获图像,它在我的笔记本电脑上运行良好,但在移动设备上却无法运行。
我在 Android Gingerbread 中使用 Opera 12.0 对其进行测试,浏览器询问我是否允许摄像头。但是在允许后它不会在视频标签中显示任何内容。这是我的代码,
的HTML:
<video autoplay id="vid" style="" width="200" height="150"></video>
<canvas id="canvas" width="640" height="480" style="display:none;""></canvas><br>
<input type="button" value="Take Picture" onclick="snapshot()"/>
的JavaScript:
var video = document.querySelector("video");
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;
var onCameraFail = function (e) {
console.log('Camera did not work.', e);
};
function snapshot() {
if (localMediaStream) {
ctx.drawImage(video, 0, 0);
}
var strDataURI = canvas.toDataURL("image/png");
$('#canvas_image').src = strDataURI;
$.ajax({type: 'POST',url: "<?php echo base_url().'my_profile/save_image' ?>",data: {strDataURI:strDataURI },
success: function(resultData) {
location.reload();
}
});}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia({video:true}, function (stream) {
video.src = window.URL.createObjectURL(stream);
localMediaStream = stream;
}, onCameraFail);