Ionic 5 Use Camera using Ionic Native plugin in Ionic 5 Latest version

Ionic 5 Application to use Camera and get pictures returning images as Base64 URL to show user Image taken.

Hybrid applications are very quick to build but sometimes it becomes challenging to achieve some features which involve access to hardware like GPS, Network, Storage, File Storage, Camera. Cordova comes in charge to provide plugins to make a connection with java libraries to use them.

Check: Ionic 3 Camera Tutorial Example

We have already discussed How to access the camera in Ionic’s version 3, in this article we will use Camera feature in Ionic application with the latest version 5 using Cordova and Ionic Native

Let’s start…

Create a new Ionic 5 Application

We will create an application using the latest version of Ionic CLI v5.0.3

Make sure you have installed the latest version by running following npm command

After updating the Ionic CLI run following command to create a new Ionic 5 application with a blank template.

Install Cordova and Native Camera Plugin

Next, run following Cordova and Ionic Native commands to install camera plugin to access camera features.

 

For IOS: Add following configuration in config.xml inside of the <platform name='ios>  section as IOS 10 asks for camera permission.

 

Import Camera Plugin in App Module

To use camera plugin in the application we will import it then add in the providers array.

Navigate to app.module.ts file then replace the below code in it.

we are done with configuration.

Use the Camera in Home Component

In the blank template we already have home component, add a button in home.page.html file to open the camera by calling takeSnap() method in (click) event handler

<img> tag will show the Image clicked using the camera.

Now open the home.page.ts file, then replace the following code in it.

Here we have imported for Camera & CameraOptions to control features.

Add Camera in the class constructor

The CameraOptions have parameters that are very important to control how images are handles clicked using the camera.

quality: can be from 1-100, due larger image size app becomes unstable so 20 is ok for reasonable image quality

destinationType: it can be FILE_URI (.jpg file path) or DATA_URI (base64)
encodingType: Image file type is returned in JPEG or PNG
mediaType: Used to get ALLMEDIA(Pictures or Videos), PICTURE or VIDEO

 

The takeSnap() method calls getPicture which returns a promise to get data url or base64 url based on the option provided in destinationType.

To test your application just connect your mobile with computer via USB cable and run following command:

$ ionic cordova run android -l

Note: Make sure you have USB debug mode enabled on your mobile under developer mode.

To create an APK file to test in mobile you can run the following command:

$ ionic cordova build android

Conclusion: Cordova plugin with ionic Native wrapper makes it very handy to use camera features in an Ionic application. It solves the basic function when the quality parameter is small otherwise it can do app crashes due to limited device memory of webviews which we use in hybrid apps. But its better than nothing 🙂

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
3 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Camila Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Camila
Guest
Camila

Thank you! Works perfectly!