/** * @aside guide sensor_apis * ## Example * * You can use the {@link Ext.space.Camera#capture} function to take a photo: * * var promise = Ext.space.Camera.capture({ * destination: 'file' * collection: 'photos' * }); * * * By specifying a destination of 'file' capture will store the selected photo in the * applications Secure File system. The promise resolves with the `Ext.space.files.File` * that represents the file: * * promise.then(function(file){ * file.view(); // display the photo in Sencha's file viewer. * }) * * Specifying a collection when capturing a photo will place that photo inside to the * named collection on the filesytem. See `Ext.space.files.Collection` for more details. * It is a good idea to keep all of the application's photos organized inside of * collections. * * See `Ext.space.files.File` for details on how to use the captured photo. * File contains a URL that can be used to display the image using an img tag. * The file can also be viewed in a new tab (Ext.space.files.File.view) * Because the file is store persistent make sure to delete photos you no-longer need. * * See the documentation for {@link Ext.space.Camera#capture} all available configurations. */Ext.define('Ext.space.Camera', { singleton: true, source: { library: 0, camera: 1, album: 2 }, destination: { data: 0, // Returns base64-encoded string file: 1 // Returns file's URI }, encoding: { jpeg: 0, jpg: 0, png: 1 }, /** * Allows you to capture a photo. * * @param {Object} options * The options to use when taking a photo. * * @param {Number} options.quality * The quality of the image which is returned in the callback. This should be a percentage. * * @param {String} options.source * The source of where the image should be taken. Available options are: * * - **album** - prompts the user to choose an image from an album * - **camera** - prompts the user to take a new photo (default) * - **library** - prompts the user to choose an image from the library * * @param {String} options.destination * The destination of the image which is returned. Available options are: * * - **data** - returns a base64 encoded string (default) * - **file** - Will store captured photo in the applications file system. returns an Ext.space.files.File * * @param {String} options.collection * The name of the Ext.space.files.Collection where the file should be stored. * * * @param {String} options.encoding * The encoding of the returned image. Available options are: * * - **jpg** (default) * - **png** * * @param {Number} options.width * The width of the image to return * * @param {Number} options.height * The height of the image to return * * * @return {Ext.space.Promise} Promise that resolves when the image is captured */ capture: function(options) { var sources = this.source, destinations = this.destination, encodings = this.encoding, path = options.collection, source = options.source || "camera", destination = options.destination || "data", encoding = options.encoding || "jpg"; var result = new Ext.space.Promise(); if (sources.hasOwnProperty(source)) { source = sources[source]; } if (destinations.hasOwnProperty(destination)) { destination = destinations[destination]; } if (encodings.hasOwnProperty(encoding)) { encoding = encodings[encoding]; } Ext.space.Communicator.send({ command: 'Camera#capture', callbacks: { success: function(image){ if(destinations.file === destination) { Ext.space.SecureFiles.getFile(image).connect(result); } else { result.fulfill(image); } }, failure: function(error){ result.reject(error); } }, scope: options.scope, quality: options.quality, width: options.width, height: options.height, source: source, destination: destination, encoding: encoding, path: path }); return result; }});