2013-05-22 03:11:20 +08:00
/ *
*
* Licensed to the Apache Software Foundation ( ASF ) under one
* or more contributor license agreements . See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership . The ASF licenses this file
* to you under the Apache License , Version 2.0 ( the
* "License" ) ; you may not use this file except in compliance
* with the License . You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing ,
* software distributed under the License is distributed on an
* "AS IS" BASIS , WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND , either express or implied . See the License for the
* specific language governing permissions and limitations
* under the License .
*
* /
2023-08-17 23:59:25 +08:00
const argscheck = require ( 'cordova/argscheck' ) ;
const exec = require ( 'cordova/exec' ) ;
const Camera = require ( './Camera' ) ;
2017-06-10 05:51:30 +08:00
// XXX: commented out
// CameraPopoverHandle = require('./CameraPopoverHandle');
2013-05-22 03:11:20 +08:00
2016-02-18 10:24:30 +08:00
/ * *
2015-10-05 18:48:19 +08:00
* @ namespace navigator
* /
/ * *
* @ exports camera
* /
2023-08-17 23:59:25 +08:00
const cameraExport = { } ;
2013-05-22 03:11:20 +08:00
// Tack on the Camera Constants to the base camera plugin.
2023-08-17 23:59:25 +08:00
for ( const key in Camera ) {
2013-05-22 03:11:20 +08:00
cameraExport [ key ] = Camera [ key ] ;
}
/ * *
2015-10-05 18:48:19 +08:00
* Callback function that provides an error message .
* @ callback module : camera . onError
* @ param { string } message - The message is provided by the device ' s native code .
* /
/ * *
* Callback function that provides the image data .
* @ callback module : camera . onSuccess
* @ param { string } imageData - Base64 encoding of the image data , _or _ the image file URI , depending on [ ` cameraOptions ` ] { @ link module : camera . CameraOptions } in effect .
* @ example
* // Show image
* //
* function cameraCallback ( imageData ) {
* var image = document . getElementById ( 'myImage' ) ;
* image . src = "data:image/jpeg;base64," + imageData ;
* }
* /
/ * *
* Optional parameters to customize the camera settings .
* * [ Quirks ] ( # CameraOptions - quirks )
* @ typedef module : camera . CameraOptions
* @ type { Object }
* @ property { number } [ quality = 50 ] - Quality of the saved image , expressed as a range of 0 - 100 , where 100 is typically full resolution with no loss from file compression . ( Note that information about the camera ' s resolution is unavailable . )
* @ property { module : Camera . DestinationType } [ destinationType = FILE _URI ] - Choose the format of the return value .
* @ property { module : Camera . PictureSourceType } [ sourceType = CAMERA ] - Set the source of the picture .
2018-02-12 01:22:34 +08:00
* @ property { Boolean } [ allowEdit = false ] - Allow simple editing of image before selection .
2015-10-05 18:48:19 +08:00
* @ property { module : Camera . EncodingType } [ encodingType = JPEG ] - Choose the returned image file ' s encoding .
* @ property { number } [ targetWidth ] - Width in pixels to scale image . Must be used with ` targetHeight ` . Aspect ratio remains constant .
* @ property { number } [ targetHeight ] - Height in pixels to scale image . Must be used with ` targetWidth ` . Aspect ratio remains constant .
* @ property { module : Camera . MediaType } [ mediaType = PICTURE ] - Set the type of media to select from . Only works when ` PictureSourceType ` is ` PHOTOLIBRARY ` or ` SAVEDPHOTOALBUM ` .
* @ property { Boolean } [ correctOrientation ] - Rotate the image to correct for the orientation of the device during capture .
* @ property { Boolean } [ saveToPhotoAlbum ] - Save the image to the photo album on the device after capture .
* @ property { module : CameraPopoverOptions } [ popoverOptions ] - iOS - only options that specify popover location in iPad .
* @ property { module : Camera . Direction } [ cameraDirection = BACK ] - Choose the camera to use ( front - or back - facing ) .
* /
2013-05-22 03:11:20 +08:00
2015-10-05 18:48:19 +08:00
/ * *
* @ description 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
2016-03-02 09:09:03 +08:00
* Base64 - encoded ` String ` , or as the URI for the image file .
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* The ` camera.getPicture ` function opens the device ' s default camera
* application that allows users to snap pictures by default - this behavior occurs ,
2016-02-18 10:24:30 +08:00
* when ` Camera.sourceType ` equals [ ` Camera.PictureSourceType.CAMERA ` ] { @ link module : Camera . PictureSourceType } .
2015-10-05 18:48:19 +08:00
* Once the user snaps the photo , the camera application closes and the application is restored .
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* If ` Camera.sourceType ` is ` Camera.PictureSourceType.PHOTOLIBRARY ` or
* ` Camera.PictureSourceType.SAVEDPHOTOALBUM ` , then a dialog displays
2016-08-23 21:14:11 +08:00
* that allows users to select an existing image .
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* The return value is sent to the [ ` cameraSuccess ` ] { @ link module : camera . onSuccess } callback function , in
* one of the following formats , depending on the specified
* ` cameraOptions ` :
2016-02-18 10:24:30 +08:00
*
2016-03-02 09:09:03 +08:00
* - A ` String ` containing the Base64 - encoded photo image .
2015-10-05 18:48:19 +08:00
* - A ` String ` representing the image file location on local storage ( default ) .
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* You can do whatever you want with the encoded image or URI , for
* example :
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* - Render the image in an ` <img> ` tag , as in the example below
* - Save the data locally ( ` LocalStorage ` , [ Lawnchair ] ( http : //brianleroux.github.com/lawnchair/), etc.)
* - Post the data to a remote server
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* _ _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 ` .
*
* _ _Supported Platforms _ _
*
2016-02-18 10:24:30 +08:00
* - Android
* - Browser
* - iOS
2013-05-22 03:11:20 +08:00
*
2016-02-18 10:24:30 +08:00
* More examples [ here ] ( # camera - getPicture - examples ) . Quirks [ here ] ( # camera - getPicture - quirks ) .
2015-10-05 18:48:19 +08:00
*
* @ example
* navigator . camera . getPicture ( cameraSuccess , cameraError , cameraOptions ) ;
* @ param { module : camera . onSuccess } successCallback
* @ param { module : camera . onError } errorCallback
* @ param { module : camera . CameraOptions } options CameraOptions
2013-05-22 03:11:20 +08:00
* /
2017-06-10 05:51:30 +08:00
cameraExport . getPicture = function ( successCallback , errorCallback , options ) {
2013-05-22 03:11:20 +08:00
argscheck . checkArgs ( 'fFO' , 'Camera.getPicture' , arguments ) ;
options = options || { } ;
2023-08-17 23:59:25 +08:00
const getValue = argscheck . getValue ;
2013-05-22 03:11:20 +08:00
2023-08-17 23:59:25 +08:00
const quality = getValue ( options . quality , 50 ) ;
const destinationType = getValue ( options . destinationType , Camera . DestinationType . FILE _URI ) ;
const sourceType = getValue ( options . sourceType , Camera . PictureSourceType . CAMERA ) ;
const targetWidth = getValue ( options . targetWidth , - 1 ) ;
const targetHeight = getValue ( options . targetHeight , - 1 ) ;
const encodingType = getValue ( options . encodingType , Camera . EncodingType . JPEG ) ;
const mediaType = getValue ( options . mediaType , Camera . MediaType . PICTURE ) ;
const allowEdit = ! ! options . allowEdit ;
const correctOrientation = ! ! options . correctOrientation ;
const saveToPhotoAlbum = ! ! options . saveToPhotoAlbum ;
const popoverOptions = getValue ( options . popoverOptions , null ) ;
const cameraDirection = getValue ( options . cameraDirection , Camera . Direction . BACK ) ;
2013-05-22 03:11:20 +08:00
2024-10-30 20:48:20 +08:00
if ( allowEdit ) {
console . warn ( 'allowEdit is deprecated. It does not work reliably on all platforms. Utilise a dedicated image editing library instead. allowEdit functionality is scheduled to be removed in a future release.' ) ;
}
2023-08-17 23:59:25 +08:00
const args = [ quality , destinationType , sourceType , targetWidth , targetHeight , encodingType ,
2017-06-10 05:51:30 +08:00
mediaType , allowEdit , correctOrientation , saveToPhotoAlbum , popoverOptions , cameraDirection ] ;
2013-05-22 03:11:20 +08:00
2017-06-10 05:51:30 +08:00
exec ( successCallback , errorCallback , 'Camera' , 'takePicture' , args ) ;
2013-09-21 01:59:05 +08:00
// XXX: commented out
2017-06-10 05:51:30 +08:00
// return new CameraPopoverHandle();
2013-05-22 03:11:20 +08:00
} ;
2015-10-05 18:48:19 +08:00
/ * *
* Removes intermediate image files that are kept in temporary storage
* after calling [ ` camera.getPicture ` ] { @ link module : camera . getPicture } . Applies only when the value of
* ` Camera.sourceType ` equals ` Camera.PictureSourceType.CAMERA ` and the
* ` Camera.destinationType ` equals ` Camera.DestinationType.FILE_URI ` .
*
* _ _Supported Platforms _ _
*
2016-02-18 10:24:30 +08:00
* - iOS
2015-10-05 18:48:19 +08:00
*
* @ example
* navigator . camera . cleanup ( onSuccess , onFail ) ;
2016-02-18 10:24:30 +08:00
*
2015-10-05 18:48:19 +08:00
* function onSuccess ( ) {
* console . log ( "Camera cleanup success." )
* }
*
* function onFail ( message ) {
* alert ( 'Failed because: ' + message ) ;
* }
* /
2017-06-10 05:51:30 +08:00
cameraExport . cleanup = function ( successCallback , errorCallback ) {
exec ( successCallback , errorCallback , 'Camera' , 'cleanup' , [ ] ) ;
2013-05-22 03:11:20 +08:00
} ;
module . exports = cameraExport ;