-
-
Save brianleroux/2174497 to your computer and use it in GitHub Desktop.
PhoneGap Convert Photo File URI to Data URI
This file contains hidden or 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
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