Ionic Cordova Camera - Ionic

What is Cordova Camera in Ionic Framework?

Use of the native camera for taking pictures or extracting images from the image gallery is known as Ionic Cordova Camera plugin.

How to use Cordova Camera in Ionic Framework?

Cordova camera plugin is downloaded and installed in the project root folder by the command:

A service is created for using the camera plugin. AngularJs factory and promise object $q has to be injected to the factory.

services.js Code

The service is used in the app by injecting it to the controller as dependency. To take pictures using native camera, getPicture method is provided by Cordova camera API. The settings of the camera can be customized by passing options parameter to the takePicture function. Copy this code sample to the controller to trigger this behavior. It will open camera application and return success callback function with the image data or error callback function with error message. Two buttons are required that will call the functions to create and the image need to be shown on the screen.

HTML Code

Controller Code

Ionic Cordova Camera

To extract the images from the gallery, change the sourceType method from the options parameter. A dialogue popup is opened by the change, instead of camera and enables to choose the image desired from the device. In the below code the sourceType option is changed to 0. When the second button is tapped, the file menu is opened from the device.

Controller Code

Ionic Cordova Camera Gallery

When the image clicked is saved, it will appear on the screen, which can be styled as desired.

Ionic Cordova Camera Image

The other options used for Cordova Camera are:

Parameter
Type
Details
quality
Number
The quality of the image, range 0-100
destinationType
Number
Format of the image.
sourceType
Number
Used for setting the source of the picture.
allowEdit
boolean
Used for allowing editing of the image.
encodingType
Number
Value 0 will set JPEG, and value 1 will set PNG.
targetWidth
Number
Used for scaling image width.
targetHeight
Number
Used for scaling image height.
mediaType
string
Used for setting the media type.
cameraDirection
Number
Value 0 will set the back camera, and value 1 will set the front camera.
popoverOptions
string
iOS-only options that specify popover location in iPad.
saveToPhotoAlbum
boolean
Used for saving image to photo album.
correctOrientation
boolean
Used for correcting orientation of the captured images.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ionic Topics