41

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>
User97693321
  • 3,336
  • 7
  • 45
  • 69
love sunset
  • 517
  • 1
  • 8
  • 10
  • 7
    HTML5's WebCam API is still very new, and it's not even very widely supported on *desktops*, much less mobile devices. You can't depend on it being available. – Kitsune Feb 24 '12 at 13:27
  • see here for a demo of HTML5 Microphone & Camera access in Chrome.. this is bleeding edge stuff.. http://www.youtube.com/watch?feature=player_detailpage&v=VP0SV3YWOWA#t=1425s – Lloyd Feb 24 '12 at 14:49
  • HTML5 mobile support no longer "bleeding edge" - see http://stackoverflow.com/a/13489553/957950 for how to get images directly from the camera, or test example at http://mobilehtml5.org/ts/?id=23. Welcome to the future! Still no hoverboards, sorry. :( – brichins Feb 18 '16 at 17:21

9 Answers9

40

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

Screenshot of the CanIUse.com support grid as of 2/24/12

Quannt
  • 2,035
  • 2
  • 21
  • 29
Anthony Faull
  • 17,549
  • 5
  • 55
  • 73
  • I have installed chrome on my ipad but still does not open the video – J888 May 29 '13 at 02:29
  • 2
    @Anthony, Question was asked about accessing camera on Mobile phone. Firefox 17+, Chrome 23+, Opera 12+ are not available on Mobile! – Om Shankar Jul 08 '13 at 06:04
14

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

<input type="file" id="mypic" accept="image/*">
Jez D
  • 1,461
  • 2
  • 25
  • 52
  • Does this invoke the camera on iPhones and Android phones? – Travis M. Jan 24 '13 at 17:26
  • That's not WebRTC or related to getuserMedia(). It was related to the proposed HTML Media Capture which will probably go away. More info [here](http://dev.w3.org/2009/dap/camera/). – taco Feb 01 '13 at 18:14
  • @Travis M: It launches Androids camera. I don't know about iPhone, as I don't have access to one. – Jez D Jul 04 '13 at 17:36
  • 1
    @taco: [HTML Media Capture](https://w3c.github.io/html-media-capture/) is alive and well. – Dan Dascalescu Jul 27 '16 at 10:14
7
<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)
Pang
  • 9,564
  • 146
  • 81
  • 122
user3475960
  • 167
  • 2
  • 8
5

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.

Raydot
  • 1,458
  • 1
  • 23
  • 38
  • This is essentially the same answer as [the one provided by Jez D](http://stackoverflow.com/questions/9431475/html5-camera-access/12673480#12673480), using the [HTML Media Capture API](https://w3c.github.io/html-media-capture/). – Dan Dascalescu Jul 27 '16 at 10:13
  • 1
    Not sure why that requires a downvote. I just answered the question. Didn't research to make sure I was the only one who'd ever used that approach. But thanks for keeping me on the straight and narrow. – Raydot Aug 02 '16 at 16:28
  • No worries, it's just best practice to upvote or improve an existing answer, unless yours is significantly different. – Dan Dascalescu Aug 02 '16 at 19:39
  • 1
    So...then mark the question as duplicate, not the answer. I didn't get my answer from Jez D. Also the inclusion of the enctype and suggestion for generating unique file names is not part of his answer. I don't much care about being downvoted, but for a duplicate answer? From three years ago? That I've never heard of. – Raydot Aug 02 '16 at 19:47
  • 1
    Dave, your answer and Jez's are on this same question. I know your answer wasn't stolen, but see on meta about duplicate answers: [1](http://meta.stackoverflow.com/questions/258558/how-to-deal-with-a-stolen-answer/258835#258835) or [2](http://meta.stackoverflow.com/questions/258344/adding-answers-after-a-correct-answer-already-exists/258370#258370). Your answer was posted almost a year after an existing answer that gives the same solution. As to "three years ago", I've just noticed now the quasi-duplicate answers because now is when I looked at the question. Its age is irrelevant. – Dan Dascalescu Aug 02 '16 at 19:52
3

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/

OrangeKing89
  • 694
  • 6
  • 16
  • `bridgeit.camera('id', 'callback', {postURL:'url'})` invokes the camera function of the BridgeIt App with an image data-url thumbnail returned in event.preview passed to callback and the photo uploaded to the specified url. – Ted Goddard Jan 24 '14 at 23:07
  • @OrangeKing89 the link is down and shows some random ads. – Erwol Dec 05 '19 at 21:44
3

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.

Parth Choudhary
  • 468
  • 4
  • 5
3

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/

Roger
  • 15,793
  • 4
  • 51
  • 73
  • Thanks all. It seems that Opera 12 does not support using Camera. Is there any way to access camera by using HTML5? – love sunset Feb 24 '12 at 20:07
1

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

Hasanavi
  • 8,455
  • 2
  • 29
  • 35
-1

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.

Gunhan
  • 6,807
  • 3
  • 43
  • 37
  • If you're giving negative score to 5 years old answer, you should explain what's wrong about the answer. Obviously the answer was given at back then and working. – Gunhan May 23 '18 at 08:19