Class: Snapshot
Defined in: | src/snapshot.coffee |
Overview
Snapshot taken using JpegCamera.
Instance Method Summary
- # (Snapshot) show() Display the snapshot with the camera element it was taken with.
- # (Snapshot) hide() Stop displaying the snapshot and return to showing live camera stream.
- # (void) get_stats(callback) Calculate snapshot pixel statistics (mean gray value, std).
- # (Boolean) get_canvas(callback) Get canvas element showing the snapshot.
- # (Boolean) get_blob(callback, mime_type = "image/jpeg") Get the file that would be uploaded to the server as a Blob object.
- # (void) get_image_data(callback) Get ImageData object containing color values for each pixel of the snapshot.
- # (Snapshot) upload(options = {}) Upload the snapshot to the server.
- # (Snapshot) done(callback) Bind callback for upload complete event.
- # (Snapshot) fail(callback) Bind callback for upload error event.
- # (void) discard() Hide and discard this snapshot.
Instance Method Details
#
(Snapshot)
show()
Display the snapshot with the camera element it was taken with.
#
(Snapshot)
hide()
Stop displaying the snapshot and return to showing live camera stream.
Ignored if camera is displaying different snapshot.
#
(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
.
#
(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.
#
(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.
#
(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.
#
(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
- 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.
#
(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.
#
(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.
#
(void)
discard()
Hide and discard this snapshot.
After discarding a snapshot an attempt to show or upload it will raise an error.