Android将位图旋转90度会导致压缩图像.需要在纵向和横向之间进行真正的旋转

use*_*764 18 android bitmap rotation

我试图将位图图像旋转90度,将其从横向格式更改为纵向格式.例:

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

顺时针旋转90度变为

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

使用下面的代码(来自在线示例),图像旋转90度,但保留横向纵横比,因此您最终得到一个垂直压扁的图像.难道我做错了什么?我需要使用另一种方法吗?我也愿意旋转我用来创建位图的jpeg文件,如果这更容易的话.

 // 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); 
Run Code Online (Sandbox Code Playgroud)

Sha*_*ley 41

这就是旋转图像所需的全部内容:

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(original, 0, 0, 
                              original.getWidth(), original.getHeight(), 
                              matrix, true);
Run Code Online (Sandbox Code Playgroud)

在您的代码示例中,您包含了对postScale的调用.这可能是你的图像被拉伸的原因吗?也许拿出那个并做更多的测试.


Tol*_*a E 13

这是你如何正确旋转它(这确保了图像的正确旋转)

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;
}
Run Code Online (Sandbox Code Playgroud)