Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce size of Bitmap to some specified pixel in Android

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.

like image 404
Nav Ali Avatar asked Apr 02 '13 08:04

Nav Ali


People also ask

Can you resize a bitmap?

❓ 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 you handle bitmaps in Android as it takes too much memory?

Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.


1 Answers

If you pass bitmap width and height then use:

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) {     return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true); } 

If you want to keep the bitmap ratio the same, but reduce it to a maximum side length, use:

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {         int width = image.getWidth();         int height = image.getHeight();          float bitmapRatio = (float) width / (float) height;         if (bitmapRatio > 1) {             width = maxSize;             height = (int) (width / bitmapRatio);         } else {             height = maxSize;             width = (int) (height * bitmapRatio);         }          return Bitmap.createScaledBitmap(image, width, height, true); } 
like image 198
Divyang Metaliya Avatar answered Sep 21 '22 19:09

Divyang Metaliya