Camera API

The Camera API allows your app to access the device's camera features. It gives you the ability to capture a photo using the device camera and return it to the application. The photos can be stored on the device and can be returned as a data URI or file path.

For example, you can build an application that will capture a photo and save it to the file directory in the device. You can choose what you want to do with the encoded image or URI, for example:

  • Render the image in an <img> tag.
  • Save the image data locally.
  • Post the data to a remote server.

The Camera API is supported on Android, iOS, and Web platforms.

The following are some of the main functions of Camera API:

  • Allows the app to capture a photo. This image can be saved to the device image gallery.
  • Allows the app to select a photo from different storage location such as default image gallery, SD card storage on the device, or a specific image folder.
  • Allows the app to store the image to a specific storage location on the device.
  • Remove the captured photos from the device image cache in iOS platform.

Methods


ChoosePicture

Use this method to access the device storage location and choose an existing image file. This method returns either file path or data URI of the file.

Syntax

$m.choosePicture( success_callback, error_callback, options );

Parameters

success_callback: (function). The callback function that provides the image data.

error_callback: The callback function, in case of error, provides the error object.

options: (object) optional. Optional parameters to customize the camera settings and image.

Return

  • path: (String). The image file URI.
  • data: (String). Base64 encoded string of the image data.
  • message: (String). A short description of the error.
  • description: (String). A detailed description of the error.

The return value is sent to the success_callback function, in one of the following formats, depending on the specified options:

  • A String containing the base64-encoded image.
  • A String representing the image file location.

Example

var success_callback = function(res){ console.log(res); };
var error_callback = function(res){ console.log(res); };
var options = {
	sourceType: 0,
	encodingType: 1,
	mediaType: 0,
	allowEdit: true,
	quality: 50,
	correctOrientation: true
};
$m.choosePicture( success_callback, error_callback, { quality: 50 }options )


GetPicture

Use this method to retrieve an image file from different types of storage location such as the device default image gallery, SD card storage, or any other specific image folder in the device. Also, you can use this method to get picture by capturing image through the device camera.

Syntax

$m.getPicture( success_callback, error_callback, options );

Parameters

success_callback: (function). The callback function that provides the image data.

error_callback: The callback function, in case of error, provides the error object.

options: (object) optional. Optional parameters to customize the camera settings and image.

Return

  • path: (String). The image file URI.
  • data: (String). Base64 encoded string of the image data.
  • message: (String). A short description of the error.
  • description: (String). A detailed description of the error.

The return value is sent to the success_callback function, in one of the following formats, depending on the specified options:

  • A String containing the base64-encoded photo image.
  • A String representing the image file location.

Example

var success_callback = function(res){ console.log(res); };
var error_callback = function(res){ console.log(res); };
var options = {
	sourceType: 0,
	encodingType: 1,
	mediaType: 0,
	quality: 25,
	destinationType: $m.FILE_URI
};
$m.getPicture( success_callback, error_callback, options);


CapturePicture

Use this method to launch the device camera app and capture a picture. The captured picture can be written to the default photos gallery.

Syntax

$m.capturePicture( success_callback, error_callback, options );

Parameters

success_callback: (function). The callback function to execute when the user has captured a picture using the camera app.

error_callback: The callback function, in case of error, provides the error object.

options: (object) optional. Optional parameters for the capture picture operation.

Return

  • path: (String). The image file URI.
  • data: (String). Base64 encoded string of the image data.
  • message: (String). A short description of the error.
  • description: (String). A detailed description of the error.

The return value is sent to the success_callback function, in one of the following formats, depending on the specified options:

  • A String containing the base64-encoded photo image.
  • A String representing the image file location.

Example

var success_callback = function(res){ console.log(res); }; 
var error_callback = function(res){ console.log(res); };
var options = {
	encodingType: 1,
	targetHeight: 100,
	targetWidth: 100,
	quality: 50,
	destinationType: $m.FILE_URI
};
$m.capturePicture( success_callback, error_callback,options );


Options: Values

The following table provides the optional parameters and valid values that can be specified in the syntax.

Property Datatype Description
quality  Number 

Quality of the image when returned, expressed as a range of 0-100, where 100 is typically full resolution with no loss from file compression. This quality value denotes the compression rate of the captured picture. Higher the quality higher the time taken to process the picture and the memory usage.

destinationType  Number 

Choose the format of the return value.

0 - Data URL

1 - File URI

2 - Native URI

sourceType  Number 

Set the source of the picture.

0 - Gallery

1 - Camera

2 - Select photos that are taken only by the device Camera.

allowEdit  Boolean  If true, the user is allowed to edit the picture after capturing it in the device camera app. The default value is false.
encodingType  Number 

Choose the returned image file's encoding.

0 - JPEG

1 - PNG

targetWidth  Number 

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

The width is applied before the captured or selected picture is written to the app or photos gallery.

targetHeight  Number 

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

The height is applied before the captured picture is written to the app or photos gallery.

mediaType  Number 

Set the type of media to select from.

0 - Picture

1 - Video

2 - All Media

correctOrientation  Boolean  Rotate the image to correct for the orientation of the device during capture.
saveToPhotoAlbum  Boolean  Save the image to the photo album on the device after capture.
popoverOptions  Number  iOS-only option that can specify popover location in iPad.

cameraDirection 

Number 

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