Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a bitmap to a fixed value but without changing the aspect ratio

I'm looking for a solution for the following problem: how to change the size of a Bitmapto a fixed size (for example 512x128). The aspect ratio of the bitmap content must be preserved.

I think it should be something like this:

  • create an empty 512x128 bitmap

  • scale the original bitmap down to fit the 512x128 pixels with keeping the aspect ratio

  • copy the scaled into the empty bitmap (centered)

What is the simplest way to achieve this?

The reason for all this is, that the GridView messes the layout up when the aspect ratio of an image differs from the other. Here is a screenshot (all images except the last one have the aspect ratio of 4:1):

screenshot

like image 346
Dark.Rider Avatar asked Feb 27 '13 22:02

Dark.Rider


People also ask

How do you keep aspect ratio when resizing?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

Can bitmaps be resized without experiencing any distortion?

Bitmaps can be resized without experiencing any distortion. Draw programs store images as bitmaps. Marble is not a desirable material for making sculptures because it is not very durable. Sculptors primarily use visual texture in their works.


2 Answers

Try this, calculate the ratio and then rescale.

private Bitmap scaleBitmap(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();

    Log.v("Pictures", "Width and height are " + width + "--" + height);

    if (width > height) {
        // landscape
        float ratio = (float) width / maxWidth;
        width = maxWidth;
        height = (int)(height / ratio);
    } else if (height > width) {
        // portrait
        float ratio = (float) height / maxHeight;
        height = maxHeight;
        width = (int)(width / ratio);
    } else {
        // square
        height = maxHeight;
        width = maxWidth;
    }

    Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);

    bm = Bitmap.createScaledBitmap(bm, width, height, true);
    return bm;
}
like image 76
Coen Damen Avatar answered Oct 10 '22 01:10

Coen Damen


The answer by Coen Damen doesn't always respect Max Height and Max Width. Here's an answer that does:

 private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
    if (maxHeight > 0 && maxWidth > 0) {
        int width = image.getWidth();
        int height = image.getHeight();
        float ratioBitmap = (float) width / (float) height;
        float ratioMax = (float) maxWidth / (float) maxHeight;

        int finalWidth = maxWidth;
        int finalHeight = maxHeight;
        if (ratioMax > 1) {
            finalWidth = (int) ((float)maxHeight * ratioBitmap);
        } else {
            finalHeight = (int) ((float)maxWidth / ratioBitmap);
        }
        image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
        return image;
    } else {
        return image;
    }
}
like image 22
joaomgcd Avatar answered Oct 10 '22 01:10

joaomgcd