Skip to content

Instantly share code, notes, and snippets.

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 brianleroux/2174497 to your computer and use it in GitHub Desktop.
Save brianleroux/2174497 to your computer and use it in GitHub Desktop.
PhoneGap Convert Photo File URI to Data URI
function getPhoto() {
navigator.camera.getPicture(onPhotoSuccess, onPhotoFail,
{quality: 70, targetWidth: 500, targetHeight: 500,
sourceType: navigator.camera.SourceType.PHOTOLIBRARY,
destinationType: navigator.camera.DestinationType.FILE_URI,
});
}
function onPhotoSuccess(imageUri) {
var $img = $('<img/>');
$img.attr('src', imageUri);
$img.css({position: 'absolute', left: '0px', top: '-999999em', maxWidth: 'none', width: 'auto', height: 'auto'});
$img.bind('load', function() {
var canvas = document.createElement("canvas");
canvas.width = $img.width();
canvas.height = $img.height();
var ctx = canvas.getContext('2d');
ctx.drawImage($img[0], 0, 0);
var dataUri = canvas.toDataURL('image/png');
$mealImg.attr('src', 'data:image/png;base64,' + imageDataUri);
});
$img.bind('error', function() {
console.log('Couldnt convert photo to data URI');
});
$('body').append($img);
}
function onPhotoFail(message) {
console.log(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment