【问题标题】:Rotate a saved bitmap in android在android中旋转保存的位图
【发布时间】:2012-04-26 11:48:46
【问题描述】:

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

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

如何实现?

【问题讨论】:

    标签: java android bitmap android-image


    【解决方案1】:
    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);
        }
    

    【讨论】:

    • 当心,也缩小新图像:)
    【解决方案2】:

    检查一下

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-25
            • 1970-01-01
            • 1970-01-01
            • 2015-07-11
            • 1970-01-01
            • 1970-01-01
            • 2014-01-20
            相关资源
            最近更新 更多