HTML5:相机访问
我对 HTML5
还很陌生。我尝试使用以下 HTML5
代码来访问手机上的相机。它始终显示“不支持本机网络摄像头”。看来我的移动浏览器(safari 和 android 2.1 网络浏览器)不支持相机。
您能告诉我应该使用哪个浏览器来访问相机吗?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
<style>
body {width: 100%;}
canvas {display: none;}
</style>
<script>
var video, canvas, msg;
var load = function () {
video = document.getElementById('video');
canvas = document.getElementById('canvas');
msg = document.getElementById('error');
if( navigator.getUserMedia ) {
video.onclick = function () {
var context = canvas.getContext("2d");
context.drawImage(video, 0, 0, 240, 320);
var image = {"demo" : {
"type" : "device",
"image" : canvas.toDataURL("image/png")
}};
};
var success = function ( stream ) {
video.src = stream;
};
var error = function ( err ) {
msg.innerHTML = "Error: " + err.code;
};
navigator.getUserMedia('video', success, error);
} else {
msg.innerHTML = "Native web camera not supported :(";
}
};
window.addEventListener('DOMContentLoaded', load, false);
</script>
</head>
<body>
<video id="video" width="240" height="320" autoplay> </video>
<p id="error">Click on the video to send a snapshot to the receiving screen</p>
<canvas id="canvas" width="240" height="320"> </canvas>
</body>
</html>
I am quite new to HTML5
. I try the following HTML5
code to access camera on my mobile phone. It always display "Native web camera not supported". It seems that my mobile browser (safari and android 2.1 web browser) does not support the camera.
Could you please tell me which browser should I use to access to camera?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1.0">
<style>
body {width: 100%;}
canvas {display: none;}
</style>
<script>
var video, canvas, msg;
var load = function () {
video = document.getElementById('video');
canvas = document.getElementById('canvas');
msg = document.getElementById('error');
if( navigator.getUserMedia ) {
video.onclick = function () {
var context = canvas.getContext("2d");
context.drawImage(video, 0, 0, 240, 320);
var image = {"demo" : {
"type" : "device",
"image" : canvas.toDataURL("image/png")
}};
};
var success = function ( stream ) {
video.src = stream;
};
var error = function ( err ) {
msg.innerHTML = "Error: " + err.code;
};
navigator.getUserMedia('video', success, error);
} else {
msg.innerHTML = "Native web camera not supported :(";
}
};
window.addEventListener('DOMContentLoaded', load, false);
</script>
</head>
<body>
<video id="video" width="240" height="320" autoplay> </video>
<p id="error">Click on the video to send a snapshot to the receiving screen</p>
<canvas id="canvas" width="240" height="320"> </canvas>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Firefox 17+、Chrome 23+ 和 Opera 12+ 现在支持
getUserMedia
方法。 (请参阅 caniuse.com)The
getUserMedia
method is now supported on Firefox 17+,Chrome 23+ and Opera 12+. (See caniuse.com)这适用于 Firefox 手机、Chrome 手机、iPhone 和 Android:
This works on Firefox mobile, Chrome mobile, iPhone and Android:
请参阅捕获音频和音频HTML5 视频
See Capturing Audio & Video in HTML5
我们通过网络拼凑而成的基本方法取得了一些成功:
然后在 PHP 文件中,我们使用 now() 或类似的存储方法生成唯一的文件名。
We've had some success with this basic approach cobbled together from across the Web:
Then in the PHP file we generate unique file names using now() or something similar for storage.
我认为 Opera 是唯一支持此 HTML5 扩展的移动浏览器。
请参阅作者对此主题的注释;
http://francisshanahan.com/index.php/2011/stream-a-webcam-using-javascript-nodejs-android-opera-mobile-web-sockets-and-html5/
I think Opera is the only mobile browser that supports this HTML5 extension.
See note from the author to this thread;
http://francisshanahan.com/index.php/2011/stream-a-webcam-using-javascript-nodejs-android-opera-mobile-web-sockets-and-html5/
我最近刚刚开始使用一个名为 Bridgeit 的工具。
这是浏览器中的 JavaScript 和手机上的应用程序的组合。到目前为止似乎效果很好。
http://bridgeit.mobi/
I've just recently started working with a tool called Bridgeit.
This is a combination of javascript in the browser and an app on the phone. It seems to work pretty well so far.
http://bridgeit.mobi/
HTML5 添加了对相机访问的支持,您可以像这样使用它:
<输入类型=“文件”接受=“图像/*”捕获=“用户”>
其中
user
用于前置摄像头,environment
用于后置摄像头。HTML5 added support for camera access, you can use it like this:
<input type="file" accept="image/*" capture>
<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">
Where
user
is for front camera andenvironment
is for back camera.Opera Desktop、Opera mobile 和 Chrome(更改一些配置后)目前支持 HTML5 摄像头/麦克风访问。
Opera Desktop, Opera mobile and Chrome (after changing some configuration) supports HTML5 camera / microphone access so far.
您可以通过启用 chrome://flags 下的“启用 WebRTC”标志来使用 Android 版 Chrome,
我在 Android 手机上测试过,可以访问 html5 页面上的摄像头,并且它可以正常工作。
You can use Chrome for Android by enabling the "Enable WebRTC" flag under chrome://flags
I tested on my android phone to access camera on html5 page and it's working.