Camera and Video Control with HTML5

Written by David Walsh on November 7, 2012
Browser Camera

Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API, providing developers access to the user's camera.  Let me show you how to get simple camera access from within your browser!

The HTML

Please read my note about the HTML structure below:

<!--
	Ideally these elements aren't created until it's confirmed that the 
	client supports video/camera, but for the sake of illustrating the 
	elements involved, they are created with markup (not JavaScript)
-->
<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Snap Photo</button>
<canvas id="canvas" width="640" height="480"></canvas>

Each of these elements should be created once confirmation of camera support is confirmed, but for the sake of this tutorial, I wanted to show you what the elements look like with basic HTML.  Do note that the dimensions we're working with are 640x480.

The JavaScript

Since the HTML elements above are already created, the JavaScript portion will look smaller than you think:

// Put event listeners into place
window.addEventListener("DOMContentLoaded", function() {
	// Grab elements, create settings, etc.
	var canvas = document.getElementById("canvas"),
		context = canvas.getContext("2d"),
		video = document.getElementById("video"),
		videoObj = { "video": true },
		errBack = function(error) {
			console.log("Video capture error: ", error.code); 
		};

	// Put video listeners into place
	if(navigator.getUserMedia) { // Standard
		navigator.getUserMedia(videoObj, function(stream) {
			video.src = stream;
			video.play();
		}, errBack);
	} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
		navigator.webkitGetUserMedia(videoObj, function(stream){
			video.src = window.webkitURL.createObjectURL(stream);
			video.play();
		}, errBack);
	}
}, false);

Once it's been established that the browser supports getUserMedia, a simple method sets the video element's src to the user's live camera/webcam.  Calling the play method of the video then enacts the element's live video connection.  That's all that's required to connect your camera to the browser!

Taking a photo is only marginally more difficult.  Simply add a click listener to a generic button and and draw an image from video!

// Trigger photo take
document.getElementById("snap").addEventListener("click", function() {
	context.drawImage(video, 0, 0, 640, 480);
});

Of course you could add some sexy image filters and make a billion dollars...but I'll save that for another post.  At minimum you could convert the canvas snapshot to an image though!  I'll talk about canvas image filters in the future...

Being able to access the camera within the browser without using third party software is an incredible advancement.  Paired with canvas and a bit of JavaScript, the camera has become quickly and easily accessible.  Not only it the camera accessible, but since canvas is ultra-flexible, we'll be able to add sexy Instagram-style image filters in the future.  For now, however, simply accessing the camera in our browser moves us miles ahead.  Have fun taking images within your browser!

Post inspired by I see you getUserMedia; provided a great starting point for this post.

Comments

  1. My friend, you are an inexhaustible source of source code, all more useful to each other! I follow you for a long time and I appreciate all the effort you put into your research and development! I say a big thank you!

  2. Hi Daivd,

    This is really nice. we dont need to use flash anymore for this.
    Thanks for your effort.

  3. Hi Daivd, This is really nice. we dont need to use flash anymore for this.
    Thanks for your effort.

  4. Hi Daivd, This is really nice. we dont need to use flash anymore for this.
    Thanks for your effort.

  5. It works with Chrome Version 23.0.1271.64 as well, no need for Canary.

  6. and Opera 12.10. Thank you, David, for this

  7. Chrome 22.0.1229.94 m and Opera 12.10. Thank you, David, for this post.

  8. Can we add other feature liks adding some butter to apply the effects (color filter, fish-eye lens effect) to the snap photo?

  9. can you demo this exactly with the audio element instead of video?
    i’m trying to :
    1 – access user’s microphone
    2 – record something
    3 – upon completion of audio recording have it display (with controls) under the recording area
    4 – and here’s the hard part – record again (and again) and have them show up under each other with the ability to toggle/play/mute each track
    5 – voila! an HTML5 audio multitracker!
    6 – i understand that getting them to play in sync will require some work – but – a man can dream…!

  10. Hi David, I want to know if it’s possible to access an external camera – i.e a broadcast quality camera? I’m looking to maybe develop some interactive products for live events, using broadcast camera(s) with maybe HTML5 /Canvas. Would be great to get your advice on this.

    Paul.

Be Heard

Tip: Wrap your code in <code> tags...and don't flame others.

Use Code Editor
Older
Square Search Boxes in WebKit
Newer
5 More HTML5 APIs You Didn’t Know Existed