Camera Method in PhoneGap

In my previous article I explained the camera.getPicture method of Camera in Apache Cordova/PhoneGap which is used to get or retrieve the pictures from the camera and we can also take the picture using the camera. The picture is returned either as an URL of the image or a base64 encoded string.

navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );

The same parameter that we used in camera.getPicture can also be used in this second method; these are:

  • CameraSuccess

    This function shows the success of the callback function which means that if the picture or image is captured successfully then this provides the image data.
     

    function onPhotoDataSuccess(imageData) {      

    //Do something for image

    }

    The parameter that we use for CameraSuccess is imageData.ImageData to return the image as an URL of the image or the of Base64 encoded imageData, depending on the third attribute of the Camera.getPicture method.


    Example
     

    function onPhotoDataSuccess(imageData) {   

        var smallImage = document.getElementById('smallImage');   

        smallImage.src = "data:image/jpeg;base64," + imageData;

    }

     

  • CameraError

    This shows the error or onError callback function that gives an error. It takes as the parameter the Error Message.
     

    function onFail(message) {

        //Error message

    }
     

  • CameraOptions

    These are the optional parameters to customize the camera setting. The all optional parameter I discuss in my previous article where I describe the first method of camera.

 In this article I explain the method camera.cleanup method of camera in PhoneGap/Cordova. 

camera.cleanup

This method can be used to clean up the image files that were taken by the camera and stored in a temporary storage location. The camera.getPicture can be used to take the picture and this is used to remove the pictures stored in a temporary location. The only supported platform of this method is iOS.The camera.getPicture is used with camera.sourceType = camera.PictureSourceType.CAMERA and Camera.destinationType = Camera.DestinationType.FILE_URI

The Camera.cleanup method works when there is a picture so that after using this method the camera will clean up the picture/or image. The JavaScript code for some camera.getpicture methods is:

function capturePhoto() {

    // Take picture using device camera and retrieve image as base64-encoded string

    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,

        destinationType: destinationType.DATA_URL

    });

}

// A button will call this function 

function capturePhotoEdit() {

    // Take picture using device camera, allow edit, and retrieve image as base64-encoded string 

    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,

        destinationType: destinationType.DATA_URL

    });

}

// A button will call this function

function getPhoto(source) {

    // Retrieve image file location from specified source

    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,

        destinationType: destinationType.FILE_URI,

        sourceType: source

    });

}

The example of camera.cleanup method is as:
 

navigator.camera.cleanup(onSuccess, onFail);

function onSuccess()
{

     console.log("Camera cleanup success.")

}
function
onFail(message)
{

     alert('Failed because: ' + message);

}

This article simply shows how to use the camera.cleanup method of camera.

Up Next
    Ebook Download
    View all
    Learn
    View all