Copyright © 2010 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document represents the early consensus of the group on the scope and features of the proposed Capture API. Issues and editors notes in the document highlight some of the points on which the group is still working and would particularly like to get feedback.
This document was published by the Device APIs and Policy Working Group as a First Public Working Draft. This document is intended to become a W3C Recommendation. If you wish to make comments regarding this document, please send them to public-device-apis@w3.org (subscribe, archives). All feedback is welcome.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
Capture interfaceMediaFile interfaceFormatData interfaceCaptureCB interfaceCaptureErrorCB interfaceViewFinderCB interfaceViewFinderErrorCB interfaceCaptureError interfaceCaptureImageOptions interfaceCaptureVideoOptions interfaceCaptureAudioOptions interfacePendingOperation interfaceViewFinder interfaceThe Capture API defines a high-level interface for accessing the microphone and camera of a hosting device.
The following code extracts illustrate how to work with a camera service in the hosting device:
Launching a device camera application and retrieving the pictures taken.
function success(data) { var container = document.createElement("div"); document.body.appendChild(container); for (var i in data) { var img = document.createElement("img"); img.src = data[i].uri; container.appendChild(img); } } function error(err) { alert(err.message + " (" + err.code + ")"); } navigator.device.captureImage(success, error, { maxNumberOfMediaFiles: 1 });
Example of retrieving image sizes and formats supported by hosting device camera.
var summary, formats = navigator.device.supportedImageFormats; for (var key in formats) { summary += key + ": " + formats[key] + "\n"; } alert(summary);
This section is under development.
The API defined in this specification launches the capture application which allows the user to take pictures, record voice or record video and provides a handle to the content. This information can potentially compromise user privacy and a conforming implementation of this specification must provide a mechanism that protects the user's privacy and this mechanism should ensure that such operations must be authenticated.
A conforming implementation of this specification must provide a mechanism that protects the user's privacy and this mechanism should ensure that privacy information is not revealed without user's informed consent.
Capture interfaceThe Capture interface exposes an interface to the
      camera and microphone of the hosting device.
Objects implementing the NavigatorDevice
      interface (e.g. the window.navigator.device object
      in Web browsers [NAVIGATOR]) provide access to the
      Capture interface through the Capture
      interface. An instance of Capture would be then
      obtained by using binding-specific casting methods on an
      instance of NavigatorDevice.
[Supplemental, NoInterfaceObject]
interface Capture {
    readonly attribute FormatData[] supportedImageFormats;
    readonly attribute FormatData[] supportedVideoFormats;
    readonly attribute FormatData[] supportedAudioFormats;
    PendingOperation captureImage (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureImageOptions options);
    PendingOperation captureVideo (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureVideoOptions options);
    PendingOperation captureAudio (in CaptureCB successCB, in optional CaptureErrorCB? errorCB, in optional CaptureAudioOptions options);
    PendingOperation getViewFinder (in ViewFinderCB successCB, in optional ViewFinderErrorCB? errorCB);
};
supportedAudioFormats of type array of FormatData, readonlysupportedImageFormats of type array of FormatData, readonlysupportedVideoFormats of type array of FormatData, readonlycaptureAudioLaunch device native audio recorder application for recording audio clip(s).
This method takes two or three arguments. When called, it
      immediately returns a 
      PendingOperation object and then asynchronously
      start a capture audio process defined as follows:
successCB
      with a  MediaArray
      argument. If the attempt fails, and the method was invoked with a
      non-null errorCallback argument, this method must invoke
      the errorCallback with a CaptureError object as
      an argument.CaptureAudioOptions parameter was present,
      and its maxNumberOfMediaFiles attribute was defined,
      successCB must be invoked after the user has
      captured a number of audio clips defined in
      maxNumberOfMediaFiles.  If user exited the native camera
      application prematurely, errorCB must be invoked.
      | Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| successCB |  | ✘ | ✘ | Function to call when the asynchronous operation completes | 
| errorCB |  | ✔ | ✔ | Function to call when the asynchronous operation fails. This parameter is optional. | 
| options |  | ✘ | ✔ | Audio capture options. This parameter is optional. | 
PendingOperationcaptureImageLaunch device native camera application for taking image(s).
This method takes two or three arguments. When called, it
      immediately returns a PendingOperation
      object and then asynchronously start a capture image
      process defined as follows:
successCB
      with a  MediaArray
      argument. If the attempt fails, and the method was invoked with a
      non-null errorCallback argument, this method must
      invoke the errorCallback with a CaptureError object
      as an argument.CaptureImageOptions parameter was present,
      and its maxNumberOfMediaFiles attribute was defined,
      successCB must be invoked after the user has
      captured a number of images defined in maxNumberOfMediaFiles.  If
      user exited the native camera application prematurely,
      errorCB must be invoked.| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| successCB |  | ✘ | ✘ | Function to call when the asynchronous operation completes | 
| errorCB |  | ✔ | ✔ | Function to call when the asynchronous operation fails. This parameter is optional. | 
| options |  | ✘ | ✔ | Image capture options. This parameter is optional. | 
PendingOperationcaptureVideoLaunch device native camera application for recording video(s).
This method takes three or four arguments. When called, it
      immediately returns a PendingOperation
      object and then asynchronously start a capture video process
      defined as follows:
successCB with a  MediaArray argument. If
      the attempt fails, and the method was invoked with a non-null
      errorCallback argument, this method must invoke the
      errorCallback with a CaptureError object as
      an argument.CaptureVideoOptions parameter was present, and
      its maxNumberOfMediaFiles attribute was defined,
      successCB must be invoked after the user has
      captured a number of video clips defined in
      maxNumberOfMediaFiles.  If user exited the native camera
      application prematurely, errorCB must be invoked.CaptureVideoOptions parameter was present,
      and its duration attribute was defined,
      successCB must be invoked after the video has
      been captured for the number of milliseconds defined in
      duration.  If user exited the native camera
      application prematurely, errorCB must be invoked.
      | Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| successCB |  | ✘ | ✘ | Function to call when the asynchronous operation completes | 
| errorCB |  | ✔ | ✔ | Function to call when the asynchronous operation fails. This parameter is optional. | 
| options |  | ✘ | ✔ | Image capture options. This parameter is optional. | 
PendingOperationgetViewFinderThis method takes one or two arguments. When called, it
      immediately returns a PendingOperation
      object and then tries asynchronously to return the ViewFinder object of the
      active capture device selected by the system or user.
User consent must be verified.
If successful, invokes the associated ViewFinderCB
      with a  ViewFinder
      argument. If the attempt fails, and the method was invoked with a
      non-null errorCallback argument, this method must invoke
      the errorCB with a CaptureError object as
      an argument.
| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| successCB |  | ✘ | ✘ | Function to call when the asynchronous operation completes | 
| errorCB |  | ✔ | ✔ | Function to call when the asynchronous operation fails. This parameter is optional. | 
PendingOperationMediaFile interfaceMediaFile captures a single photo, video or sound
    captured by the device native capture application. It inherits
    from File [FILE-API]
[NoInterfaceObject]
interface MediaFile : File {
    attribute FormatData format;
};
format of type FormatDataMediaFile
    object.FormatData interfaceFormatData captures format information of a media
    file captured by the device native capture application.
[NoInterfaceObject]
interface FormatData {
    attribute DOMString     type;
    attribute unsigned long height;
    attribute double        duration;
    attribute unsigned long width;
};
duration of type doubleheight of type unsigned longtype of type DOMStringwidth of type unsigned longCaptureCB interface[Callback=FunctionOnly, NoInterfaceObject]
interface CaptureCB {
    void onSuccess (in MediaArray capturedMedia);
};
onSuccess| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| capturedMedia | MediaArray | ✘ | ✘ | Sequence of MediaFile successfully captured by the device | 
voidThe MediaArray typedef represents a sequence of
     MediaFile
    objects. 
typedef sequence<MediaFile> MediaArray;MediaFile> type.CaptureErrorCB interface[Callback=FunctionOnly, NoInterfaceObject]
interface CaptureErrorCB {
    void onError (in CaptureError error);
};
onError| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| error |  | ✘ | ✘ | The error object of an unsuccessful capture asynchronous operation. | 
voidViewFinderCB interface[Callback=FunctionOnly, NoInterfaceObject]
interface ViewFinderCB {
    void onSuccess (in ViewFinder viewfinder);
};
onSuccess| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| viewfinder |  | ✘ | ✘ | The ViewFinder object of the selected capture device. | 
voidViewFinderErrorCB interface[Callback=FunctionOnly, NoInterfaceObject]
interface ViewFinderErrorCB {
    void onError (in CaptureError error);
};
onError| Parameter | Type | Nullable | Optional | Description | 
|---|---|---|---|---|
| error |  | ✘ | ✘ | The error object of an unsuccessful asynchronous viewfinder acquisition operation. | 
voidCaptureError interfaceThe CaptureError interface encapsulates all errors in the
    Capture API. 
More error codes to be defined here.
[NoInterfaceObject]
interface CaptureError {
    const unsigned short CAPTURE_INTERNAL_ERR = 0;
    readonly attribute unsigned short code;
};
code of type unsigned short, readonlyCAPTURE_INTERNAL_ERR of type unsigned shortCaptureImageOptions interfaceThe CaptureImageOptions interface encapsulates all image
    capture operation configuration options.
Additional attributes proposed: width
    and height.
[NoInterfaceObject]
interface CaptureImageOptions {
    attribute unsigned long maxNumberOfMediaFiles;
};
maxNumberOfMediaFiles of type unsigned longCaptureVideoOptions interface[NoInterfaceObject]
interface CaptureVideoOptions {
    attribute unsigned long maxNumberOfMediaFiles;
    attribute double        duration;
};
duration of type doublemaxNumberOfMediaFiles of type unsigned longCaptureAudioOptions interfaceAdditional attributes proposed: duration.
[NoInterfaceObject]
interface CaptureAudioOptions {
    attribute unsigned long maxNumberOfMediaFiles;
};
maxNumberOfMediaFiles of type unsigned longPendingOperation interfaceThis may be a general interface for use throughout all APIs. Included here for now for completion.
[NoInterfaceObject]
interface PendingOperation {
    void cancel ();
};
cancelvoidViewFinder interfaceThis interface represents a camera viewfinder. It inherits from
  File [FILE-API] and provides a method to embed a
  viewfinder to web content.
[NoInterfaceObject]
interface ViewFinder : File {
    void startCapture (in Blob data);
    void stopCapture ();
};
  In addition to programmable API, content can be acquired from capture devices through a file-select control.
This section is non-normative.
If input element in the File Upload state [HTML5] contains
    accept attribute with values image/*,
    sound/*, or video/*, the user agent can
    invoke a file picker that allows respectively the user to take a
    picture, record a sound file, or record a video in addition to
    selecting an existing file from the file system.
See the User Interface Examples appendix for the illustration.
In case the user chooses to capture video, audio, or image content, the user agent creates media files on the fly as specified in [HTML5].
<input id="cameraInput" type="file" accept="image/*">
In trusted environments the click event of file input element can be synthesised as follows:
<input id="cameraInput" onchange="successCB();" type="file" accept="image/*"> <script> function successCB() { // Read the image using file-reader or submit the form } document.getElementById('cameraInput').click(); </script>
Conforming user agents must provide the user a file picker from which it is possible to select any of the available capture devices in addition to possibility to select one or more files from the file system.
If a user selects camera device, the FileList object
that represents the currently selected files in the input element in
the File Upload state must contain one ViewFinder
object instead of File objects as in [FILE-API].
<script> function getVideo () { var inp = document.createElement("input"); inp.type = "file"; inp.accept = "video/*"; inp.onchange = function () { setupVideo(this.files[0]); }; // this is only possible in trusted environments // the click event cannot be synthetized by default inp.click(); } var blob; function setupVideo (vf) { document.getElementById("my-video").src = vf.URL; var bb = new BlobBuilder(); blob = bb.getBlob(); document.getElementById("start").onclick = function () { vf.startCapture(bb.getBlob()); } document.getElementById("stop").onclick = function () { vf.stopCapture(); } document.getElementById("send").onclick = function () { send(); } } function send () { if (!blob) { alert("You didn't capture anything!") return; } var xhr = new XMLHttpRequest(); xhr.open("GET", "http://my-server/video-upload"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) alert("Video uploaded!"); } xhr.send(blob); } </script> <button onclick='getVideo();'>Film your face!</button> <video id='my-video'></video> <button id='start'>Start</button> <button id='stop'>Stop</button> <button id='send'>Upload</button>
Capture API aware file picker might render as:

This is a list of features that have been discussed with respect to this version of the API but for which it has been decided that if they are included it will be in a future revision.
Many thanks to Google, Nokia, and OMTP BONDI who provided the initial input into this specification.
No normative references.