Created
January 25, 2016 18:40
-
-
Save thdaraujo/dd653423969781db44ad to your computer and use it in GitHub Desktop.
Add camera plugin on ionic project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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