【问题标题】:how to rotate a bitmap 90 degrees如何将位图旋转 90 度
【发布时间】:2012-01-26 08:16:33
【问题描述】:

android中有canvas.drawBitmap(visiblePage, 0, 0, paint);声明

当我添加canvas.rotate(90) 时,没有效果。但是如果我写

canvas.rotate(90)
canvas.drawBitmap(visiblePage, 0, 0, paint);

我没有绘制位图。那我做错了什么?

【问题讨论】:

标签: android


【解决方案1】:

你也可以试试这个

Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg, width, height, true);

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

然后您可以使用旋转后的图像在您的imageview中设置通过

imageView.setImageBitmap(rotatedBitmap);

【讨论】:

  • 我认为对于你想要的 scaledBitmap (bitmapOrg, width, height, true)
  • 导入哪个矩阵? android.graphics 还是 android.opengl ?
  • 导入android.graphics
  • 这会占用大量内存。对于大型位图,由于内存中有多个位图副本,可能会产生问题。
  • 如果您不需要原始位图,请致电bitmap.recycle() 确定。
【解决方案2】:
Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

【讨论】:

  • 我是安卓新手。只是想知道我是否执行 Bitmap newBitmap = RotateBitmap(oldBitmap, 90),我的“解码位图”是否有两个内存块(旧的和新的)或者它们是否指的是同一个内存,但一个没有旋转,另一个有旋转?....我担心的是,如果我将 R.drawable.picture 解码为 oldBitmap,如果它假设占用 2 MB 内存(我猜是堆?),newBitmap 是否会占用额外的 2 MB 内存(即 2+2 =总共4MB)?还是 newBitmap 只引用 oldBitmap (因此不需要额外的 2MB)? .........我想不惜一切代价避免 outOfMemory 错误!
  • @ShishirGupta 未经测试,但由 android 文档测试:If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
  • @Arvis 嘿,arvis 我尝试了你的建议,它适用于方向但是现在我得到了一个小得多的纵向居中图像。有什么想法吗?
【解决方案3】:

Kotlin

的短扩展
fun Bitmap.rotate(degrees: Float): Bitmap {
    val matrix = Matrix().apply { postRotate(degrees) }
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}

及用法:

val rotatedBitmap = bitmap.rotate(90f)

【讨论】:

    【解决方案4】:

    以下是在 android 中旋转或调整图像大小的代码

    public class bitmaptest extends Activity {
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            LinearLayout linLayout = new LinearLayout(this);
    
            // load the origial BitMap (500 x 500 px)
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
                   R.drawable.android);
    
            int width = bitmapOrg.width();
            int height = bitmapOrg.height();
            int newWidth = 200;
            int newHeight = 200;
    
            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
    
            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);
            // rotate the Bitmap
            matrix.postRotate(45);
    
            // recreate the new Bitmap
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                              width, height, matrix, true);
    
            // make a Drawable from Bitmap to allow to set the BitMap
            // to the ImageView, ImageButton or what ever
            BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
    
            ImageView imageView = new ImageView(this);
    
            // set the Drawable on the ImageView
            imageView.setImageDrawable(bmd);
    
            // center the Image
            imageView.setScaleType(ScaleType.CENTER);
    
            // add ImageView to the Layout
            linLayout.addView(imageView,
                    new LinearLayout.LayoutParams(
                          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                    )
            );
    
            // set LinearLayout as ContentView
            setContentView(linLayout);
        }
    }
    

    您也可以查看此链接了解详情:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

    【讨论】:

      【解决方案5】:

      使用Java createBitmap() 方法可以通过度数。

      Bitmap bInput /*your input bitmap*/, bOutput;
      float degrees = 45; //rotation degree
      Matrix matrix = new Matrix();
      matrix.setRotate(degrees);
      bOutput = Bitmap.createBitmap(bInput, 0, 0, bInput.getWidth(), bInput.getHeight(), matrix, true);
      

      【讨论】:

        【解决方案6】:

        我会进一步简化 comm1xKotlin 扩展函数

        fun Bitmap.rotate(degrees: Float) =
            Bitmap.createBitmap(this, 0, 0, width, height, Matrix().apply { postRotate(degrees) }, true)
        

        【讨论】:

          【解决方案7】:

          默认情况下,旋转点是画布的 (0,0) 点,我猜你可能想围绕中心旋转它。 我做到了:

          protected void renderImage(Canvas canvas)
          {
              Rect dest,drawRect ;
          
              drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight());
              dest = new Rect((int) (canvas.getWidth() / 2 - mImage.getWidth() * mImageResize / 2), // left
                              (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize / 2), // top
                              (int) (canvas.getWidth() / 2 + mImage.getWidth() * mImageResize / 2), //right
                              (int) (canvas.getWidth() / 2 + mImage.getHeight()* mImageResize / 2));// bottom
          
              if(!mRotate) {
                  canvas.drawBitmap(mImage, drawRect, dest, null);
              } else {
                  canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
                  canvas.rotate(90,canvas.getWidth() / 2, canvas.getHeight()/ 2);
                  canvas.drawBitmap(mImage, drawRect, dest, null);
                  canvas.restore();
              }
          }
          

          【讨论】:

            【解决方案8】:

            如果你旋转位图,90 180 270 360 是可以的,但对于其他角度,画布会绘制不同大小的位图。

            所以,最好的办法是

            canvas.rotate(degree,rotateCenterPoint.x,rotateCenterPoint.y);  
            canvas.drawBitmap(...);
            canvas.rotate(-degree,rotateCenterPoint.x,rotateCenterPoint.y);//rotate back
            

            【讨论】:

              【解决方案9】:

              如果您的目标是在 imageView 或文件中包含旋转图像,您可以使用 Exif 来实现。支持库现在提供:https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

              以下是它的用法,但要实现您的目标,您必须检查库 api 文档。我只是想提示一下,旋转位图并不总是最好的方法。

              Uri uri; // the URI you've received from the other app
              InputStream in;
              try {
                in = getContentResolver().openInputStream(uri);
                ExifInterface exifInterface = new ExifInterface(in);
                // Now you can extract any Exif tag you want
                // Assuming the image is a JPEG or supported raw format
              } catch (IOException e) {
                // Handle any errors
              } finally {
                if (in != null) {
                  try {
                    in.close();
                  } catch (IOException ignored) {}
                }
              }
              
              int rotation = 0;
              int orientation = exifInterface.getAttributeInt(
                  ExifInterface.TAG_ORIENTATION,
                  ExifInterface.ORIENTATION_NORMAL);
              switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                  rotation = 90;
                  break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                  rotation = 180;
                  break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                  rotation = 270;
                  break;
              }
              

              依赖

              编译“com.android.support:exifinterface:25.1.0”

              【讨论】:

                【解决方案10】:

                请注意来自 java 平台调用的 Bitmap 类型,例如 comm1xGnzlt 的答案,因为它可能返回 null。我认为如果参数可以是任何数字并使用中缀以提高可读性,它也会更加灵活,具体取决于您的编码风格。

                infix fun Bitmap.rotate(degrees: Number): Bitmap? {
                    return Bitmap.createBitmap(
                        this,
                        0,
                        0,
                        width,
                        height,
                        Matrix().apply { postRotate(degrees.toFloat()) },
                        true
                    )
                }
                

                如何使用?

                bitmap rotate 90
                // or
                bitmap.rotate(90)
                

                【讨论】:

                  猜你喜欢
                  • 2011-01-14
                  • 1970-01-01
                  • 2014-04-27
                  • 1970-01-01
                  • 2013-02-13
                  • 1970-01-01
                  • 2010-11-21
                  • 2012-12-23
                  相关资源
                  最近更新 更多