Rotate image(bitmap) to any angle in Android

In many image related Android applications, we may need to rotate the bitmap(not ImageView). Here I added the code below to rotate the image at any possible angle using Matrix.

rotation of image resulted from below code
Bitmap bInput/*your input bitmap*/, bOutput;
float degrees = 45;//rotation degree
Matrix matrix = new Matrix();
bOutput = Bitmap.createBitmap(bInput, 0, 0, bInput.getWidth(), bInput.getHeight(), matrix, true);

8 thoughts on “Rotate image(bitmap) to any angle in Android

  1. Brazil November 4, 2018 / 6:59 pm

    Thank you very much for your code. So simple, clean and works! I was reading some similar code in SO, but they were all so confusing.

  2. Pedro Arthur May 19, 2019 / 2:20 am

    ” Cannot resolve symbol ‘bOutput’ ”
    how can i fix it ?

    • dakshbhatt May 24, 2019 / 5:15 am

      Please make sure you have the following line in your code.

      Bitmap bInput/*your input bitmap*/, bOutput;

      It defines the ‘bOutput’ variable.

  3. N_E_Dub July 18, 2019 / 10:02 am

    When rotated by 45deg, the image shrinks to fit the bitmap. Is there a way that the image can be rotated but remain the same size? Even if it means portions of the image get cropped out?

    • dakshbhatt August 1, 2019 / 6:58 am

      For that, you need to calculate new height and width for that according to the angle I guess.

  4. Martin Ignacio Backhaus May 30, 2020 / 8:33 pm

    Were do i need to put this code?

    • dakshbhatt May 30, 2020 / 10:27 pm

      You can use this where you want to rotate the image in the code.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s