Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bitmap resize

What is the best way of resizing a bitmap?

Using

options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mandy_moore, options);

or

Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
like image 893
John Victor Avatar asked Jan 27 '13 11:01

John Victor


People also ask

How do I resize a bitmap image?

❓ How can I resize a BMP image? First, you need to add a BMP image file: drag & drop your BMP image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.

How do I set the width and height of a bitmap in android programmatically?

scaleToFitWidth(bitmap, 100); public static Bitmap scaleToFitWidth(Bitmap b, int width) { float factor = width / (float) b. getWidth(); return Bitmap. createScaledBitmap(b, width, (int) (b. getHeight() * factor), true); } // Scale and maintain aspect ratio given a desired height // BitmapScaler.

How do you handle bitmaps in Android?

For most cases, we recommend that you use the Glide library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of the complexity in handling these and other tasks related to working with bitmaps and other images on Android.

How do I resize a JPEG on Android?

Tap the image you want to adjust. You can adjust the size of an image or rotate it: Resize: Touch and drag the squares along the edges.


1 Answers

What is the best way of resizing a bitmap?

It depends the flexibility you need:

options.inSampleSize = N; means that you will obtain an image which is N times smaller than the original. Basically, the decoder will read 1 pixel every N pixel.

Use that option if you don't need a particular size for your bitmap but you need to make it smaller in order to use less memory. This is particularly useful for reading big images.

Bitmap.createScaledBitmap on the other hand give you more control: you can indicate precisely the final dimension of the bitmap, ...

The best is to use a combination of both method:

  1. Determine the maximal value of inSampleSize you can use to decode the source image efficiently (such that the decoded image is still bigger than the final size you need)
  2. Use Bitmap.createScaledBitmap to precisely control the resulting size
like image 94
Vincent Mimoun-Prat Avatar answered Oct 24 '22 12:10

Vincent Mimoun-Prat