zoukankan      html  css  js  c++  java
  • Android两种 旋转Bitmap方法

    方法1. 利用Bitmap.createBitmap

       Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

                Matrix m = new Matrix();
                m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);

                try {

               Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);

                   return bm1;

                  } catch (OutOfMemoryError ex) {
                        }

                   return null;

    }


    方法2. 利用Canvas.drawBitmap

    Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

            Matrix m = new Matrix();
            m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
            float targetX, targetY;
            if (orientationDegree == 90) {
            targetX = bm.getHeight();
            targetY = 0;
            } else {
            targetX = bm.getHeight();
            targetY = bm.getWidth();
      }

        final float[] values = new float[9];
        m.getValues(values);

        float x1 = values[Matrix.MTRANS_X];
        float y1 = values[Matrix.MTRANS_Y];

        m.postTranslate(targetX - x1, targetY - y1);

        Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
        Canvas canvas = new Canvas(bm1);
        canvas.drawBitmap(bm, m, paint);


        return bm1;
      }


     

    性能测试:

    1. 手机

    CPU : MTK6575 ,1G Hz

    MEM : 512MB

    OS : andoid 2.3.7

    2.图片尺寸1632 * 1224


     

    结果:

    1. 方法1在280 - 350毫秒间, 方法2在110毫秒左右。

    2. 方法2优于方法1

  • 相关阅读:
    湖南省队集训 Day 2
    一句话题解(~ 2020.4.9)
    NOIP 2017 宝藏
    NOIP 2017 逛公园
    bzoj 4767 两双手
    Codeforces Gym 101623E English Restaurant
    浅谈Tarjan算法
    Codeforces 1027F Session in BSU
    Codeforces Gym 101623A Ascending Photo
    2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror) Solution
  • 原文地址:https://www.cnblogs.com/exmyth/p/4632700.html
Copyright © 2011-2022 走看看