HTML5:相机访问

前端之家收集整理的这篇文章主要介绍了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,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>

解决方法

Firefox 17,Chrome 23和Opera 12现在支持getUserMedia方法。 (见 caniuse.com)
原文链接:https://www.f2er.com/html5/169752.html

猜你在找的HTML5相关文章