0

I am converting image to Base64 to send it to the server, But the converted string is a huge string. If the converted image is ~100kb then the converted base64 value string is ~1mb...

My code...

  protected String doInBackground(Void...arg0) {
        Cursor cursor = mydb.getDat1();
        //fetching the image location
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            for( int i=0 ;  i< 1 ; i++ )
            {
                if( cursor.getColumnName(i) != null )
                {
                    try
                    {

                        if( cursor.getString(i) != null )
                        {
                            //saving image to bitmap
                             Bitmap bitmap = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(DBHelper.PHOTO)));
                           //converting it to base64
                             String en= encodeToBase64( resize(bitmap,1080,1920), Bitmap.CompressFormat.JPEG,100);

                            Log.d("base",en);
                            //inserting it to table pic
                            mydb.insertpic(cursor.getInt(1),en);
                        }

                    }
                    catch( Exception ignored)
                    {

                    }
                }
            }
            cursor.moveToNext();
        }

        cursor.close();
        mydb.updatebin();
        return null;
    }


private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {

    //for conerting images to lower resolution
    if (maxHeight > 0 && maxWidth > 0) {
        int width = image.getWidth();
        int height = image.getHeight();
        float ratioBitmap = (float) width / (float) height;
        float ratioMax = (float) maxWidth / (float) maxHeight;
        int finalWidth = maxWidth;
        int finalHeight = maxHeight;
        if (ratioMax > 1) {
            finalWidth = (int) ((float)maxHeight * ratioBitmap);
        } else {
            finalHeight = (int) ((float)maxWidth / ratioBitmap);
        }
        image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
        return image;
    } else {
        return image;
    }
}

   public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    //converting image to base 64
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

How can i resolve this issue.?

2 Answers2

1

This is the code:

Code to fetch image :

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                String encodedImage = encodeImage(selectedImage);
            }

Try to compress and then convert(Bitmap)

 private String encodeImage(Bitmap bm)
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
        byte[] b = baos.toByteArray();
        String encImage = Base64.encodeToString(b, Base64.DEFAULT);

        return encImage;
    }
W4R10CK
  • 5,300
  • 2
  • 15
  • 28
Raut Darpan
  • 1,452
  • 6
  • 14
0

My guess is that the encondeToBase64() is creating a large image file, since you resize the original image to 1920x1080 and convert to a jpeg with quality 100, and then the file then grows around 1.333... times in size when you convert to base64.

  • can you explain.? –  Jan 06 '17 at 10:33
  • You first resize the image to `1920x1080` and then you do `image.compress(compressFormat, quality, byteArrayOS);` which will convert your image to a jpeg with quality 100 since those are the parameters you sent to `encodeToBase64`. I don't know what you mean by "the converted image is ~100kb". Do you mean that you printed the length of the image after `image.compress()`? –  Jan 06 '17 at 10:43