Home > Software design >  Bitmap.createScaledBitmap not resizing the image twice the original size
Bitmap.createScaledBitmap not resizing the image twice the original size

Time:10-12

I am trying to resize an image twice its original size.

Say, a width of 623 and height of 415.

So i tried this code:

Bitmap resized_captured_image_bm = 
    Bitmap.createScaledBitmap(ogBitmap,(int)(ogBitmap.getWidth() * 2), 
        (int)(ogBitmap.getHeight() * 2), false);

But the above code does nothing to the image, the displayed image is still the same dimension size.

CodePudding user response:

Try to use next method:

public static final int MAX_FILE_ONE_SIZE = 200 * 1024;
public static final int MAX_SIZE_1 = 1280;
public static final int MAX_SIZE_2 = 960;

public static Bitmap resizeBitmap(Context context, Bitmap bitmap) {
        Bitmap result = null;

        if(bitmap != null){
            int cnt = 0;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            result = bitmap.copy(bitmap.getConfig(), true);

            while(getImageSize(result) >= MAX_FILE_ONE_SIZE || !canSendBitmapResolution(result)){
                result.recycle();
                width = width/2;
                height = height/2;
                result = Bitmap.createScaledBitmap(bitmap, width, height, true);

                if(cnt   > 10) {
                    result = null;
                    break;
                }
            }
            bitmap.recycle();
        }

        return result;
    }

public static boolean canSendBitmapResolution(Bitmap bitmap){
        boolean result = false;

        if((bitmap.getWidth() <= MAX_SIZE_1 && bitmap.getHeight() <= MAX_SIZE_2)
            || (bitmap.getWidth() <= MAX_SIZE_2 && bitmap.getHeight() <= MAX_SIZE_1)){
            result = true;
        }

        return result;
    }

CodePudding user response:

The problem is that the scale ratio doesn't match.

you just need to figure out what ratio to use and scale accordingly.

Ex:

final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = myBitmap.getWidth();
int inHeight = myBitmap.getHeight();
if(inWidth > inHeight){
    outWidth = maxSize;
    outHeight = (inHeight * maxSize) / inWidth; 
} else {
    outHeight = maxSize;
    outWidth = (inWidth * maxSize) / inHeight; 
}

Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);

or, follow by google docs: Loading large bitmaps

  • Related