3

Am encoding an image from android to base64 with this code:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);

and than insert it into mysql data base, than decode it with php using that code:

$filename_path = md5(time().uniqid()).".jpeg"; 
$decoded=base64_decode($image_str);
 file_put_contents("uploads/".$filename_path,$decoded);
//echo '<img src="uploads/".$filename_path"/>';
echo '<img src="uploads/'.$filename_path.'"/>';

it's working all fine i can see the picture and there is no errors, but the problem is am losing like about 80% of the quality and the size , how can i fix that please.

Laidi Oussama
  • 108
  • 1
  • 12
  • 1
    It shouldn't affect the image quality. I'd look into this `bmp.compress(Bitmap.CompressFormat.PNG, 90, stream);` - I'm guessing `90` there is a quality setting. Try adjusting it to 100 and turn it down depending on what quality / file size is acceptable to you. – JimL Apr 03 '16 at 10:10
  • 2
    don't compress the image – Adnan Amjad Apr 03 '16 at 10:11
  • Stick with the original `png` format which uses a lossless compression. The moment you use a `jpg` compression, you lose quality. – arkascha Apr 03 '16 at 10:14
  • i changed it to 100 but the picture has the same quality and size – Laidi Oussama Apr 03 '16 at 10:28
  • "am losing 80% of the quality and the size" -- please edit your question and provide the code where you are getting this `Bitmap` and doing the comparison to determine your 80% metric. – CommonsWare Apr 03 '16 at 12:15
  • it's not an exact aprox, just tryed to show that am losing much quality and size , thanks for you'r advice i edited it . – Laidi Oussama Apr 03 '16 at 13:10
  • `insert it into mysql data base, than decode it with php`. No. Php first decodes the string. After that the image is saved to file system. Not to a database. – greenapps Apr 03 '16 at 15:43
  • exactly i misexplained , and i found that changing 90 to 100 is the best i cn do , thanks guys – Laidi Oussama Apr 04 '16 at 07:59

1 Answers1

1

I think if you make the compress format JPEG, and reduce the quality of the image to 50, that should do the trick:

ByteArrayOutputStream baos = new  ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte [] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
jacefarm
  • 6,747
  • 6
  • 36
  • 46
Wael AL-ameen
  • 11
  • 1
  • 2