nothnk
4/19/2012 - 5:57 AM

PhoneGap Convert Photo File URI to Data URI

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);
}