19

I am trying to rotate a bitmap image 90 degrees to change it from a landscape format to a portrait format. Example:

[a, b, c, d]
[e, f, g, h]
[i, j, k, l]

rotated 90 degree clockwise becomes

[i,e,a]
[j,f,b]
[k,g,c]
[l,h,d]

Using the code below (from an online example) the image is rotated 90 degrees but retains the landscape aspect ratio so you end up with a vertically squashed image. Am I doing something wrong? Is there another method I need to use? I am also willing to rotate the jpeg file that I'm using to create the bitmap if that is easier.

 // create a matrix for the manipulation
 Matrix matrix = new Matrix();
 // resize the bit map
 matrix.postScale(scaleWidth, scaleHeight);
 // rotate the Bitmap
 matrix.postRotate(90);

 // recreate the new Bitmap
 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOriginal, 0, 0, widthOriginal, heightOriginal, matrix, true); 
user999764
  • 333
  • 1
  • 3
  • 6
  • Not sure if I understand your question correctly. Do you want to rotate the screen layout when the orientation changes? Or do you want to change the screen layouyt even though the orientation does not change? (Android automatically rotate the screen elements when the orientation changes!) – Ahmed Faisal Dec 22 '11 at 19:42
  • So I want to change the bitmap data itself. In my app the camera is storing the picture improperly so that even if a picture is taken in the "portrait" orientation is still loads it in "landscape" orientation. I am able to detect the true orientation via the file's EXIF data and I want to rotate the bitmap to the proper orientation. I don't want to just change how the bitmap is displayed but change the actual data so that after I modify the image it can be saved properly. – user999764 Dec 22 '11 at 19:53
  • Does this answer your question? [how to rotate a bitmap 90 degrees](https://stackoverflow.com/questions/9015372/how-to-rotate-a-bitmap-90-degrees) – Vega Feb 27 '21 at 11:07
  • This is what I'm also looking for and every single answer does a W x H -> W x H "rotation" (quotations intended), whereas I'd need W x H -> H x W. Not just this question but I browsed two dozen other questions too. Matrix rotation is great for non 90 degree angles. 90 degree angle is just switch of the axes, no need to matrix transform, just the indexing needs to be flipped. So far I haven't found a sound solution. – Csaba Toth Jan 24 '23 at 22:40

4 Answers4

41

This is all you need to rotate the image:

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(original, 0, 0, 
                              original.getWidth(), original.getHeight(), 
                              matrix, true);

In your code sample, you included a call to postScale. Could that be the reason your image is being stretched? Perhaps take that one out and do some more testing.

SharkAlley
  • 11,399
  • 5
  • 51
  • 42
  • If you want to make a rotated image with height more than original (like it must be), you will see an exception `java.lang.IllegalArgumentException: y + height must be <= bitmap.height()`. So, don't make bitmap larger than original. – CoolMind Nov 16 '16 at 18:26
  • How is this a W x H -> H x W rotation if the source and the destination dimensions are the same??? – Csaba Toth Jan 24 '23 at 22:37
13

Here's how you would rotate it properly (this insures proper rotation of the image)

public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();

        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(
                    b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
           throw ex;
        }
    }
    return b;
}
Tolga E
  • 12,188
  • 15
  • 49
  • 61
  • no problem, when i was looking into it, i did it the way you had it (probably used the same example) eventually came up with the code i posted for proper rotation – Tolga E Dec 22 '11 at 20:09
  • hmmm, it's rotating but still getting squashed vertically. The same as before. – user999764 Dec 23 '11 at 15:41
  • What are you displaying it in? Could it have something to do with what you are displaying it in.. It's gotta be something with your containers because i'm sure by the end the "b" that the method is returning is rotated properly, and that is a guarentee :) – Tolga E Dec 23 '11 at 19:13
  • Works great. Squashed problem is unrelated to the rotation code. – theJosh Feb 08 '12 at 19:54
  • How is this a W x H -> H x W rotation if the source and the destination dimensions are the same??? – Csaba Toth Jan 24 '23 at 22:37
0

This code worked great for me:

                Matrix matrix = new Matrix();

            matrix.setRotate(90, 0, 0);
            matrix.postTranslate(original.getHeight(), 0);

            rotatedBitmap = Bitmap.createBitmap(newWidth, newHeight, original.getConfig());
            Canvas tmpCanvas = new Canvas(rotatedBitmap);
            tmpCanvas.drawBitmap(original, matrix, null);
            tmpCanvas.setBitmap(null);
EyalBellisha
  • 1,874
  • 19
  • 28
0

check the canvas size where you draw the bitmap, maybe your canvas is still landscape, so only the square part of the rotated bitmap could be seen.