Reputation: 17393
How to reduce bitmap quality without affecting it's size?
this is my recursive function to get :
private String compressIv1(Bitmap b , int quality){
long size_on_disk = 0;
ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, quality, byteArrayOutputStream1);
byte[] byteArray1 = byteArrayOutputStream1 .toByteArray();
App.registerBitmapModel.bitmapSize1 = byteArray1.length/1024;
size_on_disk = byteArray1.length/1024;
Log.e("size_on_disk","=>"+size_on_disk);
Bitmap temp_b = BitmapFactory.decodeByteArray(byteArray1, 0, byteArray1.length);
if(quality <= 0)
return "";
if(size_on_disk >= 1000)
{
quality = quality - 10;
compressIv1(temp_b,quality);
}else{
encodedImg1 = "data:image/jpeg;base64," + Base64.encodeToString(byteArray1, Base64.DEFAULT);
return encodedImg1;
}
return "";
}
but I get the same size:
Log.e("size_on_disk","=>"+size_on_disk);
Upvotes: 2
Views: 433
Reputation: 4304
Because PNG
format is lossless, it will ignore the quality setting, so the result of your compress is always the same. Here is the docs:
quality - int: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
Upvotes: 1