1

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 enter image description here

I'm using phonegap 2.2.0

Ryan
  • 5,644
  • 3
  • 38
  • 66
danielrvt-sgb
  • 1,118
  • 5
  • 17
  • 33

3 Answers3

0

Can you try to increase the Image Quality. I remember reading some android phones have issues if the quality is set to low. I know it is a long shot but worth the try :)

pehaada
  • 513
  • 2
  • 8
  • 20
0

I believe console.log has a limit as to the number of characters it can print. What happens when you set the data as the source of an image tag like:

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

Also, you may want to try writing the data out to a file.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
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!

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70