Skip to content

Instantly share code, notes, and snippets.

@Studira
Created January 30, 2017 08:39
Show Gist options
  • Save Studira/78452c84dc9bd58e6d6c428341b19048 to your computer and use it in GitHub Desktop.
Save Studira/78452c84dc9bd58e6d6c428341b19048 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
/**
* @ngdoc object
* @name camera.cameraDevice.drve
* @description
* Directive to add camera functionality to the html page
*/
var module = angular.module('Camera');
var injectables = [ '$window', '$parse', 'lodash', '$cordovaCamera', 'camera.fctr', 'notify.fctr', 'Camera.cnst', '$spinner', '$ionic'];
function directive($window,$parse,_, Camera, Service, Notify, Constant, $spinner,$ionic){
function link($scope,$elem,$attrs){
$ionic.Platform.ready(function () {
var deviceCam = $window.Camera || null;
if(_.isObject(deviceCam)){
var type = $parse($attrs.imageOptions)($scope).type || 'jpg';
var options = {
quality: 30,
destinationType: deviceCam.DestinationType.DATA_URL,
sourceType: deviceCam.PictureSourceType.CAMERA,
allowEdit: false,
encodingType: Service.getImgTypeId(type),
targetWidth: Constant.camera.image.size.width,
targetHeight: Constant.camera.image.size.height,
saveToPhotoAlbum: true,
};
$scope.cameraOptions = $parse($attrs.cameraOptions)($scope);
$scope.takePhoto = function(){
if(_.isObject(Camera) && _.has(Camera,'getPicture') ){
$spinner.set(true,$scope.cameraOptions.secondaryColor ? true : null);
return Camera.getPicture(options)
.then(function(image){
image = {
type: type,
typeId: Service.getImgTypeId(type),
src: "data:image/" + _.toLower(type) + ";base64," + image,
context: 'camera'
};
return Service.saveImgToLocal(image);
})
.then(function(image){
$scope.imageStyleCamera = {'background-image':'url(' + image.src + ')','background-repeat':'no-repeat','background-size':'cover'}
$scope.imageCamera = true;
$spinner.set(false);
$scope.$emit('camera:image:captured',image)
Notify.show('Photo captured','default');
return image;
})
.catch(function(err){
$spinner.set(false);
console.log('Error: ',err);
return err;
});
}
};
$scope.removeImageCamera = function(){
Service.removeImgFromLocal();
$scope.imageStyleCamera = null;
$scope.imageCamera = false;
}
$scope.$on('camera:image:remove',function(){
$scope.removeImageCamera();
})
}
}, false);
}
return ({
restrict: 'A',
replace: true,
templateUrl: 'app/components/camera/tpl/camera.cameraDevice.html',
link: link,
});
}
directive.$inject = injectables;
module.directive('cameraDevice',directive);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment