Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what mean bitmap.compress() parameter 'quality'?

Tags:

android

In my app, i send some image to server. images max size is 300kb, i'll resize file.

this is my resize code.

public static void resize_filesize(Context context, Uri uri, int maxSize) {
    int filesize;  
    int compressRate;
    try {

        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        filesize = inputStream.available();
        inputStream.close();

        compressRate = ((100*maxSize) / filesize);

        File storageDir = new File(Environment.getExternalStorageDirectory() + context.getString(R.string.photoPath));
        File image = new File(storageDir,"resize.jpg");

        FileOutputStream outputStream = new FileOutputStream(image);
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        bitmap.compress(Bitmap.CompressFormat.JPEG, compressRate, outputStream);

        outputStream.close();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".provider", image);
        } else {
            uri = Uri.fromFile(image);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

i think this. if original image size is 500kb, i need 300kb. than i set quality:60, image will 60% compress and it become 300kb.

but i try it, so much compress work. this is my log.

original : 831210(811kb)
maxsize : 307200(300kb)
rate :  35
final size : 95053(92kb)

what happen???

ofcource i want resize, but it's too small!

what did i do wrong? 'quality' isnt mean % ? than how can i get 'near' maxsize image?

in android document, just say it mean compress size...

like image 374
Redwings Avatar asked Feb 22 '18 18:02

Redwings


People also ask

What is bitmap compression?

Compression reduces the disk and memory storage required for the bitmap. When the Compression member of the bitmap information header structure is BI_RLE8, a run-length encoding (RLE) format is used to compress an 8-bit bitmap. This format can be compressed in encoded or absolute modes.

What is bitmap compress in Android?

public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream) Write a compressed version of the bitmap to the specified outputstream. If this returns true, the bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory. decodeStream().

Is bitmap compressed format?

For most purposes standardized compressed bitmap files such as GIF, PNG, TIFF, and JPEG are used; lossless compression in particular provides the same information as a bitmap in a smaller file size. TIFF and JPEG have various options. JPEG is usually lossy compression.


1 Answers

i think this. if original image size is 500kb, i need 300kb. than i set quality:60, image will 60% compress and it become 300kb.

No.

in android document, just say it mean compress size...

No. 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. If you compress() to a JPEG, then it is used to control JPEG compression, which commonly uses a 0-100 range to express the desired quality. However, that does not mean that a value of 0 gives you a photo that takes no space.

than how can i get 'near' maxsize image?

There is no way of knowing the size of a JPEG compressed image without compressing it. Ideally, you do not worry about getting it "near" some particular compression ratio. Instead, either let the user choose the quality value, or just pick some quality value and move on.

like image 150
CommonsWare Avatar answered Oct 14 '22 15:10

CommonsWare