42

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>
3
  • 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
    Commented Feb 24, 2012 at 13:27
  • see here for a demo of HTML5 Microphone & Camera access in Chrome.. this is bleeding edge stuff.. youtube.com/…
    – Lloyd
    Commented Feb 24, 2012 at 14:49
  • HTML5 mobile support no longer "bleeding edge" - see stackoverflow.com/a/13489553/957950 for how to get images directly from the camera, or test example at mobilehtml5.org/ts/?id=23. Welcome to the future! Still no hoverboards, sorry. :(
    – brichins
    Commented Feb 18, 2016 at 17:21

9 Answers 9

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

2
  • I have installed chrome on my ipad but still does not open the video
    – J888
    Commented May 29, 2013 at 2: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
    Commented Jul 8, 2013 at 6:04
14

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

<input type="file" id="mypic" accept="image/*">
4
  • Does this invoke the camera on iPhones and Android phones?
    – Travis M.
    Commented Jan 24, 2013 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.
    – taco
    Commented Feb 1, 2013 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
    Commented Jul 4, 2013 at 17:36
  • 1
    @taco: HTML Media Capture is alive and well. Commented Jul 27, 2016 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)
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.

5
  • This is essentially the same answer as the one provided by Jez D, using the HTML Media Capture API. Commented Jul 27, 2016 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
    Commented Aug 2, 2016 at 16:28
  • No worries, it's just best practice to upvote or improve an existing answer, unless yours is significantly different. Commented Aug 2, 2016 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
    Commented Aug 2, 2016 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 or 2. 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. Commented Aug 2, 2016 at 19:52
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/

1
  • Thanks all. It seems that Opera 12 does not support using Camera. Is there any way to access camera by using HTML5? Commented Feb 24, 2012 at 20:07
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/

2
  • 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. Commented Jan 24, 2014 at 23:07
  • @OrangeKing89 the link is down and shows some random ads.
    – ewulff
    Commented Dec 5, 2019 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.

1

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

1
  • Which settings do i need to set in Chrome mobile? Commented Jan 4, 2013 at 9:20
0

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.

1
  • 1
    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
    Commented May 23, 2018 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.