INTRODUCTION
This tutorial will use the cordova-camera-plugin to build a simple access camera in an Ionic 4/5 Framework app. I hope you already know what Ionic is and how it works. We'll use CLI, HTML, CSS, and a lot more.
- Step 1 — Create a simple Ionic/Angular app
- Step 2 — Cordova Camera plugin
- Step 3— Use Camera Plugin In Ionic/Angular app
- Step 4 — Build Ionic/Angular app in Android & Test
- Step 5— Various Cordova Camera Plugin Features & Usage
So let’s dive right in!
STEP 1 — GETTING STARTED
Make sure you have installed node JS and the most recent versions of Ionic & Cordova, and then let's make an app and add authentication to it. First, let's start by making a new app. Open Terminal or Command Prompt and go to the directory where you want to make an Ionic/Angular app. With the following command, you can change the directory.
cd Desktop/
Then, to make a new app, run the following command.
ionic start myAppName blank --type=angular --spec false
When you see this question after installing 'NPM' modules and dependencies, type 'N' because we are not using it yet.
Next, go to the app folder that you just made.
cd myAppName/
Now that we have made a successful Ionic/Angular app,
Step 2 — Let's Start Working on Ionic/Angular
To use the camera in our Ionic App, we must first install the camera plugin in our app. Run the following commands given below in the terminal of your ionic project to add the camera plugin:
ionic cordova plugin add cordova-plugin-camera
npm install @ionic-native/camera
Next, we need to add the platform we want to use. Depending on your platform, we can add the target platform by running the following commands:
ionic platform add android
[Note] Since IOS 10, the camera needs to be given permission
in your config.xml file.
<config-file parent="NSCameraUsageDescription" platform="ios"
target="*-Info.plist">
<string>We need this access to maintain our app</string>
</config-file>
Now, we've put everything we needed into our Ionic app and set it up.
Next, we need to import the Cordova Camera plugin and
register in the app.module.ts
file's providers array to use the camera plugin.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from
'@ionic/angular';
import { SplashScreen } from
'@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { Camera } from '@ionic-native/camera/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(),
AppRoutingModule],
providers: [
StatusBar, Camera,
SplashScreen,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule { }
So next next is to use camera plugin in our Ionic/Angular home component.
Go to home.page.html
file and open it. Then replace the file code with the below one.
Last, an Ionic home component will use a camera plugin.
Copy and paste the code below into the home.page.html file.
<ion-button (click)="captureImage()">
Take Picture
</ion-button>
<img [src]="imgURL" />
Go to the home.page.ts file and paste the code below into it.
import { Component } from '@angular/core';
import { Camera, CameraOptions } from
'@ionic-native/camera/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
imgURL: string;
options: CameraOptions = {
quality: 30,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
constructor(private camera: Camera) { }
captureImage() {
this.camera.getPicture(this.options).then((imageData) => {
// imageData is string | base64 or file URI
// If it's base64 (DATA_URL):
const base64Image = 'data:image/jpeg;base64,' + imageData;
this.imgURL = base64Image;
}, (err) => {
console.log(err);
// Handle error
});
}
}
Look at what we did above with the help of the Cordova Camera plugin in Our Ionic/Angular application.
Quality: We can set the image quality between 1 to 100.
In the above example, we set the image size to 30, and this will control the
image size.
destinationType: Choose how the value will be returned.
encodingType: This is the type of Image file (JPEG or PNG).
mediaType is used to find out the type of media.
It can mean either "Picture" or "Video."
This method calls the getPicture() method. It takes the camera options as a
parameter and returns the data that we set in a variable to show the clicked
image.
Let's test our camera functionality in Ionic/Angular app
To deploy Ionic/Angular project following command can be used:
ionic cordova platform add android
ionic cordova run android --aot
Please note that you can only test the camera plugin on the device itself.
You can't use the browser to try it out.
Run the following command to make an app to test on your phone:
ionic cordova build android --aot --prod
What is the difference between the native plugins for Ionic 4 and Ionic 5?
The camera plugin works the same way in Ionic 4 and 5 as in Ionic 3.
But the only difference is how the libraries are brought in. At the end of the import,
you need to add /ngx. Let's use the import command for Ionic 3 as an example.
import { Camera } from '@ionic-native/camera';
It is the Ionic 4 import command.
import { Camera } from '@ionic-native/camera/ngx';
The import path for Angular should end with /ngx.
Conclusion
We finally added the Native Camera feature to our Ionic 4/5 Angular app by adding the Cordova and Native plugins. Also, we talked about the different methods used in the Camera class. We hope this tutorial was helpful. Think about giving to others. Thanks for stopping by! Message me in the comments if you have any questions.