Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between compressing image and resizing image ? android

in my android app , i want to upload image to the server. the problem that the server will not accept images with size larger than 2M. but the user can choose an image larger than 2M.

so I want to build a code that will make the image smaller than 2M.

I have two approaches :

  1. to resize the image dimensions. as below :

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    
  2. also I can compress the image

    image.compress(Bitmap.CompressFormat.PNG, 10, fos);
    

what the difference between these two approaches ?

like image 781
david Avatar asked Sep 15 '25 13:09

david


1 Answers

Image re sizing means you're going to shorten the resolution of the image. suppose user selects a 1000*1000 px image. you're going to convert the image into a 300*300 image. thus image size will be reduced.

And Image compression is lowering the file size of the image without compromising the resolution. Of course lowering file size will affect the quality of the image. There are many compression algorithm available which can reduce the file size without much affecting the image quality.

like image 97
Faysal Ahmed Avatar answered Sep 17 '25 06:09

Faysal Ahmed