zoukankan      html  css  js  c++  java
  • 获取图片信息

    引用:http://my.eoe.cn/blue_rain/archive/240.html

    当图片过大,或图片数量较多时使用BitmapFactory解码图片会出java.lang.OutOfMemoryError: bitmap size exceeds VM budget,要想正常使用则需分配更少的内存,具体的解决办法是修改采样值BitmapFactory.Options.inSampleSize,例如:

    1
    2
    3
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 4;
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
    

    如何设置恰当的inSampleSize

    设置恰当的inSampleSize是解决该问题的关键之一。BitmapFactory.Options提供了另一个成员inJustDecodeBounds。

    1
    2
    3
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);
    

    设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,即opts.width和opts.height。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。

    查看Android源码,Android提供了一种动态计算的方法。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
    
        int roundedSize;
        if (initialSize <= 8 ) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
    
        return roundedSize;
    }
    
    private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
    
        int lowerBound = (maxNumOfPixels == -1) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                Math.floor(h / minSideLength));
    
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
    
        if ((maxNumOfPixels == -1) &&
                (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    

    使用该算法,就可动态计算出图片的inSampleSize。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imageFile, opts);
    
    opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
    opts.inJustDecodeBounds = false;
    try {
        Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
        imageView.setImageBitmap(bmp);
        } catch (OutOfMemoryError err) {
    }
    

    此办法只是获取的图片的缩略图,可能会造成失真!

  • 相关阅读:
    会话追踪技术
    request session application
    Java CAS同步机制 原理详解(为什么并发环境下的COUNT自增操作不安全): Atomic原子类底层用的不是传统意义的锁机制,而是无锁化的CAS机制,通过CAS机制保证多线程修改一个数值的安全性。
    mysql连接拍错总结
    git 从分支上创建一个分支
    linux ---> taskkill pid 8080 /f
    阿里java开发规范 强制约束
    for break
    Mybatis Update操作返回值问题
    springboot 缓存架构
  • 原文地址:https://www.cnblogs.com/sode/p/3169421.html
Copyright © 2011-2022 走看看