Android Radio 90 degrees results in a crumpled image. Need real rotation between portrait and landscape

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

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

rotated 90 degrees clockwise becomes

[i, e, a]
[J, F, b]
[K, g, s]
[L, h, d]

Using the code below (from an online example), the image rotates 90 degrees, but retains the aspect ratio of the landscape, so you get a vertically compressed image. Am I doing something wrong? Is there any other method I need to use? I also want to rotate the jpeg file that I use to create a 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); 
+17
android rotation bitmap
Dec 22 '11 at 19:20
source share
4 answers

This is all it takes 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 sample code, you included the call to postScale. Maybe this is the reason why your image is stretched? Perhaps take this and do some more tests.

+41
Dec 31 '11 at 8:24
source share

Here's how you would rotate it correctly (this ensures that the image rotates correctly)

 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; } 
+13
Dec 22 '11 at 20:03
source share

This code worked fine 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); 
0
Jan 23 '13 at 8:19
source share

check the size of the canvas on which you are drawing the bitmap, your canvas may still remain landscape, so you can see only the square part of the rotated bitmap.

0
Aug 07 '16 at 7:12
source share



All Articles