Bitmap compression does not change the size of the bitmap

I am trying to reduce the size of a bitmap using the compress method.

This is my code:

public Bitmap compressImage(Bitmap image) { Bitmap immagex = image; ByteArrayOutputStream baos = new ByteArrayOutputStream(); Log.i("before compress", immagex.getByteCount()+""); boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); if(compress) Log.i("after compress", immagex.getByteCount()+""); else Log.i("bad compress", "bad compress"); return immagex; } 

When I check my logs, I get:

 11-28 11:10:38.252: I/before compress(2429): 374544 11-28 11:10:38.262: I/after compress(2429): 374544 

Why does the compress not work?

UPDATE:

I tried this code:

 public Bitmap compressImage(Bitmap image) { Bitmap immagex = image; ByteArrayOutputStream baos = new ByteArrayOutputStream(); Log.i("before compress", immagex.getByteCount()+""); boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+""); return immagex; } 

However, the same byte

 11-28 11:33:04.335: I/before compress(3472): 374544 11-28 11:33:04.395: I/after compress 2(3472): 374544 
+6
source share
2 answers

Below is the code to reduce the size of the bitmap and convert it to base64,

  Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] byteArray = baos.toByteArray(); String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT); Log.e("Base 64 String", imageEncoded); 
+4
source

Using Bitmap.compress (), you simply specify the compression algorithm and, by the way, the compression operation takes quite a lot of time. If you need to play with sizes to reduce the memory allocation for your image, you need to use the scaling of your image using Bitmap.Options, first calculating the raster borders and then decoding it to the specified size.

The best sample I've found on StackOverflow is this one. Strange memory issue when loading image into Bitmap object

+1
source

Source: https://habr.com/ru/post/958975/


All Articles