×

Ionic Camera

Hybrid mobile applications can make very quickly, but sometimes it becomes a challenge to get the features that access the hardware such as GPS, storage, file storage, cameras, and networks. Cordova offers plugins to build relationships to use them with java libraries.

The ionic native camera plugin allows the user to click the image by using the device camera. In this topic, we are going to use the ionic camera feature in the ionic application by using the Cordova and ionic native plugin.

Now, you will see step by step all the processes of the ionic camera in the ionic application.

Step 1: Firstly, you create a new project in the ionic application. You also make sure that you have installed the latest version of the ionic framework.

Step 2: After installation of the latest version of ionic, you are required to select a blank template from the various templates. After this process, you run the following commands to install the Cordova camera plugin in the ionic application.

$ cd myapp
 $ ionic cordova plugin add cordova-plugin-camera
 $ npm install @ionic-native/camera                      

Step 3: In this step, you are required to import camera plugin in the app.module.ts file, as shown below:

app.module.ts

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,
     SplashScreen,
     Camera,
     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
   ],
   bootstrap: [AppComponent]
 })
 export class AppModule { } 

Step 4: In this step, you also required to import the camera plugin in the home.page.ts file. Here, you create a function to click the photo, as shown below:

Home.page.ts

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'], 
 }) 
   constructor(private camera: Camera) { }
   takeSnap() {
     this.camera.getPicture(this.cameraOptions).then((imageData) => {
       // this.camera.DestinationType.FILE_URI gives file URI saved in local
       // this.camera.DestinationType.DATA_URL gives base64 URI
       let base64Image = 'data:image/jpeg;base64,' + imageData;
       this.capturedSnapURL = base64Image;
     }, (err) => {
       console.log(err);
       // Handle error
     });
   }
 } 

Step 5: In this step, you write the code in the Home.page.html file and create a button to click the photo. Here, we have created a click function to click the image, as shown below:

Home.page.html

<ion-header>  
   <ion-toolbar color="success">  
     <ion-title>Ionic Camera Example</ion-title>  
   </ion-toolbar>  
 </ion-header>  
 <ion-content>  
   <div class="padding">  
 <ion-button shape="round" color="danger" (click)="takeSnap()">  
       Click Image
     </ion-button>  
     <img [src]="capturedSnapURL" />  
   </div>  
 </ion-content>   

Step 6: In this step, you can use your android device to run the app. Before testing the app, it is required to add the platform to the app. The following command helps to install the platform with android:

$ ionic cordova platform add android
 $ ionic cordova run android –aot 

If you want to run and test the app in the browser, execute the below command:

$ ionic cordova run browser

Output:

After executing the above code, you will get the output. When you click on the Click Image button, it takes the image from the device as shown below:

Ionic Camera

Camera Option

Different options are available in the ionic camera that is used in the ionic application, as shown in the following table:

S.No Parameter Type Description
1. allowEdit Boolean It allows the user to edit the image before selection.
2. cameraDirection number It is used to choose the camera direction. By default, camera is back and back = 0, front = 1.
3. destinationType number It is used to choose the format of the camera picture.
4. encodingType number It is used to choose the encoding image.
5. correctOrientatiom boolean It is used to rotate the image to the correct orientation of the device during the image capturing.
6. popoverOptions string It is used for iOS options that specify a popover location on the IPad.
7. quality number It is used to set the image quality in range 0 to 100. The default is 50.
8. mediaType number It is used to set the type of media.
9. sourceType number It is used to set the source of the picture.
10. saveToPhotoAlbum boolean It is used to save images in the photo album on the device.
11. targetHeight number It is used to scaling the image width in pixels.
12. targetWidth number It is used to scaling the image height in pixels.

Related Topics

Ionic Colors

Ionic Colors Before we start with the actual elements available in the Ionic frame, let's understand a little bit how Ionic uses colors for different elements. Ionic Color Classes Ionic framework is a set of nine predefined...

3 minutes read.

Ionic-fab Button

Ionic-fab Button The Ionic FABs (Floating Action Buttons) are standard material design compounds. It is a container element that contains one or more fab buttons. They are required to be placed in a fixed position,...

2 minutes read.

Ionic Slides

The ionic slides component is the multi-section container. You can swipe or drag any segment between them. This contains any number of slides components. Slides help it easy to make galleries, tutorials, and...

2 minutes read.

Ionic vs React Native

Ionic Vs React Native Ionic and React Native are both common framework which is used to develop mobile applications. Here, you will know about the popular differences between the ionic and the React Native. Ionic Ionic...

3 minutes read.

Ionic Buttons

Ionic Buttons There are different types of buttons in the Ionic framework. These buttons are subtly animated, enhancing the user experience when using the application. Buttons are a fundamental way of interacting and navigating...

4 minutes read.

Ionic vs Apache Cordova

Ionic Vs Apache Cordova Ionic and Cordova are two popular technology going on in the market for mobile application development. The combination of these two technologies creates a development platform that is known as...

3 minutes read.

Ionic Geolocation

In today’s time, the mobile location plays an essential role in the mobile application. The Ionic Cordova geolocation plugin provides information about the location of the device, like as latitude and longitude. The...

5 minutes read.

Ionic List

Ionic List The ionic list is created of several rows of items that can contain text, buttons, thumbnails, keys, icons, and more. Lists are one of the essential components in any web or...

4 minutes read.

Ionic Navigation and Routing

The latest version (ionic 4) of the ionic framework, no longer limited to the angular. In Ionic 4, you can create an ionic project in different front-end frameworks such as Vue.js, React.js, and...

6 minutes read.

Ionic progress bar

The ionic progress bar element is a horizontal progress bar used to visualize the progression of operation and activity. You can use the <ion-progress-bar> component to create a progress bar in the ionic...

4 minutes read.

Ionic vs Xamarin

Ionic vs Xamarin Ionic and Xamarin are both the most popular frameworks used to develop a hybrid mobile application. Both frameworks are used in large enterprises by the developers. Here, we will discuss the...

4 minutes read.

Ionic Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 minutes read.

Ionic Segment

A segment is a collection of buttons, sometimes which are known as segmented controls. The segment buttons are displayed at horizontal rows. These segment buttons can be shown in a toolbar or the...

2 minutes read.

Ionic Cards

Ionic Cards: Ionic cards are a standard part of the user interface that acts as an entry point for more specific information. These are the best components for displaying information...

4 minutes read.

Ionic Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Ionic Alert

Ionic Alert An Alert is a dialog box that presents the users with important information, or it stores the data from the user by using the inputs. Alerts are a great way to provide...

5 minutes read.

Ionic Spinner

The ionic spinner component provides the various SVG (Scalable Vector Graphic) spinners. Ionic spinners are the visual indicators that display the app which is loading content or performing another process that the user...

5 minutes read.

Ionic Editors

Editors are software programs that permit the users to create and edit the text files. In the development field, editors are usually used to create the source code editors that includes many specific...

3 minutes read.

Ionic Grid

The Ionic grid is a very powerful mobile-first flexbox system for building the custom layouts. It is a CSS feature supported by all devices that ionic supports. The Ionic grid is composed of...

11 minutes read.

Ionic Camera

Hybrid mobile applications can make very quickly, but sometimes it becomes a challenge to get the features that access the hardware such as GPS, storage, file storage, cameras, and networks. Cordova offers plugins...

4 minutes read.