what is the difference between compressing image a

2019-05-23 10:03发布

问题:

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 tow approaches :

  1. tow 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);

}

  1. also i can compress the image

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

what the difference between this tow approaches ?

回答1:

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.