|
1 |
| -webcamjs |
2 |
| -======== |
| 1 | +# WebcamJS |
3 | 2 |
|
4 |
| -HTML5 Webcam Image Capture Library with Flash Fallback |
| 3 | +## HTML5 Webcam Image Capture Library with Flash Fallback |
| 4 | + |
| 5 | +WebcamJS is a small (~2K minified and gzipped) standalone JavaScript library for capturing still images from your computer's camera, and delivering them to you as JPEG or PNG [http://en.wikipedia.org/wiki/Data_URI_scheme](Data URIs). The images can then be displayed in your web page, rendered into a canvas, or submitted to your server via a standard form. WebcamJS uses [http://dev.w3.org/2011/webrtc/editor/getusermedia.html](HTML5 getUserMedia), but provides an automatic and invisible Flash fallback. |
| 6 | + |
| 7 | +WebcamJS is based on my old [https://code.google.com/p/jpegcam/](JPEGCam) project, but has been redesigned for the modern web. Instead of relying on Flash and only being able to submit images directly to a server, WebcamJS delivers your images as client-side Data URIs, and it uses HTML5 getUserMedia where available. Flash is only used if your browser doesn't support getUserMedia, and the fallback is handled automatically. |
| 8 | + |
| 9 | +[http://pixlcore.com/demos/webcamjs/basic.html](Check out a live demo here!) |
| 10 | + |
| 11 | +## QuickStart Guide |
| 12 | + |
| 13 | +Host the `webcam.js` and `webcam.swf` files on your web server, and drop in this HTML snippet: |
| 14 | + |
| 15 | +```html |
| 16 | + <script src="webcam.js"></script> |
| 17 | + |
| 18 | + <div id="my_camera" style="width:320px; height:240px;"></div> |
| 19 | + <div id="my_result"></div> |
| 20 | + |
| 21 | + <script language="JavaScript"> |
| 22 | + Webcam.attach( '#my_camera' ); |
| 23 | + |
| 24 | + function take_snapshot() { |
| 25 | + var data_uri = Webcam.snap(); |
| 26 | + document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>'; |
| 27 | + } |
| 28 | + </script> |
| 29 | + |
| 30 | + <a href="javascript:void(take_snapshot())">Take Snapshot</a> |
| 31 | +``` |
| 32 | + |
| 33 | +This will create a live camera view in the `my_camera` DIV, and when the **Take Snapshot** link is clicked it will take a still snapshot, convert it to a JPEG, and deliver a Data URI which is inserted into the `my_result` DIV as a standard `<IMG SRC>` tag. |
| 34 | + |
| 35 | +Data URIs may be passed around like any URL, and can be submitted to your server by stuffing them into a form field (see below for example of this). |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +If you want to override the default settings, just call `Webcam.set()` and pass in a hash with any of the following keys: |
| 40 | + |
| 41 | +| Param Name | Default Value | Notes | |
| 42 | +-------------------------------------- |
| 43 | +| `width` | (Auto) | Width of the live camera viewer in pixels, defaults to the actual size of the DOM element. | |
| 44 | +| `height` | (Auto) | Height of the live camera viewer in pixels, defaults to the actual size of the DOM element. | |
| 45 | +| `dest_width` | (Auto) | Width of the captured camera image in pixels, defaults to the live viewer size. | |
| 46 | +| `dest_height` | (Auto) | Height of the captured camera image in pixels, defaults to the live viewer size. | |
| 47 | +| `image_format` | jpeg | Desired image format of captured image, may be "jpeg" or "png". | |
| 48 | +| `jpeg_quality` | 90 | For JPEG images, this is the desired quality, from 0 (worst) to 100 (best). | |
| 49 | + |
| 50 | +Here is an example of overriding some parameters. Remember to call this *before* you attach the viewer. |
| 51 | + |
| 52 | +```javascript |
| 53 | + Webcam.set({ |
| 54 | + width: 320, |
| 55 | + height: 240, |
| 56 | + dest_width: 640, |
| 57 | + dest_height: 480, |
| 58 | + image_format: 'jpeg', |
| 59 | + jpeg_quality: 90 |
| 60 | + }); |
| 61 | + |
| 62 | + // Attach camera here |
| 63 | +``` |
| 64 | + |
| 65 | +## Initialization |
| 66 | + |
| 67 | +WebcamJS is initialized and activated by "attaching" a live camera viewer to a DOM element. The DOM element must already be created and empty. Pass in a ID or CSS selector to the `Webcam.attach()` function. Example: |
| 68 | + |
| 69 | +```javascript |
| 70 | + Webcam.attach( '#my_camera' ); |
| 71 | +``` |
| 72 | + |
| 73 | +This will activate the user's webcam, ask for the appropriate permission, and begin showing a live camera image in the specified DOM element. |
| 74 | + |
| 75 | +## Snapping a Picture |
| 76 | + |
| 77 | +To snap a picture, just call the `Webcam.snap()` function. The image data will be returned as a Data URI, which you can then display in your web page, or submit to a server. Example: |
| 78 | + |
| 79 | +```javascript |
| 80 | + var data_uri = Webcam.snap(); |
| 81 | + |
| 82 | + document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>'; |
| 83 | +``` |
| 84 | + |
| 85 | +## Customizing Image Size |
| 86 | + |
| 87 | +WebcamJS will automatically size the live camera viewer based on the DOM element it is attached to. However, you can override this by setting the `width` and/or `height` parameters: |
| 88 | + |
| 89 | +```javascript |
| 90 | + Webcam.set({ |
| 91 | + width: 320, |
| 92 | + height: 240 |
| 93 | + }); |
| 94 | + |
| 95 | + // Attach camera here |
| 96 | +``` |
| 97 | + |
| 98 | +The size of the captured JPEG / PNG image is set to match the live camera viewer by default. However, you can override this by setting the `dest_width` and/or `dest_height`. Note that you can set the destination image size different than the viewer size. So you can have a small live viewer, but capture a large image. Example: |
| 99 | + |
| 100 | +```javascript |
| 101 | + Webcam.set({ |
| 102 | + width: 320, |
| 103 | + height: 240, |
| 104 | + dest_width: 640, |
| 105 | + dest_height: 480, |
| 106 | + }); |
| 107 | + |
| 108 | + // Attach camera here |
| 109 | +``` |
| 110 | + |
| 111 | +## Setting an Alternate SWF Location |
| 112 | + |
| 113 | +By default WebcamJS looks for the SWF file in the same directory as the JS file. If you are hosting the SWF in a different location, please set it using the `Webcam.setSWFLocation()` function. Example: |
| 114 | + |
| 115 | +```javascript |
| 116 | + Webcam.setSWFLocation("/path/to/the/webcam.swf"); |
| 117 | +``` |
| 118 | + |
| 119 | +## Reset (Shutdown) |
| 120 | + |
| 121 | +To shut down the live camera preview and reset the system, call `Webcam.reset()`. This removes any DOM elements we added, including a Flash movie if applicable, and resets everything in the library to the initial state. Example: |
| 122 | + |
| 123 | +```javascript |
| 124 | + Webcam.reset(); |
| 125 | +``` |
| 126 | + |
| 127 | +To use the library again after resetting, you must call `Webcam.attach()` and pass it your DOM element. |
| 128 | + |
| 129 | +## Custom Events |
| 130 | + |
| 131 | +WebcamJS fires a number of events you can intercept using a JavaScript hook system. Events are fired when the library is fully loaded, when the camera is live, and when an error occurs. To register an event listener, call the `Webcam.on()` function, passing an event name and callback function. Here is a table of the available event types: |
| 132 | + |
| 133 | +| Event Name | Notes | |
| 134 | +-------------------------------------- |
| 135 | +| `load` | Fires when the library finishes loading. | |
| 136 | +| `live` | Fires when the user's camera goes live (i.e. showing a live preview). | |
| 137 | +| `error` | Fires when an error occurs (your callback function is passed an error string). | |
| 138 | + |
| 139 | +Example: |
| 140 | + |
| 141 | +```javascript |
| 142 | + Webcam.on( 'load', function() { |
| 143 | + // library is loaded |
| 144 | + } ); |
| 145 | + |
| 146 | + Webcam.on( 'live', function() { |
| 147 | + // camera is live, showing preview image |
| 148 | + } ); |
| 149 | + |
| 150 | + Webcam.on( 'error', function(err) { |
| 151 | + // an error occurred (see 'err') |
| 152 | + } ); |
| 153 | +``` |
| 154 | + |
| 155 | +By default the `error` event shows a JavaScript alert dialog, but if you register your own event handler this action is suppressed, and your function is called instead. |
| 156 | + |
| 157 | +## Submitting Images to a Server |
| 158 | + |
| 159 | +WebcamJS delivers your images by way of a client-side JavaScript Data URI. The binary image data is encoded with Base64 and stuffed into the URI. It is up to you to send this data to your server and decode it. There are many ways to do this, but here is the easiest. |
| 160 | + |
| 161 | +First, add a hidden text element to a form: |
| 162 | + |
| 163 | +```html |
| 164 | + <form id="myform" method="post" action="myscript.php"> |
| 165 | + <input id="mydata" type="hidden" name="mydata" value=""/> |
| 166 | + </form> |
| 167 | +``` |
| 168 | + |
| 169 | +Then, when you snap your picture, stuff the Data URI into the form field value (minus the header), and submit the form: |
| 170 | + |
| 171 | +```javascript |
| 172 | + var data_uri = Webcam.snap(); |
| 173 | + var raw_image_data = data_uri.replace(/^data\:image\/\w+\;base64\,/, ''); |
| 174 | + |
| 175 | + document.querySelector('#mydata').value = raw_image_data; |
| 176 | + document.querySelector('#myform').submit(); |
| 177 | +``` |
| 178 | + |
| 179 | +Finally, on your server, grab the form data as if it were a plain text field, decode the Base64, and you have your binary image file. Example here in PHP, assumes JPEG format: |
| 180 | + |
| 181 | +```php |
| 182 | + $encoded_data = $_POST['mydata']; |
| 183 | + $binary_data = base64_decode( $encoded_data ); |
| 184 | + |
| 185 | + // save to server (beware of permissions) |
| 186 | + $result = file_put_contents( 'webcam.jpg', $binary_data ); |
| 187 | + if (!$result) die("Could not save image! Check file permissions."); |
| 188 | +``` |
| 189 | + |
| 190 | +## License |
| 191 | + |
| 192 | +The MIT License (MIT) |
| 193 | + |
| 194 | +Copyright (c) 2012 - 2014 Joseph Huckaby |
| 195 | + |
| 196 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 197 | +this software and associated documentation files (the "Software"), to deal in |
| 198 | +the Software without restriction, including without limitation the rights to |
| 199 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 200 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 201 | +subject to the following conditions: |
| 202 | + |
| 203 | +The above copyright notice and this permission notice shall be included in all |
| 204 | +copies or substantial portions of the Software. |
| 205 | + |
| 206 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 207 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 208 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 209 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 210 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 211 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments