Source: StackOverflow
Question: How to scale Bitmap
without losing much quality
Answer: Use Matrix
instead of Bitmap.createScaledBitmap()
/**
* @param bitmap the Bitmap to be scaled
* @param threshold the maxium dimension (either width or height) of the scaled bitmap
* @param isNecessaryToKeepOrig is it necessary to keep the original bitmap? If not recycle the original bitmap to prevent memory leak.
* */
public static Bitmap getScaledDownBitmap(Bitmap bitmap, int threshold, boolean isNecessaryToKeepOrig){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = width;
int newHeight = height;
if(width > height && width > threshold){
newWidth = threshold;
newHeight = (int)(height * (float)newWidth/width);
}
if(width > height && width <= threshold){
//the bitmap is already smaller than our required dimension, no need to resize it
return bitmap;
}
if(width < height && height > threshold){
newHeight = threshold;
newWidth = (int)(width * (float)newHeight/height);
}
if(width < height && height <= threshold){
//the bitmap is already smaller than our required dimension, no need to resize it
return bitmap;
}
if(width == height && width > threshold){
newWidth = threshold;
newHeight = newWidth;
}
if(width == height && width <= threshold){
//the bitmap is already smaller than our required dimension, no need to resize it
return bitmap;
}
return getResizedBitmap(bitmap, newWidth, newHeight, isNecessaryToKeepOrig);
}
private static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, boolean isNecessaryToKeepOrig) {
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);
if(!isNecessaryToKeepOrig){
bm.recycle();
}
return resizedBitmap;
}
return blurred image not a good solution