2010-10-19 04:31:16 +08:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
2011-05-21 01:50:20 +08:00
|
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
2010-10-19 04:31:16 +08:00
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-03-31 02:29:24 +08:00
|
|
|
if (!PhoneGap.hasResource("camera")) {
|
|
|
|
PhoneGap.addResource("camera");
|
|
|
|
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* This class provides access to the device camera.
|
2010-09-03 00:27:48 +08:00
|
|
|
*
|
2009-11-18 02:38:49 +08:00
|
|
|
* @constructor
|
|
|
|
*/
|
2011-05-21 01:50:20 +08:00
|
|
|
var Camera = function() {
|
2010-09-03 00:27:48 +08:00
|
|
|
this.successCallback = null;
|
|
|
|
this.errorCallback = null;
|
|
|
|
this.options = null;
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
|
|
|
/**
|
2010-09-17 00:04:27 +08:00
|
|
|
* Format of image that returned from getPicture.
|
|
|
|
*
|
|
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
|
|
* { quality: 80,
|
|
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
2010-09-17 01:47:13 +08:00
|
|
|
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
|
2010-09-17 00:04:27 +08:00
|
|
|
*/
|
|
|
|
Camera.DestinationType = {
|
|
|
|
DATA_URL: 0, // Return base64 encoded string
|
|
|
|
FILE_URI: 1 // Return file uri (content://media/external/images/media/2 for Android)
|
|
|
|
};
|
2010-09-17 01:47:13 +08:00
|
|
|
Camera.prototype.DestinationType = Camera.DestinationType;
|
2010-09-17 00:04:27 +08:00
|
|
|
|
2011-07-26 02:51:08 +08:00
|
|
|
/**
|
|
|
|
* Encoding of image returned from getPicture.
|
|
|
|
*
|
|
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
|
|
* { quality: 80,
|
|
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
|
|
|
* sourceType: Camera.PictureSourceType.CAMERA,
|
|
|
|
* encodingType: Camera.EncodingType.PNG})
|
|
|
|
*/
|
|
|
|
Camera.EncodingType = {
|
|
|
|
JPEG: 0, // Return JPEG encoded image
|
|
|
|
PNG: 1 // Return PNG encoded image
|
|
|
|
};
|
|
|
|
Camera.prototype.EncodingType = Camera.EncodingType;
|
|
|
|
|
2011-09-29 11:13:07 +08:00
|
|
|
/**
|
|
|
|
* Type of pictures to select from. Only applicable when
|
|
|
|
* PictureSourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM
|
|
|
|
*
|
|
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
|
|
* { quality: 80,
|
|
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
|
|
|
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
|
|
|
|
* mediaType: Camera.MediaType.PICTURE})
|
|
|
|
*/
|
|
|
|
Camera.MediaType = {
|
|
|
|
PICTURE: 0, // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
|
|
|
|
VIDEO: 1, // allow selection of video only, ONLY RETURNS URL
|
|
|
|
ALLMEDIA : 2 // allow selection from all media types
|
|
|
|
};
|
|
|
|
Camera.prototype.MediaType = Camera.MediaType;
|
|
|
|
|
|
|
|
|
2010-09-17 00:04:27 +08:00
|
|
|
/**
|
|
|
|
* Source to getPicture from.
|
|
|
|
*
|
|
|
|
* Example: navigator.camera.getPicture(success, fail,
|
|
|
|
* { quality: 80,
|
|
|
|
* destinationType: Camera.DestinationType.DATA_URL,
|
2010-09-17 01:47:13 +08:00
|
|
|
* sourceType: Camera.PictureSourceType.PHOTOLIBRARY})
|
2010-09-17 00:04:27 +08:00
|
|
|
*/
|
|
|
|
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)
|
|
|
|
};
|
2010-09-17 01:47:13 +08:00
|
|
|
Camera.prototype.PictureSourceType = Camera.PictureSourceType;
|
2010-09-17 00:04:27 +08:00
|
|
|
|
|
|
|
/**
|
2010-09-17 01:47:13 +08:00
|
|
|
* Gets a picture from source defined by "options.sourceType", and returns the
|
2010-09-17 00:04:27 +08:00
|
|
|
* image as defined by the "options.destinationType" option.
|
|
|
|
|
2010-09-17 01:47:13 +08:00
|
|
|
* The defaults are sourceType=CAMERA and destinationType=DATA_URL.
|
2010-09-03 00:27:48 +08:00
|
|
|
*
|
2009-11-18 02:38:49 +08:00
|
|
|
* @param {Function} successCallback
|
|
|
|
* @param {Function} errorCallback
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
|
|
|
Camera.prototype.getPicture = function(successCallback, errorCallback, options) {
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
// successCallback required
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof successCallback !== "function") {
|
2010-09-03 00:27:48 +08:00
|
|
|
console.log("Camera Error: successCallback is not a function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorCallback optional
|
2011-02-03 00:46:19 +08:00
|
|
|
if (errorCallback && (typeof errorCallback !== "function")) {
|
2010-09-03 00:27:48 +08:00
|
|
|
console.log("Camera Error: errorCallback is not a function");
|
|
|
|
return;
|
|
|
|
}
|
2011-04-04 07:57:10 +08:00
|
|
|
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
2011-04-04 07:57:10 +08:00
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.quality) {
|
|
|
|
options.quality = 80;
|
2010-09-17 00:04:27 +08:00
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.maxResolution) {
|
|
|
|
options.maxResolution = 0;
|
2010-09-17 00:04:27 +08:00
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.destinationType) {
|
|
|
|
options.destinationType = Camera.DestinationType.DATA_URL;
|
2011-07-26 02:51:08 +08:00
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.sourceType) {
|
|
|
|
options.sourceType = Camera.PictureSourceType.CAMERA;
|
2011-09-29 11:13:07 +08:00
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.encodingType) {
|
|
|
|
options.encodingType = Camera.EncodingType.JPEG;
|
|
|
|
}
|
|
|
|
if (!options.mediaType) {
|
|
|
|
options.mediaType = Camera.MediaType.PICTURE;
|
|
|
|
}
|
|
|
|
if (!options.targetWidth) {
|
|
|
|
options.targetWidth = -1;
|
2011-07-26 02:51:08 +08:00
|
|
|
} else if (typeof options.targetWidth == "string") {
|
|
|
|
var width = new Number(options.targetWidth);
|
|
|
|
if (isNaN(width) === false) {
|
2011-09-30 23:30:04 +08:00
|
|
|
options.targetWidth = width.valueOf();
|
2011-07-26 02:51:08 +08:00
|
|
|
}
|
|
|
|
}
|
2011-09-30 23:30:04 +08:00
|
|
|
if (!options.targetHeight) {
|
|
|
|
options.targetHeight = -1;
|
2011-07-26 02:51:08 +08:00
|
|
|
} else if (typeof options.targetHeight == "string") {
|
|
|
|
var height = new Number(options.targetHeight);
|
|
|
|
if (isNaN(height) === false) {
|
2011-09-30 23:30:04 +08:00
|
|
|
options.targetHeight = height.valueOf();
|
2011-07-26 02:51:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-30 23:30:04 +08:00
|
|
|
PhoneGap.exec(successCallback, errorCallback, "Camera", "takePicture", [options]);
|
2010-09-03 00:27:48 +08:00
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
|
|
|
PhoneGap.addConstructor(function() {
|
2011-02-03 00:46:19 +08:00
|
|
|
if (typeof navigator.camera === "undefined") {
|
|
|
|
navigator.camera = new Camera();
|
|
|
|
}
|
2009-11-18 02:38:49 +08:00
|
|
|
});
|
2011-05-21 01:50:20 +08:00
|
|
|
}
|