简体   繁体   中英

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

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM