7

我正在从处于横向模式的相机中保存图像。所以它会以横向模式保存,然后我在其上应用一个也处于横向模式的叠加层。我想旋转该图像然后保存。例如,如果我有这个

在此处输入图像描述

我想顺时针旋转一次 90 度,然后将其保存到 sdcard:

在此处输入图像描述

这要如何实现?

4

5 回答 5

14
void rotate(float x)
    {
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.tedd);

        int width = bitmapOrg.getWidth();

        int height = bitmapOrg.getHeight();


        int newWidth = 200;

        int newHeight  = 200;

        // calculate the scale - in this case = 0.4f

         float scaleWidth = ((float) newWidth) / width;

         float scaleHeight = ((float) newHeight) / height;

         Matrix matrix = new Matrix();

         matrix.postScale(scaleWidth, scaleHeight);
         matrix.postRotate(x);

         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);

         iv.setScaleType(ScaleType.CENTER);
         iv.setImageBitmap(resizedBitmap);
    }
于 2012-04-26T11:57:02.717 回答
6

检查这个

public static Bitmap rotateImage(Bitmap src, float degree) 
{
        // create new matrix
        Matrix matrix = new Matrix();
        // setup rotation degree
        matrix.postRotate(degree);
        Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        return bmp;
}
于 2014-01-05T18:46:17.500 回答
1

您可以使用 Canvas API 来执行此操作。请注意,您需要切换宽度和高度。

    final int width = landscapeBitmap.getWidth();
    final int height = landscapeBitmap.getHeight();
    Bitmap portraitBitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(portraitBitmap);
    c.rotate(90, height/2, width/2);
    c.drawBitmap(landscapeBitmap, 0,0,null);
    portraitBitmap.compress(CompressFormat.JPEG, 100, stream);
于 2012-04-26T11:58:24.057 回答
0

使用 Matrix.rotate(degrees) 并使用该旋转矩阵将位图绘制到它自己的画布上。我不知道您是否可能需要在绘制之前制作位图的副本。

使用 Bitmap.compress(...) 将您的位图压缩为输出流。

于 2012-04-26T11:51:43.510 回答
0

Singhak 的解决方案效果很好。如果您需要适合结果位图的大小(可能是 ImageView),您可以按如下方式扩展该方法:

public static Bitmap rotateBitmapZoom(Bitmap bmOrg, float degree, float zoom){
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);

    float newHeight = bmOrg.getHeight() * zoom;
    float newWidth  = bmOrg.getWidth() / 100 * (100.0f / bmOrg.getHeight() * newHeight);

    return Bitmap.createBitmap(bmOrg, 0, 0, (int)newWidth, (int)newHeight, matrix, true);
}
于 2014-11-30T12:37:59.140 回答