我尝试调整使用cordova.camera选择的图像以保存一些数据
function imageToDataUri(img, width, height) {
// create an off-screen canvas
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
// set its dimension to target size
canvas.width = width;
canvas.height = height;
var img1 = new Image;
img.onload = function(){
ctx.drawImage(img1, 0, 0, width, height);
};
img1.src = img;
// draw source image into the off-screen canvas:
var newUrl = canvas.toDataURL('image/jpeg', 0.9);
alert(img.length + ' optimizada' + newUrl.length);
// encode image to data-uri with base64 version of compressed image
return newUrl;
}
$('body').on('click','.takePicture',function(e){
navigator.camera.getPicture(
function (imageData) {
var src = imageToDataUri(imageData,1024,640);
var uploadedImage = $('<img>').attr('src', src).attr('alt', 'imageeeeeee!');
$('.takePictureHolder div').html(uploadedImage);
$('.takePictureHolder input').val(src);
},
null,
{
correctOrientation: true,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY
}
);
});
捕捉照片部分的问题在于imageToDataUri
会生成黑色图像,
知道我错过了什么吗?
我选择的图片的提醒提示1137795 optimizada 14515