Camera Plugin
Tested Version: 4.0.3
This document is based on the original Cordova docs available at Cordova Docs.
This plugin defines a global navigator.camera
object, which provides an API for taking pictures and for choosing images from the system's image library.
Although the object is attached to the global scoped navigator
, it is not available until after the deviceready
event.
Plugin ID
Adding the Plugin in Monaca
In order to use this plugin, please enable Camera
plugin in Monaca Cloud IDE.
iOS Quirks
Since iOS 10 it's mandatory to provide usage description in the info.plist
if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will displayed as part of the permission dialog box, but if you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide usage description.
This plugins requires the following usage descriptions:
NSCameraUsageDescription
specifies the reason for your app to access the device's camera.NSPhotoLibraryUsageDescription
specifies the reason for your app to access the user's photo library.NSLocationWhenInUseUsageDescription
specifies the reason for your app to access the user's location information while your app is in use. (Set it if you have CameraUsesGeolocation preference set to true)NSPhotoLibraryAddUsageDescription
specifies the reason for your app to get write-only access to the user's photo library
To add these entries into the info.plist
, you can use the <edit-config>
tag in the config.xml
file like this:
API Reference
.onError : function
.onSuccess : function
.CameraOptions : Object
.DestinationType : enum
.EncodingType : enum
.MediaType : enum
.PictureSourceType : enum
.PopoverArrowDirection : enum
.Direction : enum
camera
camera.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 belowSave the data locally (
LocalStorage
, Lawnchair, etc.)Post the data to a remote server
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
iOS
More examples here. Quirks here.
Kind: static method of camera
Param | Type | Description |
| Function | |
| Function | |
| JSON Object |
Example
camera.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
Kind: static method of camera
Example
camera.onError : function
Callback function that provides an error message.
Kind: static typedef of camera
Param | Type | Description |
| String | The message is provided by the device's native code |
camera.onSuccess : function
Callback function that provides the image data.
Kind: static typedef of camera
Param | Type | Description |
| String | Base64 encoding of the image data, or the image file URI, depending on cameraOptions in effect. |
Example
camera.CameraOptions : Object
Optional parameters to customize the camera settings.
Kind: static typedef of camera
Properties
Name | Type | Default | Description |
| Number |
| Quality of the saved image, expressed as a range of |
|
| Choose the format of the return value. | |
|
| Set the source of the picture. | |
| Boolean |
| Allow simple editing of image before selection. |
|
| Choose the returned image file’s encoding. | |
| Number | Width in pixels to scale image. Must be used with | |
| Number | Height in pixels to scale image. Must be used with | |
|
| Set the type of media to select from. Only works when | |
| Boolean | Rotate the image to correct for the orientation of the device during capture. | |
| Boolean | Save the image to the photo album on the device after capture. | |
| iOS-only options that specify popover location in iPad. | ||
|
| Choose the camera to use (front- or back-facing). |
Camera
Camera.DestinationType : enum
Defines the output format of Camera.getPicture
call.
On iOSpassing DestinationType.NATIVE_URI
along with PictureSourceType.PHOTOLIBRARY
or PictureSourceType.SAVEDPHOTOALBUM
will disable any image modifications (resize, quality change, cropping, etc.) due to implementation specific.
Kind: static enum property of Camera
Properties
Name | Type | Default | Description |
| Number |
| Return base64 encoded string. |
| Number |
| Return file uri ( |
| Number |
| Return native uri (eg. |
Camera.EncodingType : enum
Kind: static enum property of Camera
Properties
Name | Type | Default | Description |
| Number |
| Return JPEG encoded image |
| Number |
| Return PNG encoded image |
Camera.MediaType : enum
Kind: static enum property of Camera
Properties
Name | Type | Default | Description |
| Number |
| Allow selection of still pictures only. DEFAULT. Will return format specified via |
| Number |
| Allow selection of video only, ONLY RETURNS URL |
| Number |
| Allow selection from all media types |
Camera.PictureSourceType : enum
Defines the output format of Camera.getPicture
call.
On iOS passing PictureSourceType.PHOTOLIBRARY
or PictureSourceType.SAVEDPHOTOALBUM
along with DestinationType.NATIVE_URI
will disable any image modifications (resize, quality change, cropping, etc.) due to implementation specific.
Kind: static enum property of Camera
Properties
Name | Type | Default | Description |
| Number |
| Choose image from the device’s photo library (same as |
| Number |
| Take picture from camera |
| Number |
| Choose image only from the device’s Camera Roll album (same as |
Camera.PopoverArrowDirection : enum
Matches iOS UIPopoverArrowDirection constants to specify arrow location on popover.
Kind: static enum property of Camera Properties
Name | Type | Default |
| Number |
|
| Number |
|
| Number |
|
| Number |
|
| Number |
|
Camera.Direction : enum
Kind: static enum property of camera
Properties
Name | Type | Default | Description |
| Number |
| Use the back-facing camera |
| Number |
| Use the front-facing camera |
CameraPopoverOptions
iOS-only parameters that specify the anchor element location and arrow direction of the popover when selecting images from an iPad's library or album. Note that the size of the popover may change to adjust to the direction of the arrow and orientation of the screen. Make sure to account for orientation changes when specifying the anchor element location.
Name | Type | Default | Description |
| Number |
|
|
| Number |
|
|
| Number |
| width, in pixels, of the screen element onto which to anchor the popover. |
| Number |
| height, in pixels, of the screen element onto which to anchor the popover. |
|
| Direction the arrow on the popover should point. |
CameraPopoverHandle
A handle to an image picker popover.
Supported Platforms
iOS
Example
camera.getPicture Errata
Example
Take a photo and retrieve the image's file location:
Take a photo and retrieve it as a Base64-encoded image:
Preferences (iOS)
CameraUsesGeolocation (boolean, defaults to false). For capturing JPEGs, set to true to get geolocation data in the EXIF header. This will trigger a request for geolocation permissions if set to true.
xml<preference name="CameraUsesGeolocation" value="false" />
Android Quirks
Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the result from the plugin call will be delivered via the resume event. See the Android Lifecycle guide for more information. The pendingResult.result
value will contain the value that would be passed to the callbacks (either the URI/URL or an error message). Check the pendingResult.pluginStatus
to determine whether or not the call was successful.
iOS Quirks
Including a JavaScript alert()
in either of the callback functions can cause problems. Wrap the alert within a setTimeout()
to allow the iOS image picker or popover to fully close before the alert displays:
CameraOptions Errata
Android Quirks
Any
cameraDirection
value results in a back-facing photo.allowEdit
is unpredictable on Android and it should not be used! The Android implementation of this plugin tries to find and use an application on the user's device to do image cropping. The plugin has no control over what application the user selects to perform the image cropping and it is very possible that the user could choose an incompatible option and cause the plugin to fail. This sometimes works because most devices come with an application that handles cropping in a way that is compatible with this plugin (Google Plus Photos), but it is unwise to rely on that being the case. If image editing is essential to your application, consider seeking a third party library or plugin that provides its own image editing utility for a more robust solution.Camera.PictureSourceType.PHOTOLIBRARY
andCamera.PictureSourceType.SAVEDPHOTOALBUM
both display the same photo album.Ignores the
encodingType
parameter if the image is unedited (i.e.quality
is 100,correctOrientation
is false, and notargetHeight
ortargetWidth
are specified). TheCAMERA
source will always return the JPEG file given by the native camera and thePHOTOLIBRARY
andSAVEDPHOTOALBUM
sources will return the selected file in its existing encoding.
iOS Quirks
When using
destinationType.FILE_URI
, photos are saved in the application's temporary directory. The contents of the application's temporary directory is deleted when the application ends.When using
destinationType.NATIVE_URI
andsourceType.CAMERA
, photos are saved in the saved photo album regardless on the value ofsaveToPhotoAlbum
parameter.When using
destinationType.NATIVE_URI
andsourceType.PHOTOLIBRARY
orsourceType.SAVEDPHOTOALBUM
, all editing options are ignored and link is returned to original picture.
Samples
The Camera plugin allows you to do things like open the device's Camera app and take a picture, or open the file picker and select one. The code snippets in this section demonstrate different tasks including:
Open the Camera app and take a Picture
Take a picture and return thumbnails (resized picture)
Take a picture and generate a FileEntry object
Select a file from the picture library
Select a JPEG image and return thumbnails (resized image)
Select an image and generate a FileEntry object
Take a Picture
Before you can take a picture, you need to set some Camera plugin options to pass into the Camera plugin's getPicture
function. Here is a common set of recommendations. In this example, you create the object that you will use for the Camera options, and set the sourceType
dynamically to support both the Camera app and the file picker.
Typically, you want to use a FILE_URI
instead of a DATA_URL
to avoid most memory issues. JPEG is the recommended encoding type for Android.
You take a picture by passing in the options object to getPicture
, which takes a CameraOptions object as the third argument. When you call setOptions
, pass Camera.PictureSourceType.CAMERA
as the picture source.
Once you take the picture, you can display it or do something else. In this example, call the app's displayImage
function from the preceding code.
To display the image on some platforms, you might need to include the main part of the URI in the Content-Security-Policy <meta>
element in index.html. Here is an example.
Take a Picture and Return Thumbnails (Resize the Picture)
To get smaller images, you can return a resized image by passing both targetHeight
and targetWidth
values with your CameraOptions object. In this example, you resize the returned image to fit in a 100px by 100px box (the aspect ratio is maintained, so 100px is either the height or width, whichever is greater in the source).
Select a File from the Picture Library
When selecting a file using the file picker, you also need to set the CameraOptions object. In this example, set the sourceType
to Camera.PictureSourceType.SAVEDPHOTOALBUM
. To open the file picker, call getPicture
just as you did in the previous example, passing in the success and error callbacks along with CameraOptions object.
Select an Image and Return Thumbnails (resized images)
Resizing a file selected with the file picker works just like resizing using the Camera app; set the targetHeight
and targetWidth
options.
Take a picture and get a FileEntry Object
If you want to do something like copy the image to another location, or upload it somewhere using the FileTransfer plugin, you need to get a FileEntry object for the returned picture. To do that, call window.resolveLocalFileSystemURL
on the file URI returned by the Camera app. If you need to use a FileEntry object, set the destinationType
to Camera.DestinationType.FILE_URI
in your CameraOptions object (this is also the default value).
You need the File plugin to call window.resolveLocalFileSystemURL
.
Here is the call to window.resolveLocalFileSystemURL
. The image URI is passed to this function from the success callback of getPicture
. The success handler of resolveLocalFileSystemURL
receives the FileEntry object.
In the example shown in the preceding code, you call the app's createNewFileEntry
function if you don't get a valid FileEntry object. The image URI returned from the Camera app should result in a valid FileEntry, but platform behavior on some emulators may be different for files returned from the file picker.
To see an example of writing to a FileEntry, see the File plugin README .
The code shown here creates a file in your app's cache (in sandboxed storage) named tempFile.jpeg
. With the new FileEntry object, you can copy the image to the file or do something else like upload it.
See Also:
Last updated