繁体   English   中英

phonegap图片base64 android

[英]phonegap picture base64 android

我想将从相机拍摄的照片作为base64字符串发送到服务器。 我的问题是照片在手机中被损坏了。

我有一些console.logs在camera.getPicture的成功函数中打印base64字符串,并且每当我打印字符串并对图像进行解码时,它仅显示顶部,就好像它不完整一样。

这是我的代码:

photo.capturePhoto = function(image_button_id) {
        navigator.camera.getPicture(function(image) {
            photo.onPhotoDataSuccess(image)
        }, onFail, {
            quality : 30,
            destinationType: destinationType.DATA_URL,
            correctOrientation : true
        });
    }

和成功功能:

photo.onPhotoDataSuccess = function(image) {
        console.log(image); //What this prints is an incomplete image when decoded
    }

此代码有什么问题?

这是使用以下代码解码时的示例图像: http : //www.freeformatter.com/base64-encoder.html 在此处输入图片说明

我正在使用phonegap 2.2.0

您可以尝试提高图像质量吗? 我记得读过一些Android手机,如果将其质量设置为低,则会出现问题。 我知道这是一个远景,但值得尝试:)

我相信console.log对于它可以打印的字符数有限制。 将数据设置为图像标签的来源时会发生什么,例如:

function onSuccess(imageData) {
    var image = document.getElementById('myImage');
    image.src = "data:image/jpeg;base64," + imageData;
}

另外,您可能想尝试将数据写出到文件中。

我在android中也遇到过同样的问题,确切的问题是_当我将图像编码到其对应的Base64 ,并且如果图像大小更大(2mb或更大...,并且还取决于图像质量和相机质量,可能取自2MP或5MP或8MP摄像头),则将完整图像转换为Base64时会遇到问题...您必须减小关注图像的大小! 我给出了可以实现的Android code工作Android code _
获取Base64图像字符串

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap mBitmap= new decodeFile("<PATH_OF_IMAGE_HERE>");
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        int i=b.length;
        String base64ImageString=android.util.Base64.encodeToString(b, 0, i, android.util.Base64.NO_WRAP);

转换为适当的位图

       /**
        *My Method that reduce the bitmap size.
        */
        private Bitmap decodeFile(String  fPath){

        //Decode image size
        BitmapFactory.Options opts = new BitmapFactory.Options();
        //opts.inJustDecodeBounds = true;
        opts.inDither=false;                     //Disable Dithering mode
        opts.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        opts.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        opts.inTempStorage=new byte[1024]; 

        BitmapFactory.decodeFile(fPath, opts);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;//or vary accoding to your need...

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(opts.outWidth/scale/2>=REQUIRED_SIZE && opts.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
         opts.inSampleSize=scale;

        return BitmapFactory.decodeFile(fPath, opts);

   } 

希望这对其他面临相同问题的伙伴有所帮助...谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM