Class: Snapshot

Defined in: src/snapshot.coffee

Overview

Snapshot taken using JpegCamera.

Instance Method Summary

Instance Method Details

# (Snapshot) show()

Display the snapshot with the camera element it was taken with.

Returns:

# (Snapshot) hide()

Stop displaying the snapshot and return to showing live camera stream.

Ignored if camera is displaying different snapshot.

Returns:

# (void) get_stats(callback)

Calculate snapshot pixel statistics (mean gray value, std).

Because reading image data can take a while when Flash fallback is being used this method does not return the data immediately. Instead it accepts a callback that later will be called with a Stats object as an argument. Snapshot will be available as this.

Parameters:

  • callback ( Function ) Function to call when data is available. Snapshot object will be available as this, the Stats instance will be passed as the first argument.

Returns:

  • void

# (Boolean) get_canvas(callback)

Get canvas element showing the snapshot.

This can be used to display the snapshot outside the camera's container. You can show multiple snapshots at a time and allow the user to pick one he likes best.

Canvas produced by this method has a resolution of the snapshot (which depends on the camera's native resolution), not that of the camera's container. Use CSS to display this canvas in different sizes.

Because reading image data can take a while when Flash fallback is being used this method does not return the canvas element immediately. Instead it accepts a callback that later will be called with the canvas element as an argument. Snapshot will be available as this.

Multiple calls to this method will yield the same canvas element.

One caveat is that the underlaying data of this canvas is not mirrored like the stream shown in the camera container. Special CSS transform directive is applied on it so that it looks like the picture in the camera when displayed. This only matters when manipulating the canvas or reading it's data. You can read more about mirroring in JpegCamera#capture.

This method doesn't work in Internet Explorer 8 or earlier, because it does not support canvas element. Call JpegCamera.canvas_supported to learn whether you can use this method.

Parameters:

  • callback ( Function ) Function to call when canvas element is available. Snapshot object will be available as this, the canvas element will be passed as the first argument.

Returns:

  • ( Boolean ) — Whether canvas is supported in this browser.

# (Boolean) get_blob(callback, mime_type = "image/jpeg")

Get the file that would be uploaded to the server as a Blob object.

This can be useful if you want to stream the data via a websocket. Note that using upload is more efficient if all you want to do is upload this file to a server via POST call.

This method doesn't work in Internet Explorer 8 or earlier, because it does not support canvas element. Call JpegCamera.canvas_supported to learn whether you can use this method.

Because preparing image blob can take a while this method does not return the data immediately. Instead it accepts a callback that later will be called with the data object as an argument. Snapshot will be available as this.

Multiple calls to this method will yield the same data object.

Parameters:

  • callback ( Function ) Function to call when data is available. Snapshot object will be available as this, the blob object will be passed as the first argument.
  • mime_type ( String ) Mime type of the requested blob. "image/jpeg" by default.

Returns:

  • ( Boolean ) — Whether canvas is supported in this browser.

# (void) get_image_data(callback)

Get ImageData object containing color values for each pixel of the snapshot.

Data produced by this method has a resolution of the snapshot (which depends on the camera's native resolution), not that of the camera's container.

Read more about ImageData object on Mozilla's website .

Because reading image data can take a while when Flash fallback is being used this method does not return the data immediately. Instead it accepts a callback that later will be called with the data object as an argument. Snapshot will be available as this.

Multiple calls to this method will yield the same data object.

One caveat is that the returned data is not mirrored like the stream shown in the camera container. This only matters when manipulating the canvas or reading it's data. You can read more about mirroring in JpegCamera#capture.

This method returns native ImageData object in all browsers except Internet Explorer 8 or earlier which does not support the canvas element. In that browser a generic JavaScript object will be returned that mimics the native format. Call JpegCamera.canvas_supported to learn whether canvas is supported by the browser.

Parameters:

  • callback ( Function ) Function to call when data is available. Snapshot object will be available as this, the data will be passed as the first argument.

Returns:

  • void

# (Snapshot) upload(options = {})

Upload the snapshot to the server.

The snapshot is uploaded using a POST request with JPEG file sent as RAW data. This not like a multipart form upload using file element where the file is given a name and is encoded along with other form keys. To read file contents on the server side use request.raw_post in Ruby on Rails or $HTTP_RAW_POST_DATA in PHP.

Upload completes successfully only if the server responds with status code

  1. Any other code will be handled via on_upload_fail callback. Your application is free to inspect the status code and response text in that handler to decide whether that response is acceptable or not.

You cannot have multiple uploads for one snapshot running at the same time, but you are free to start another upload after one succeeds or fails.

All of the options can have their defaults set when constructing camera object or calling JpegCamera#capture.

Options Hash: (options):

  • api_url ( String ) URL where the snapshots will be uploaded.
  • csrf_token ( String ) CSRF token to be sent in the X-CSRF-Token header during upload.
  • timeout ( Integer ) IGNORED (NOT IMPLEMENTED) The number of milliseconds a request can take before automatically being terminated. Default of 0 means there is no timeout.
  • on_upload_done ( Function ) Function to call when upload completes. Snapshot object will be available as this, response body will be passed as the first argument. Calling done before the upload exits will change the handler for this upload.
  • on_upload_fail ( Function ) Function to call when upload fails. Snapshot object will be available as this, response code will be passed as the first argument followed by error message and response body. Calling fail before the upload exits will change the handler for this upload.
  • retry_if ( Function ) Function to be called before any upload done/fail callbacks to decide if the upload should be retried. By default it's null and uploads are never retried. Inside the function snapshot object will be available as this and the arguments will be: status_code, error_message, response, retry. retry is a number incremented for each retry and starting with 1 when the upload finishes for the first time. If the function returns true or 0 then upload will be retried immediately. Number greater than 0 will delay the retry by that many milliseconds. Any other value will be treated as a decision not to retry the upload and one of the on_upload_done or on_upload_fail callbacks will be fired instead.
  • retry_success ( Boolean ) By default retry_if is not called for uploads that finish with a status code from the 2XX range. Set this to true if you want to retry some of these responses. This can be useful if you're experiencing some network oddities.

Returns:

# (Snapshot) done(callback)

Bind callback for upload complete event.

The callback to fire when the previously requested upload operation succeeds. This is just a syntactic sugar that allows one to write: snapshot.upload().done(done_callback) instead of snapshot.upload(on_upload_done: done_callback). This callback will be forgotten after the next call to upload.

If the event has already happened the argument will be called immediately.

Parameters:

  • callback ( Function ) Function to call when upload completes. Snapshot object will be available as this, response body will be passed as the first argument.

Returns:

# (Snapshot) fail(callback)

Bind callback for upload error event.

The callback to fire when the previously requested upload operation fails. This is just a syntactic sugar that allows one to write: snapshot.upload().fail(fail_callback) instead of snapshot.upload(on_upload_fail: fail_callback). This callback will be forgotten after the next call to upload.

If the event has already happened the argument will be called immediately.

Parameters:

  • callback ( Function ) Function to call when upload fails. Snapshot object will be available as this, response code will be passed as the first argument with response body or error message as the second argument if available.

Returns:

# (void) discard()

Hide and discard this snapshot.

After discarding a snapshot an attempt to show or upload it will raise an error.

Returns:

  • void

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: