4 min read

In this article by Purusothaman Ramanujam, the author of PhoneGap Beginner’s Guide Third Edition, we will look at the Camera API. The Camera API provides access to the device’s camera application using the Camera plugin identified by the cordova-plugin-camera key. With this plugin installed, an app can take a picture or gain access to a media file stored in the photo library and albums that the user created on the device. The Camera API exposes the following two methods defined in the navigator.camera object:

  • getPicture: This opens the default camera application or allows the user to browse the media library, depending on the options specified in the configuration object that the method accepts as an argument
  • cleanup: This cleans up any intermediate photo file available in the temporary storage location (supported only on iOS)

(For more resources related to this topic, see here.)

As arguments, the getPicture method accepts a success handler, failure handler, and optionally an object used to specify several camera options through its properties as follows:

  • quality: This is a number between 0 and 100 used to specify the quality of the saved image.
  • destinationType: This is a number used to define the format of the value returned in the success handler. The possible values are stored in the following Camera.DestinationType pseudo constants:
    • DATA_URL(0): This indicates that the getPicture method will return the image as a Base64-encoded string
    • FILE_URI(1): This indicates that the method will return the file URI
    • NATIVE_URI(2): This indicates that the method will return a platform-dependent file URI (for example, assets-library:// on iOS or content:// on Android)
  • sourceType: This is a number used to specify where the getPicture method can access an image. The following possible values are stored in the Camera.PictureSourceType pseudo constants: PHOTOLIBRARY (0), CAMERA (1), and SAVEDPHOTOALBUM (2):
    • PHOTOLIBRARY: This indicates that the method will get an image from the device’s library
    • CAMERA: This indicates that the method will grab a picture from the camera
    • SAVEDPHOTOALBUM: This indicates that the user will be prompted to select an album before picking an image
  • allowEdit: This is a Boolean value (the value is true by default) used to indicate that the user can make small edits to the image before confirming the selection; it works only in iOS.
  • encodingType: This is a number used to specify the encoding of the returned file. The possible values are stored in the Camera.EncodingType pseudo constants: JPEG (0) and PNG (1).
  • targetWidth and targetHeight: These are the width and height in pixels, to which you want the captured image to be scaled; it’s possible to specify only one of the two options. When both are specified, the image will be scaled to the value that results in the smallest aspect ratio (the aspect ratio of an image describes the proportional relationship between its width and height).
  • mediaType: This is a number used to specify what kind of media files have to be returned when the getPicture method is called using the Camera.PictureSourceType.PHOTOLIBRARY or Camera.PictureSourceType.SAVEDPHOTOALBUM pseudo constants as sourceType; the possible values are stored in the Camera.MediaType object as pseudo constants and are PICTURE (0), VIDEO (1), and ALLMEDIA (2).
  • correctOrientation: This is a Boolean value that forces the device camera to correct the device orientation during the capture.
  • cameraDirection: This is a number used to specify which device camera has to be used during the capture. The values are stored in the Camera.Direction object as pseudo constants and are BACK (0) and FRONT (1).
  • popoverOptions: This is an object supported on iOS to specify the anchor element location and arrow direction of the popover used on iPad when selecting images from the library or album.
  • saveToPhotoAlbum: This is a Boolean value (the value is false by default) used in order to save the captured image in the device’s default photo album.

The success handler receives an argument that contains the URI to the file or data stored in the file’s Base64-encoded string, depending on the value stored in the encodingType property of the options object. The failure handler receives a string containing the device’s native code error message as an argument.

Similarly, the cleanup method accepts a success handler and a failure handler. The only difference between the two is that the success handler doesn’t receive any argument. The cleanup method is supported only on iOS and can be used when the sourceType property value is Camera.PictureSourceType.CAMERA and the destinationType property value is Camera.DestinationType.FILE_URI.

Summary

In this article, we looked at the various properties available with the Camera API.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here