Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Created January 25, 2016 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thdaraujo/dd653423969781db44ad to your computer and use it in GitHub Desktop.
Save thdaraujo/dd653423969781db44ad to your computer and use it in GitHub Desktop.
Add camera plugin on ionic project
/* WARNING: it won`t work on your browser/development, only works on your phone! */
//sources:
// http://stackoverflow.com/questions/27845345/ionic-cordova-camera-not-working
/* install ionic and cordova */
$ sudo npm install -g ionic cordova
/* create ionic app */
$ ionic start myApp
$ cd myApp
/* add camera plugin */
$ cordova plugin add cordova-plugin-camera
/* install ngCordova*/
$ bower install ngCordova
/* Include ng-cordova.js file before cordova.js and after AngularJS / Ionic file */
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
/* Inject module - www/js/ */
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])
/* inject controller dependency - $cordovaCamera */
module.controller('PictureCtrl', function($scope, $cordovaCamera) {
...
})
/* example */
.controller('PicturesCtrl', function($scope, $cordovaCamera, $ionicPlatform) {
$ionicPlatform.ready(function() {
var options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}, function(err) {
// error
});
});
});
/* view */
<img ng-show="imgURI !== undefined" ng-src="{{imgURI}}">
<img ng-show="imgURI === undefined" ng-src="http://placehold.it/300x300">
<button class="button" ng-click="takePicture()">Take Picture</button>
/* run! */
$ ionic serve
/* WARNING: it won`t work on your browser/development, only works on your phone! */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment