blog

Home / DeveloperSection / Blogs / Cordova – Camera Plugin

Cordova – Camera Plugin

Anonymous User 1951 20-Apr-2017

There is times, when you have to use Camera in your app, then for this, you have to integrate it into the app using the Camera Plugin.

This plugin is used for taking photos using camera or using files from phone’s image gallery.

Step 1 - Installing Camera Plugin

Open the command prompt window and execute the following code in order to install this plugin.

C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-camera
 

Step 2 - Add Images and Buttons

In this step you can create a button for calling the camera and img where the image will be displayed once taken. This will be added to index.html inside div class = "app" element.

<button id = "cameraButton">CAPTURE</button>

<img id = "newImage"></img>

 Step 3 - Add Event Listener

The event listener is added inside onDeviceReady function to ensure that Cordova is loaded before we start using it.

document.getElementById("cameraButton ").addEventListener

   ("click", cameraButton);

 Step 4 - Adding Functions (taking photo)

You have to create capturePicture function that will be passed as a callback to the event listener. It will be triggered when the button is tapped. Inside this function we are calling a global object provided by the plugin API navigator.camera. If picture capture is successful, the data will be sent to callback function onSuccess , if not, the alert with error message will be shown. We will write this piece of code at the bottom of index.js file.



function capturePicture() {
   navigator.camera.getPicture(onSuccess, onFail, {
      quality: 50,
      destinationType: Camera.DestinationType.DATA_URL
   });
   function onSuccess(imageData) {
      var image = document.getElementById('newImage');
      image.src = "data:image/jpeg;base64," + imageData;
   }
   function onFail(message) {
      alert('Failed because: ' + message);
   }
}

 

Now, when you run the app and press the capture button, native camera will get fired.


Cordova – Camera Plugin

When we take and save picture, it will be displayed on the screen.

Cordova – Camera Plugin


The same STEPS can be also used for getting image from local file system i.e. phone’. The only difference is the function created in the last step. You can see that an optional parameter sourceType is added.

Step 1 B     

C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-camera

 Step 2 B

<button id = "capturePicture">CAPTURE</button>

Step 3 B

document.getElementById("capturePicture ").addEventListener("click", capturePicture);

 Step 4 B


function capturePicture () {
   navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
      destinationType: Camera.DestinationType.DATA_URL,
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY
   });
   function onSuccess(imageURL) {
      var image = document.getElementById('newImage');
      image.src = imageURL;
   }
   function onFail(message) {
      alert('Failed because: ' + message);
   }
}

 

When you will press the other button, the file system will be open in place of the camera so we can choose the image from the file system we want to display.

Cordova – Camera Plugin

There are lots of optional parameters provided in this plugin for customization. These are listed below in the table:

SN

Parameter & Details

1

quality

Quality of the image in the range of 0-100. Default is 50.

2

destinationType

DATA_URL or 0 Returns base64 encoded string.

NATIVE_URI or 2 Returns image native URI.

FILE_URI or 1 Returns image file URI.

3

sourceType

CAMERA or 1 Opens native camera.

PHOTOLIBRARY or 0 Opens photo library

SAVEDPHOTOALBUM or 2 Opens saved photo album.

4

allowEdit

Allows image editing.

5

encodingType

PNG or 1 Returns PNG encoded image.

JPEG or 0 Returns JPEG encoded image.

6

targetWidth

Image scaling width in pixels.

7

targetHeight

Image scaling height in pixels.

8

mediaType

VIDEO or 1 Allows only video selection.

PICTURE or 0 Allows only picture selection.

ALLMEDIA or 2 Allows all media type selection.

9

correctOrientation

Used for correcting orientation of the image.

10

saveToPhotoAlbum

Used to save the image to the photo album.

11

popoverOptions

Used for setting popover location on IOS.

12

cameraDirection

BACK or 1 Back camera.

FRONT or 0 Front camera.

ALLMEDIA


I am a content writter !

Leave Comment

Comments

Liked By