HTML5: camera access

冷暖自知 提交于 2019-12-17 10:12:33

问题


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>

回答1:


The getUserMedia method is now supported on Firefox 17+,Chrome 23+ and Opera 12+. (See caniuse.com)




回答2:


This works on Firefox mobile, Chrome mobile, iPhone and Android:

<input type="file" id="mypic" accept="image/*">



回答3:


We've had some success with this basic approach cobbled together from across the Web:

<form method="post" action="takephoto.php" enctype="multipart/form-data">
<input type="file" accept="image/*" name="file">
<input type="submit">
</form>

Then in the PHP file we generate unique file names using now() or something similar for storage.




回答4:


<input type="file" accept="image/*;capture=camera">

See Capturing Audio & Video in HTML5

Support:

  • Android 3.0 browser - one of the first implementations. Check out this video to see it in action.
  • Chrome for Android (0.16)
  • Firefox Mobile 10.0
  • iOS6 Safari and Chrome (partial support)



回答5:


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/




回答6:


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/




回答7:


Opera Desktop, Opera mobile and Chrome (after changing some configuration) supports HTML5 camera / microphone access so far.




回答8:


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 and environment is for back camera.




回答9:


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.



来源:https://stackoverflow.com/questions/9431475/html5-camera-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!