Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap compress doesn't changing bitmap byte size

Tags:

android

bitmap

I'm trying to reduce bitmap size by using 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 is the compress does 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;
    }

Still the same byte count

11-28 11:33:04.335: I/before compress(3472): 374544
11-28 11:33:04.395: I/after compress 2(3472): 374544
like image 964
dasdasd Avatar asked Nov 28 '13 09:11

dasdasd


People also ask

How do I compress the size of a bitmap file?

❓ How can I compress a BMP image? First, you need to add a BMP image file: drag & drop your BMP image file or click inside the white area to choose a file. Then adjust compression settings, and click the "Compress" button. After the process completes, you can download your result file.

Does the size of bitmap changes with change in gate count?

The size of bitmap is irrespective of resource utilization, it is always the same,for Spartan xc3s5000 it is 1.56MB and will never change.

What is quality in bitmap compress?

The documentation has this for the quality parameter to compress() : 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. If you compress() to a PNG, the quality value is ignored.


1 Answers

Following is the code for reducing the bitmap size 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);
like image 199
Kanifnath Modak Avatar answered Sep 30 '22 09:09

Kanifnath Modak