Search code examples
androidbitmapscale

How to scale a large bitmap to a squared bitmap


After I take a photo I want to view it in a square. I follow this guide, but the bitmap created is rectangular, it creates a scaled image. My question is: what should I change in that code to create a scaled squared image?


Solution

  • I don't think it's possible to do this using BitmapFactory.Options.

    A simple way to do it (this creates another bitmap so will use up memory) is:

        Bitmap output;
        if (bitmap.getWidth() >= bitmap.getHeight())
            output = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2 - bitmap.getHeight() / 2, 0, bitmap.getHeight(), bitmap.getHeight());
        else
            output = Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2 - bitmap.getWidth() / 2, bitmap.getWidth(), bitmap.getWidth());