Module: Camera

Methods


<static> cleanup()

Removes intermediate image files that are kept in temporary storage
after calling camera.getPicture. Applies only when the value of
Camera.sourceType equals Camera.PictureSourceType.CAMERA and the
Camera.destinationType equals Camera.DestinationType.FILE_URI.

Supported Platforms

  • iOS
Source:
Example
navigator.camera.cleanup(onSuccess, onFail);

function onSuccess() {
    console.log("Camera cleanup success.")
}

function onFail(message) {
    alert('Failed because: ' + message);
}

<static> getPicture(successCallback, errorCallback, options)

Takes a photo using the camera, or retrieves a photo from the device's
image gallery. The image is passed to the success callback as a
Base64-encoded String, or as the URI for the image file.

The camera.getPicture function opens the device's default camera
application that allows users to snap pictures by default - this behavior occurs,
when Camera.sourceType equals Camera.PictureSourceType.CAMERA.
Once the user snaps the photo, the camera application closes and the application is restored.

If Camera.sourceType is Camera.PictureSourceType.PHOTOLIBRARY or
Camera.PictureSourceType.SAVEDPHOTOALBUM, then a dialog displays
that allows users to select an existing image.

The return value is sent to the cameraSuccess callback function, in
one of the following formats, depending on the specified
cameraOptions:

  • A String containing the Base64-encoded photo image.
  • A String representing the image file location on local storage (default).

You can do whatever you want with the encoded image or URI, for
example:

  • Render the image in an <img> tag, as in the example below
  • Save the data locally (LocalStorage, Lawnchair, etc.)
  • Post the data to a remote server

NOTE: Photo resolution on newer devices is quite good. Photos
selected from the device's gallery are not downscaled to a lower
quality, even if a quality parameter is specified. To avoid common
memory problems, set Camera.destinationType to FILE_URI rather
than DATA_URL.

Supported Platforms

  • Android
  • BlackBerry
  • Browser
  • Firefox
  • FireOS
  • iOS
  • Windows
  • WP8
  • Ubuntu

More examples here. Quirks here.

Parameters:
Name Type Description
successCallback module:Camera.onSuccess
errorCallback module:Camera.onError
options module:Camera.CameraOptions

CameraOptions

Source:
Example
navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);

Type Definitions


CameraOptions

Optional parameters to customize the camera settings.

Type:
  • Object
Properties:
Name Type Argument Default Description
quality number <optional>
50

Quality of the saved image, expressed as a range of 0-100, where 100 is typically full resolution with no loss from file compression. (Note that information about the camera's resolution is unavailable.)

destinationType module:Camera.DestinationType <optional>
FILE_URI

Choose the format of the return value.

sourceType module:Camera.PictureSourceType <optional>
CAMERA

Set the source of the picture.

allowEdit Boolean <optional>
false

Allow simple editing of image before selection.

encodingType module:Camera.EncodingType <optional>
JPEG

Choose the returned image file's encoding.

targetWidth number <optional>

Width in pixels to scale image. Must be used with targetHeight. Aspect ratio remains constant.

targetHeight number <optional>

Height in pixels to scale image. Must be used with targetWidth. Aspect ratio remains constant.

mediaType module:Camera.MediaType <optional>
PICTURE

Set the type of media to select from. Only works when PictureSourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM.

correctOrientation Boolean <optional>

Rotate the image to correct for the orientation of the device during capture.

saveToPhotoAlbum Boolean <optional>

Save the image to the photo album on the device after capture.

popoverOptions module:CameraPopoverOptions <optional>

iOS-only options that specify popover location in iPad.

cameraDirection module:Camera.Direction <optional>
BACK

Choose the camera to use (front- or back-facing).

Source:

onError(message)

Callback function that provides an error message.

Parameters:
Name Type Description
message string

The message is provided by the device's native code.

Source:

onSuccess(imageData)

Callback function that provides the image data.

Parameters:
Name Type Description
imageData string

Base64 encoding of the image data, or the image file URI, depending on cameraOptions in effect.

Source:
Example
// Show image
//
function cameraCallback(imageData) {
   var image = document.getElementById('myImage');
   image.src = "data:image/jpeg;base64," + imageData;
}