Reduce size of Bitmap to some specified pixel in Android

前端 未结 4 2259
北荒
北荒 2020-12-03 02:30

I would like to reduce My Bitmap image size to maximum of 640px. For example I have Bitmap image of size 1200 x 1200 px .. How can I reduce it to 640px.

4条回答
  •  死守一世寂寞
    2020-12-03 03:08

    Here is working code to reduce Bitmap image resolution (pixels) to desired value...

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class ImageProcessActivity extends AppCompatActivity {
    
    
        private static final String TAG = "ImageProcessActivity";
        private static final String IMAGE_PATH = "/sdcard/DCIM/my_image.jpg";
    
        public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
    
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }
    
        public static Bitmap decodeSampleDrawableFromFile(String file, int reqWidth, int reqHeight) {
    
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(file, options);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_process);
    
            new AsyncTask() {
    
                @Override
                protected Bitmap doInBackground(Object... objects) {
    
                    try {
    
                        return decodeSampleDrawableFromFile(IMAGE_PATH, 640, 640);
    
                    } catch (Exception e) {
    
                        e.printStackTrace();
    
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(Bitmap bitmap) {
                    super.onPostExecute(bitmap);
    
                    ((ImageView) findViewById(R.id.img)).setImageBitmap(bitmap);
                }
            }.execute();
        }
    }
    

    Steps:

    1. Get the Bitmap.Options (image info).

    2. Sample the size to desired sample size.

    3. Load to Bitmap with given options (desired resolution) from the image file into a bitmap object. But do this operation in a background thread.

    4. Load Bitmap image into ImageView on UI thread (onPostExecute()).

提交回复
热议问题