java 在android中旋转保存的位图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10332783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:37:21  来源:igfitidea点击:

Rotate a saved bitmap in android

javaandroidbitmapandroid-image

提问by prometheuspk

I am saving an image from the camera that was in landscape mode. so it gets saved in landscape mode and then i apply an overlay onto it that too is in landscape mode. I want to rotate that image and then save. e.g. if i have this

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

enter image description here

在此处输入图片说明

I want to rotate clockwise by 90 degrees once and make it this and save it to sdcard:

我想顺时针旋转 90 度一次并将其保存到 SD 卡:

enter image description here

在此处输入图片说明

How is this to be accomplished?

这将如何实现?

回答by MAC

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);
    }

回答by Singhak

Check this

检查这个

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;
}

回答by Renard

You can use the Canvas API to do that. Note that you need to switch width and height.

您可以使用 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);

回答by Bondax

Use a Matrix.rotate(degrees) and draw the Bitmap to it's own Canvas using that rotating matrix. I don't know though if you might have to make a copy of the bitmap before drawing.

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

Use Bitmap.compress(...) to compress your bitmap to an outputstream.

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

回答by Jens van de M?tter

The solution of Singhak works fine. In case you need fit the size of result bitmap (perhaps for ImageView) you can expand the method as follows:

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);
}