mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 23:42:53 +08:00
110 lines
3.4 KiB
JavaScript
Executable File
110 lines
3.4 KiB
JavaScript
Executable File
|
|
/**
|
|
* This class provides access to the device camera.
|
|
*
|
|
* @constructor
|
|
*/
|
|
Camera = function() {
|
|
this.successCallback = null;
|
|
this.errorCallback = null;
|
|
this.options = null;
|
|
};
|
|
|
|
/**
|
|
* Format of image that returned from getPicture.
|
|
*
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
* { quality: 80,
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
|
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
|
|
*/
|
|
Camera.DestinationType = {
|
|
DATA_URL: 0, // Return base64 encoded string
|
|
FILE_URI: 1 // Return file uri (content://media/external/images/media/2 for Android)
|
|
};
|
|
Camera.prototype.DestinationType = Camera.DestinationType;
|
|
|
|
/**
|
|
* Source to getPicture from.
|
|
*
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
* { quality: 80,
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
|
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
|
|
*/
|
|
Camera.PictureSourceType = {
|
|
PHOTOLIBRARY : 0, // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
|
|
CAMERA : 1, // Take picture from camera
|
|
SAVEDPHOTOALBUM : 2 // Choose image from picture library (same as PHOTOLIBRARY for Android)
|
|
};
|
|
Camera.prototype.PictureSourceType = Camera.PictureSourceType;
|
|
|
|
/**
|
|
* Gets a picture from source defined by "options.sourceType", and returns the
|
|
* image as defined by the "options.destinationType" option.
|
|
|
|
* The defaults are sourceType=CAMERA and destinationType=DATA_URL.
|
|
*
|
|
* @param {Function} successCallback
|
|
* @param {Function} errorCallback
|
|
* @param {Object} options
|
|
*/
|
|
Camera.prototype.getPicture = function(successCallback, errorCallback, options) {
|
|
|
|
// successCallback required
|
|
if (typeof successCallback != "function") {
|
|
console.log("Camera Error: successCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
// errorCallback optional
|
|
if (errorCallback && (typeof errorCallback != "function")) {
|
|
console.log("Camera Error: errorCallback is not a function");
|
|
return;
|
|
}
|
|
|
|
this.successCallback = successCallback;
|
|
this.errorCallback = errorCallback;
|
|
this.options = options;
|
|
var quality = 80;
|
|
if (options.quality) {
|
|
quality = this.options.quality;
|
|
}
|
|
var destinationType = Camera.DestinationType.DATA_URL;
|
|
if (this.options.destinationType) {
|
|
destinationType = this.options.destinationType;
|
|
}
|
|
var sourceType = Camera.PictureSourceType.CAMERA;
|
|
if (typeof this.options.sourceType == "number") {
|
|
sourceType = this.options.sourceType;
|
|
}
|
|
PhoneGap.execAsync(null, null, "Camera", "takePicture", [quality, destinationType, sourceType]);
|
|
};
|
|
|
|
/**
|
|
* Callback function from native code that is called when image has been captured.
|
|
*
|
|
* @param picture The base64 encoded string of the image
|
|
*/
|
|
Camera.prototype.success = function(picture) {
|
|
if (this.successCallback) {
|
|
this.successCallback(picture);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Callback function from native code that is called when there is an error
|
|
* capturing an image, or the capture is cancelled.
|
|
*
|
|
* @param err The error message
|
|
*/
|
|
Camera.prototype.error = function(err) {
|
|
if (this.errorCallback) {
|
|
this.errorCallback(err);
|
|
}
|
|
};
|
|
|
|
PhoneGap.addConstructor(function() {
|
|
if (typeof navigator.camera == "undefined") navigator.camera = new Camera();
|
|
});
|