Reduce size of Bitmap to some specified pixel in Android
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);}
Use this Method
/** getResizedBitmap method is used to Resized the Image according to custom width and height * @param image * @param newHeight (new desired height) * @param newWidth (new desired Width) * @return image (new resized image) * */public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { int width = image.getWidth(); int height = image.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, matrix, false); return resizedBitmap;}
or you can do it like this:
Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter);
Passing filter = false will result in a blocky, pixellated image.
Passing filter = true will give you smoother edges.