Skip to content

Instantly share code, notes, and snippets.

@jhiemer
Created July 29, 2014 20:31
Show Gist options
  • Save jhiemer/815bcd5d2fc22b77e20d to your computer and use it in GitHub Desktop.
Save jhiemer/815bcd5d2fc22b77e20d to your computer and use it in GitHub Desktop.
services.factory('camera', ['$rootScope', '$q', 'ENV', function($rootScope, $q, ENV) {
return {
getPicture: function(options, fromLibrary) {
// init $q
var deferred = $q.defer();
if (ENV.browser) {
// create file input without appending to DOM
var fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.onchange = function() {
var file = fileInput.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function () {
$rootScope.$apply(function() {
// strip beginning from string
var encodedData = reader.result.replace(/data:image\/jpeg;base64,/, '');
deferred.resolve(encodedData);
});
};
};
fileInput.click();
} else {
if (fromLibrary) {
options = {
quality: 30,
quality: 75,
targetWidth: 320,
targetHeight: 320,
saveToPhotoAlbum: false,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
};
} else {
options.sourceType = Camera.PictureSourceType.CAMERA;
}
options = angular.extend(defaultOptions, options);
var success = function(imageData) {
$rootScope.$apply(function() {
deferred.resolve(imageData);
});
};
var fail = function(message) {
$rootScope.$apply(function() {
deferred.reject(message);
});
};
navigator.camera.getPicture(success, fail, options);
}
return deferred.promise;
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment