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) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

     

    方法2. 利用Canvas.drawBitmap

        Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
            try {
                int width = bm.getWidth();
                int height = bm.getHeight();
                Matrix matrix = new Matrix();
                matrix.setRotate(orientationDegree, ( float ) width / 2, ( float ) height / 2);
                float targetX = 0;
                float targetY = 0;
                if (orientationDegree == 90 || orientationDegree == 270) {
                    if (width > height) {
                        targetX = ( float ) height / 2 - ( float ) width / 2;
                        targetY = 0 - targetX;
                    } else {
                        targetY = ( float ) width / 2 - ( float ) height / 2;
                        targetX = 0 - targetY;
                    }
                }
                matrix.postTranslate(targetX, targetY);
                Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    
                Paint paint = new Paint();
                Canvas canvas = new Canvas(bm1);
                canvas.drawBitmap(bm, matrix, paint);
    
                return bm1;
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     

    性能测试:

     1. 手机 

        CPU : MTK6575 ,1G Hz

        MEM : 512MB

        OS : andoid 2.3.7

     2.图片尺寸1632 * 1224

    结果: 

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

     2. 方法2优于方法1

  • 相关阅读:
    depression...
    Childhood is certainly not the happiest time of your time
    我在上海的日子(前言)
    struts国际化的一点尝试
    脚本(js)控制页面输入
    db2和oracle的一些区别
    工作几年是个坎?
    来深圳这半个月
    10年编程无师自通
    初试java mail
  • 原文地址:https://www.cnblogs.com/nick-zhang/p/3765707.html
Copyright © 2011-2022 走看看