Introduction
The Camera object provides the access to the Camera's default pictures or application. There are two type of methods that we use in Camera objects; they are:
Camera.getPicture
Camera.cleanup
In this article I will explain the Camera.getPicture method in details. The Camera.getPicture method can be 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 the base64 encoded string.
navigator.camera.getPicture( cameraSuccess, cameraError, [ cameraOptions ] );
First of all I will explain the parameters of getPicture that are cameraSuccess, cameraError and cameraOptions.
- 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.
- 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. Some of the options are:
Options
Descriptions
Quality Represents the quality of the image, range is [0,100] number. DestinationType The format of the return value that is defined in navigator.camera.DestinationType (number) SourceType Set the source of the picture. Defined in nagivator.camera.PictureSourceType (number) allowEdit Allow simple editing of image before selection. (boolean) encodingType The encoding of the returned image file. Defined in navigator.camera.EncodingType (number) targetWidth Width in pixels to scale image. Must be used with targetHeight. Aspect ratio is maintained.(number) targetHeight Height in pixels to scale image. Must be used with targetWidth. Aspect ratio is maintained. (number) mediaType Set the type of media to select from. Only works when PictureSourceType is PHOTOLIBRARY or SAVEDPHOTOALBUM. Defined in nagivator.camera.MediaType (number) correctOrientation Rotate the image to correct for the orientation of the device during capture. (boolean) saveToPhotoAlbum Save the image to the photo album on the device after capture. (boolean) popoverOptions iOS only options to specify popover location in iPad.
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, depanding 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;
}
The Function camera.getPicture opens the mobile device's default camera application so that we can take a picture if the following condition is satisfied. Once the picture is taken the camera application closes and the picture is saved.
(if Camera.sourceType = Camera.PictureSourceType.CAMERA,
But if we want to choose from the camera application then the conditions are:
If Camera.sourceType = Camera.PictureSourceType.PHOTOLIBRARY or Camera.PictureSourceType.SAVEDPHOTOALBUM,
The return value will be sent to the cameraSuccess parameter of the getPicture mathod.
- A
Stringcontaining the Base64 encoded photo image (default). - A
Stringrepresenting the image file location on local storage.
The supported platforms of this method are:
- Android
- Blackberry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7 ( Mango )
- Bada 1.2
- webOS
Now let's develop the application. For this the following steps are followed:
Step 1: Open Visual Studio and then select File -> New -> Project.

Step 2: A window is shown; from this select Silverlight for Window Phone from Visual C#; now select CordovaStarter and name the application "camera".

Step 3: In the Solution Explorer, there is a folder named www; inside this are the HTML, JavaScript and CSS files.

Step 4: Inside the index.html, write the code as:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Camera Example</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title"
charset="utf-8" />
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8" src="javascript.js"></script>
</head>
<body>
<button onclick="capturePhoto();">
Capture Photo</button><br />
<button onclick="capturePhotoEdit();">
Capture Editable Photo</button><br />
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">
From Photo Library</button><br />
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">
From Photo Album</button><br />











SubashPosted Jul 14, 2016, 6:22 AM
Nice