PhoneGap的图片的base64机器人 [英] phonegap picture base64 android

查看:254
本文介绍了PhoneGap的图片的base64机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发从相机拍摄到服务器为Base64字符串的图片。我的问题是,图像被在电话某种程度上损坏。

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.

我有一些console.logs打印camera.getPicture成功函数内以base64字符串,每当我打印字符串和去code的形象,那只能说明上半部分,就好像它是不完整的。

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.

下面是我的code:

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
    }

什么是错的这个code?

What is wrong with this code?

这是一个例子图像时去code有:的http:/ /www.freeformatter.com/base64-en$c$cr.html

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

我使用的PhoneGap 2.2.0

I'm using phonegap 2.2.0

推荐答案

我面临的Andr​​oid同样的问题,有什么确切的问题,确保对方当我连接code图像到其对应的的Base64 &安培;如果图像尺寸更(2MB或更多...&放大器;还取决于图像质量和相机的质量,可以从200万像素500万像素或取或可能是800万像素摄像头),那么遇到的问题完整的图像转换为Base64编码..你必须有减少的关注图像的大小!我给我的工作的Andr​​oid code ,通过它我已经实现了it_结果
获取图像的Base64字符串

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机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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