在发送到服务器之前调整cordova.camera映像的大小

时间:2015-10-31 23:39:33

标签: javascript image cordova canvas data-uri

我尝试调整使用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

0 个答案:

没有答案