phonegap图片base64 android [英] phonegap picture base64 android

查看:204
本文介绍了phonegap图片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); //解码时打印的是不完整的图像
}



这是一个示例图像,当解码时: http://www.freeformatter.com/base64-encoder.html



我正在使用phonegap 2.2.0

解决方案

我在Android中面临同样的问题,确切的问题是当我编码图像到其相应的 Base64 &如果图像尺寸更大(2mb或更多...&也取决于图像质量和摄像机质量,可以从2MP或5MP或可能是8MP相机),那么它遇到问题,以将完整的图像转换为Base64 .. 。你必须减小关注图像的大小!我已经给了我的工作 Android代码,通过它我实现了it_

获取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);



转换为正确的位图 >

  / ** 
*我的减少位图大小的方法。
* /
private Bitmap decodeFile(String fPath){

//解码图像大小
BitmapFactory.Options opts = new BitmapFactory.Options();
//opts.inJustDecodeBounds = true;
opts.inDither = false; //禁用抖动模式
opts.inPurgeable = true; //告诉gc是否需要可用内存,可以清除位图
opts.inInputShareable = true; //哪种类型的引用将被用来恢复Bitmap数据在被清除后,何时将在未来使用
opts.inTempStorage = new byte [1024];

BitmapFactory.decodeFile(fPath,opts);

//新的大小,我们要缩放到
final int REQUIRED_SIZE = 70; //或者根据你的需要改变...

//查找正确的比例值。它应该是2的幂。
int scale = 1;
while(opts.outWidth / scale / 2> = REQUIRED_SIZE&& opts.outHeight / scale / 2> = REQUIRED_SIZE)
scale * = 2;

//使用inSampleSize进行解码
opts.inSampleSize = scale;

return BitmapFactory.decodeFile(fPath,opts);

}

我希望这将有助于其他面对同样的朋友问题...谢谢!


I want to send a picture taken from the camera to a server as a base64 string. My problem is that the picture gets corrupted somehow in the phone.

I have some console.logs to print the base64 string inside the success function of the camera.getPicture, and whenever I print the string and the decode the image, it only shows the top part, as if it was incomplete.

Here is my code:

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

and the success function:

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

What is wrong with this code?

This is a example image when decode with: http://www.freeformatter.com/base64-encoder.html

I'm using phonegap 2.2.0

解决方案

I have faced the same problem in android, What the exact problem is_ When i encode image into its corresponding Base64 & if the image size is more(2mb or more... & also depends on image quality & camera quality, may be taken from 2MP or 5MP or may be 8MP camera) then it encounter problem to convert full image into Base64... you must have to decrease the size of the concern image! I have give my working Android code through which i have achieved it_
Get Base64 image String

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

Convert to proper bitmap

       /**
        *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);

   } 

I hope this will help other buddies that are facing the same problem... Thanks!

这篇关于phonegap图片base64 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆