← Plugins / Camera
This official Apache Cordova plugin lets your app take photos with the device camera or pick
existing images from the photo library. The captured image comes back either as a Base64 data
URL you can drop straight into an <img> tag, or as a file URI pointing at the
image on disk — useful when you want to keep memory usage low.
Add the plugin to your Cordova project from the command line:
cordova plugin add cordova-plugin-camera
The plugin exposes its functionality through the navigator.camera object. Call these after the deviceready event has fired:
| Method | Description |
|---|---|
navigator.camera.getPicture(onSuccess, onError, options) | Opens the camera or library and returns an image. The onSuccess callback receives imageData — a string that is either Base64 text or a file URI, depending on the destinationType you request. |
navigator.camera.cleanup(onSuccess, onError) | iOS only. Deletes the temporary image files that were created when taking pictures. |
Pass an options object as the third argument to getPicture to control how the image is captured:
| Property | Type | Description |
|---|---|---|
quality | Number | Image quality from 0 to 100. Lower values produce smaller files. |
destinationType | Number | Output format: Camera.DestinationType.DATA_URL (0), FILE_URI (1) or NATIVE_URI (2). |
sourceType | Number | Where the image comes from: Camera.PictureSourceType.CAMERA, PHOTOLIBRARY or SAVEDPHOTOALBUM. |
encodingType | Number | File encoding: Camera.EncodingType.JPEG (0) or PNG (1). |
targetWidth | Number | Width in pixels to scale the image to. |
targetHeight | Number | Height in pixels to scale the image to. |
mediaType | Number | Type of media to pick: PICTURE, VIDEO or ALLMEDIA. |
allowEdit | Boolean | Let the user crop the image before it is returned. |
correctOrientation | Boolean | Rotate the image to account for the device's orientation when shot. |
saveToPhotoAlbum | Boolean | Save the captured image to the device's photo album. |
cameraDirection | Number | Which camera to use: Camera.Direction.BACK (0) or FRONT (1). |
Take a photo and show it in an image element on the page:
function takePhoto() {
navigator.camera.getPicture(onSuccess, onError, {
quality: 70,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
correctOrientation: true
});
}
function onSuccess(imageData) {
var img = document.getElementById("photo");
img.src = "data:image/jpeg;base64," + imageData;
}
function onError(message) {
console.log("Camera error: " + message);
}
deviceready has fired — the plugin isn't ready before then.FILE_URI over DATA_URL to avoid the memory pressure that big Base64 strings cause.quality and targetWidth to keep the resulting files small.