navigator.camera.getPicture
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 method itself returns a CameraPopoverHandle object that can be used to reposition the file selection popover.
navigator.camera.getPicture( cameraSuccess, cameraError, cameraOptions );
Description
The camera.getPicture function opens the device’s default camera application that allows users to snap pictures. This behavior occurs by default, when Camera.sourceTypeequals 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 camera.getPicture function returns a CameraPopoverHandle object, which can be used to reposition the image selection dialog, for example, when the device orientation changes.
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.
Example:
navigator.camera.getPicture(pictureSuccess, pictureFail, {quality:100, destinationType:Camera.DestinationType.DATA_URL});
pictureSuccess = function(imageData) {
var picture = document.getElementById(“id-of-img-element”);
picture.src = “data:image/jpeg;base64,”+imageData;
};
pictureFail = function(message) {
alert(“Fail, cause: “+message);
};
Source Documentation: http://plugins.cordova.io/#/package/org.apache.cordova.camera