Wall's Corners Wall's Corners Author
Title: Use The Android And iOS Camera With Ionic Framework
Author: Wall's Corners
Rating 5 of 5 Des:
Most smart phones on the market have at least one camera if not two.  You may want to leverage these cameras to make the next Instagram or S...

Most smart phones on the market have at least one camera if not two.  You may want to leverage these cameras to make the next Instagram or SnapChat type application.  Lucky for us, the native Android and iOS camera can be easily accessed using Ionic Framework and the AngularJS extension set, ngCordova.

The following will help you add camera functionality into your latest creation.

Let’s start by creating a new Ionic project with the Android and iOS platforms:

Remember that you must be using a Mac if you wish to build for iOS.

The next thing we want to do is add the Apache Cordova camera plugin.  This can be done by running the following:

For this tutorial we are going to be using the AngularJS extension set for Apache Cordova called ngCordova.  Start by downloading the latest release of ngCordova and placing the ng-cordova.min.js file in your project’s www/js directory.

Next we need to include this file in our project’s code.  Open the index.html file and include the script before the cordova.js line like below:

This will include the library into our project, but now we need to include it for use in AngularJS.  Open your app.js file and alter the angular.module line to look like the following:

Now we’re ready for the fun stuff.  Inside your app.js file, we need to add a controller with a method that will launch the camera.  The following was copied almost exactly from the ngCordova documentation:

Notice on line 17, I added a bunch of other data to my scope.  Because our destination type is DATA_URLwe will be returning raw camera data rather than a file.  By adding data:image/jpeg;base64 we can use an HTML img tag to display our freshly created snapshot.

Just like that, you can call takePicture() from your UI and initialize the native Android and iOS camera in Ionic Framework.

A video version of this article can be seen below.

 

Share This:

View more at: http://bit.ly/1XReQFr

About Author

Advertisement

Post a Comment

 
Top