Resizing a Bitmap
December 25, 2009
Hey everyone,
Below is a snippet of code that will allow you to resize a Bitmap.
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); int height = bm.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(bm, 0, 0, width, height, matrix, false); return resizedBitmap; }
Pretty self explanatory – simply input the original Bitmap object and the desired dimensions of the Bitmap, and this method will return to you the newly resized Bitmap!
– jwei
18 Comments
leave one →
android.graphics.Bitmap.createScaledBitmap should do the same.
and with “filter = true” – much better. Thanks a lot.
Thanks david, that’s much better!
Excellent work… thanks for saving my time
thank you. This blog has help me a lot
Thank you….!
hi,
Thanks for the funciton. It perfectly worked when I used the same but it is making my photo blur. I want to take high Quality picture from Camera and save it in file of format .png.
But I am not able to save it to high resolution. It is only taking picture of my android phone size. π¦ Any tips?
same issue, have you been able to resolve this yet?
great job dude!!
To resolve the Photo blur, always give the original bitmap as an input. Change only the width and height of the Bitmap to resize
Hi. I tried android.graphics.Bitmap.createScaledBitmap method, but when i use it, my bitmap is scaled but the image’s ratios change.
hello, when i convert byte to biitmap it, returns null , i have a bitmap and compare to other in database so i have to convert byte to bitmap in order to compare it, can someone help me please
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
Thank you
Thank you very much for this code π
good job.. but it giving blur effect . and i am using the orignal image which is of size 1280×800 and i want to convert it to 1024×600. but on result image is giving too much blur