DedrisFrameworkDedrisFramework

← Plugins  /  Camera

📷

Camera

cordova-plugin-camera
iOS Android 🌐 Browser

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.

Installation

Add the plugin to your Cordova project from the command line:

terminal
cordova plugin add cordova-plugin-camera

Methods

The plugin exposes its functionality through the navigator.camera object. Call these after the deviceready event has fired:

MethodDescription
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.

Options

Pass an options object as the third argument to getPicture to control how the image is captured:

PropertyTypeDescription
qualityNumberImage quality from 0 to 100. Lower values produce smaller files.
destinationTypeNumberOutput format: Camera.DestinationType.DATA_URL (0), FILE_URI (1) or NATIVE_URI (2).
sourceTypeNumberWhere the image comes from: Camera.PictureSourceType.CAMERA, PHOTOLIBRARY or SAVEDPHOTOALBUM.
encodingTypeNumberFile encoding: Camera.EncodingType.JPEG (0) or PNG (1).
targetWidthNumberWidth in pixels to scale the image to.
targetHeightNumberHeight in pixels to scale the image to.
mediaTypeNumberType of media to pick: PICTURE, VIDEO or ALLMEDIA.
allowEditBooleanLet the user crop the image before it is returned.
correctOrientationBooleanRotate the image to account for the device's orientation when shot.
saveToPhotoAlbumBooleanSave the captured image to the device's photo album.
cameraDirectionNumberWhich camera to use: Camera.Direction.BACK (0) or FRONT (1).

Usage example

Take a photo and show it in an image element on the page:

www/js/app.js
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);
}

Tips

📚 Source: the technical reference on this page (the method, options and the install command) is based on the official Apache Cordova camera documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project