GitXplorerGitXplorer
j

photographer.js

public
8 stars
4 forks
2 issues

Commits

List of commits on branch master.
Unverified
29ffaa677f26427b922abc6d91a285a1d1d91f04

Update README.

jjharding committed 12 years ago
Unverified
2748aed085c946deaa90b5e51e8491f6afbf597d

Update README.

jjharding committed 12 years ago
Unverified
2c69c97f9b1397c18930e6738a9d1cd8bfb3d510

Fix bug in test suite.

jjharding committed 12 years ago
Unverified
db1b42efb606d12c1a3c43825e8f0fae813bd67a

Fix typo and replace Bugs and Limitations section with Browser Support.

jjharding committed 12 years ago
Unverified
f6c8d61159f97a750d2db036e1a745d8dff0c68b

Add download links to README.

jjharding committed 12 years ago
Unverified
a164a8a9568622e9dcb006af6b6c77e7f3a746e0

Version bump.

jjharding committed 12 years ago

README

The README file for this repository.

Photographer.js

HTML5 Apps Shouldn't Require Flash

For a long time (too long), Flash was required if a web app wanted to capture images through a user's webcam. Things are changing though and bleeding edge browsers now allow access to media devices (video and audio input) through the WebRTC API. Photographer.js is a library built to help abstract away the WebRTC API and make capturing photos in a web app dead simple.

Downloads

API

Photographer(config)

  var photographer = new Photographer({ 
    container: element
  });

The constructor function. It takes one argument, an object containing configuration details. There are 5 configuration properties:

  • container(DOM element, required) - The DOM element the Photographer instance will display the live preview video. If this property isn't provided, an error will be thrown.

  • flash(function, optional, null by default) - Before a Photographer instance captures an image, it'll call this function if provided. flash will have one argument passed to it, the container element.

  • imgFormat(string, optional, png by default) - The preferred image format of the captured images.

  • imgWidth(number, optional, width of container by default) - The preferred width of the capture images.

  • imgHeight(number, optional, height of container by default) - The preferred height of the capture images.

Photographer#start()

  photographer.start();

If the browser supports navigator.getUserMedia, this method will return true and start piping the video stream from the user's camera device to the live preview video. If the browser doesn't support navigator.getUserMedia, false will be returned. This method will also throw an error if navigator.getUserMedia results in an error.

Photographer#stop()

  photographer.stop();

If the browser supports navigator.getUserMedia, this method will stop the video stream from the user's camera device and return true. If the browser doesn't support navigator.getUserMedia, false will be returned.

Photographer#takePhoto()

  var photo = photographer.takePhoto();

If the browser supports navigator.getUserMedia and calling photographer.start() was successful, photographer.takePhoto() will capture an image and return a photo object (see below for more info on the photo object). If a flash function was configured, that function will get called right before the image is captured.

If the browser doesn't support navigator.getUserMedia, this method will return false.

The photo object photographer.takePhoto returns contains the following properties:

  • src(string) - The base64 encoding of the captured image.

  • format(string) - The format the image was captured as.

  • width(number) - The width of the captured image.

  • height(number) - The height of the captured image.

Photographer#getPhotos()

  var photos = photographer.getPhotos();

Each Photographer instance stores an array of all of the photos it has taken. photographer.getPhotos() will return a copy of that array.

Example Usage

<html>
  <head>
    <!-- load Photographer.js -->
    <script src='photographer.js'></script>
  </head>
  <body>
    <!-- this element will contain the video element Photographer.js
         pipes the webcam video stream to -->
    <div id='photobooth' style='width: 640px; height: 480px;'></div>

    <!-- when this button is clicked, a photo will be captured -->
    <button id='camera-click'>Take Photo</button>

    <script>
      // DOM references
      var body = document.getElementsByTagName('body')[0];
      var photobooth = document.getElementById('photobooth');
      var cameraClickBtn = document.getElementById('camera-click');

      // this function will get called right before Photographer.js
      // captures a photo. you can use it to provide a custom visual
      // cue to the user that their photo is about to be taken
      var flash = function(container) {
        alert('Say cheese!');
      };

      // create a Photographer instance  
      var photographer = new Photographer({
        flash: flash,
        container: photobooth
      });

      photographer.start();

      // when the user clicks on the 'Take Photo' button, capture their
      // image and append that image to the body
      cameraClickBtn.onclick = function() {
        var photo = photographer.takePhoto();

        // create the img element to be appended
        var img = new Image();
        img.src = photo.src;
        img.width = photo.width;
        img.height = photo.height;

        // add the image to the body
        body.appendChild(img);
      };
    </script>
  </body>
</html>

Supported Browsers

  • Opera 12+
  • Chrome
  • Firefox Nightly

If all goes to plan, full WebRTC support should be available in Firefox 18.

License

Copyright (c) 2012 Jake Harding
Licensed under the MIT license.