Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Cordova

How to Access and Use Your Mobile Phone Camera Using Ionic 2 and Ionic Native

5.00/5 (1 vote)
7 Nov 2016CPOL2 min read 10.4K  
In this post ,we are going to see how to use the mobile phone camera in mobile apps built using the Ionic 2(2.0.0-rc.1) Framework.

In this post, we are going to see how to use the mobile phone camera in mobile apps built using the Ionic 2(2.0.0-rc.1) Framework. To access the camera, we use the Cordova Camera plug in just like Ionic 1 but unlike Ionic 1 in Ionic 2, we can (preferably) use Ionic native to interface with Cordova plugins so how to use Ionic Native and the Cordova camera plug in to build apps that use the phone Camera.

Getting Started

Let's start by generating a new app using the blank template:

Bash
ionic start sayCheese blank --v2 --id "com.techiediaries.sayCheese"

Make sure you add the –v2 switch so the Ionic cli generates an app based on Ionic 2 not Ionic 1.

How to access and use your mobile phone camera using Ionic 2 and Ionic Native

Now enter in your app project and ionic serve it.

Bash
cd sayCheese
ionic serve

If everything is OK, you should see your blank app served with your default browser.

You can’t use the Cordova plugins in the browser, you need an actual plug in so go ahead and plug in your mobile phone to your computer with a USB cable and enter.

Bash
ionic run android

If you are developing with a MAC, you can also build apps for iOS so instead of Android, you can use iOS.

Bash
ionic run ios

How to access and use your mobile phone camera using Ionic 2 and Ionic Native

Next, you should add your target platform with:

Bash
ionic platform add android

Please note that if this is your first time using Cordova, make sure you first install it via npm:

Bash
npm install -g cordova

Now after adding your platform, you need to add the camera plug in:

Bash
ionic plug in add plug in

Now after installing the Android platform and the camera plug in, let's start writing the code for accessing the camera and taking a nice picture:

How to access and use your mobile phone camera using Ionic 2 and Ionic Native - App Anatomy

Go ahead and open home.ts inside your app src/pages/home directory and write this code to access the camera using Ionic native.

JavaScript
import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import {Camera} from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {
  public myImage:string;
  constructor(public navCtrl: NavController,public ngzone: NgZone) {

       this.myImage = "https://placehold.it/150x150";
  }

  public takePic(){

    Camera.getPicture({

            quality : 75,
            destinationType : Camera.DestinationType.DATA_URL,
            sourceType : Camera.PictureSourceType.CAMERA,
            allowEdit : false,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            saveToPhotoAlbum: true

    }).then( (imageData) => {

        this.myImage = "data:image/jpeg;base64," + imageData;

    }, (err) => {

        alert(err);
    });
  }
}

Now let's understand the code.

First, we imported the Camera class from ionic-native.

JavaScript
import {Camera} from 'ionic-native';

Then, we declared a string attribute with the name myImage which will hold the URL of the image.

Next in the constructor, we initialized myImage with an image from placehold.it.

JavaScript
this.myImage = "https://placehold.it/150x150";

Next, we added a public method takePic() which holds the logic to take a picture using Camera.getPicture which takes an options object defining properties of the image to take and other settings for the camera, most important we tell the camera to return image data in base64 format with destinationType: Camera.DestinationType.DATA_URL.

JavaScript
Camera.getPicture({

        quality : 75,
        destinationType : Camera.DestinationType.DATA_URL,
        sourceType : Camera.PictureSourceType.CAMERA,
        allowEdit : false,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        saveToPhotoAlbum: true

}).then( (imageData) => {

    this.myImage = "data:image/jpeg;base64," + imageData;

}, (err) => {

    alert(err);
});

This method returns a promise, if the operation is successful, we assign the image data(base64) to myImage, otherwise we just show the error.

That’s all now just in your terminal enter:

Bash
ionic run android

Don’t forget to plug in your mobile device.

And take a picture, if everything goes as expected, you should see your picture in the place of the placehold.it image. If anything goes wrong, just drop me a comment below.

Thanks for reading!

The post How to access and use your mobile phone camera using Ionic 2 and Ionic Native appeared first on techiediaries.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)