美文网首页Android开发探索Android Android知识
关于android 图片压缩你需要知道的事

关于android 图片压缩你需要知道的事

作者: 江风凌晓 | 来源:发表于2016-09-06 00:15 被阅读1889次

    关键词: Bitmap,质量压缩,比例压缩,采样率压缩,微信分享

    前言


    android 系统的图片压缩大体上有三种方式,质量压缩,比例压缩,采样率压缩

    一般最简单直观的应该是bitmap.compress方法,把位图的压缩信息写入到一个指定的输出流,其中有一个参数quality,取值0-100,数值越小,输出流越小。但是无论是质量压缩,比例压缩,还是采样率压缩,单独使用可能都没法达到理想的效果。比如微信的32k限制,单纯的质量压缩就无法达到要求。所以我不得不花些时间分析这三种压缩方式,最后把这三种方式结合在一起,才得出了一个比较理想的压缩结果,以下是对这几种压缩方式的一个整理。

    质量压缩


    1.代码:

        public static Bitmap getCompressBitmapByQuality(Bitmap bitmap, int quality) {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
                return BitmapFactory.decodeStream(inputStream, null, null);
        }
    

    2.关于质量压缩

    关于质量压缩需要注意的是质量压缩只改变了图片的位深及透明度,但是并没有改变Bitmap在内存中的大小
    即上边的代码只改变了ByteArrayOutputStream 的大小,如果把压缩过的Bitmap保存到文件中,文件的大小会变小,但是Bitmap本身的大小不会变

    这里有一个延伸的知识点:Bitmap在内存中的占用大小是由什么决定的呢?
    google对于bitmap大小的获取在不同的API版本中有不同的方法
    Api 19: 以上用getAllocationByteCount()
    Api 12: 以上用getByteCount()
    更早: 自己算:-)

    我们可以先看看这个函数 Bitmap.getByteCount()

        /**
         * Returns the minimum number of bytes that can be used to store this bitmap's pixels.
         *
         * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can
         * no longer be used to determine memory usage of a bitmap. See {@link
         * #getAllocationByteCount()}.</p>
         */
        public final int getByteCount() {
            // int result permits bitmaps up to 46,340 x 46,340
            return getRowBytes() * getHeight();
        }
    
    

    其中getRowBytes() 是bitmap 中每一行所占的比特数,乘以bitmap的高度getHeight(),就是bitmap在内存中所占用的空间大小,其中getRowBytes()和bitmap的宽度还有bitmap所使用的色彩格式有关系,比如如果使用的是ARGB_8888 那么getRowBytes()的大小就是bitmap.getWidth()4,乘以4*的原因是在ARGB_8888的色彩格式中,每个像素点占4位。
    android系统中的色彩模式有一下几种

    Bitmap.Config 描述 占用内存(字节)
    Bitmap.Config ARGB_8888 表示32位的ARGB位图 4
    Bitmap.Config ARGB_4444 表示16位的ARGB位图 2
    Bitmap.Config RGB_565 表示16位的RGB位图 2
    Bitmap.Config ALPHA_8 表示8位的Alpha位图 1

    由此可见bitmap在内存中的大小相关的因素是:像素点,分辨率(宽x高),色彩模式

    比例压缩


    1.代码

    public static Bitmap getCompressBitmapByScale(Bitmap bitmap, int maxW, int maxH) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
    
            float sx = (float) maxW / (float) w;
            float sy = (float) maxH / (float) h;
    
            Matrix matrix = new Matrix();
            matrix.setScale(sx, sy);
            return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    

    2.关于比例压缩

    比例压缩通过改变bitmap的宽高,可以显著改变图片大小,但是如果缩放过度了,图片也会完全糊掉。一般会按照一个指定的比例(比如 scale=0.8)循环缩放,直到压到合适的尺寸

    采样率压缩


    1.代码

    public static Bitmap getCompressBitmapBySampleSize(Bitmap bitmap, int sampleSize) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = sampleSize;
    
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            return BitmapFactory.decodeByteArray(outputStream.toByteArray(), 0, outputStream.toByteArray().length, options);
    }
    

    2.关于采样率

    采样率为1的时候为原始大小,为2的时候,宽高为原来的1/2,像素数和占用内存数为原来1/4,采样率是2的指数。

    谷歌提供的一个关于采样率的计算方法:

    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
    
        if (height > reqHeight || width > reqWidth) {
    
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
    
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
    
        return inSampleSize;
    }
    

    分享到微信的压缩算法


    最后提供一个分享到微信的压缩算法,基本上是结合了采样率压缩,比例压缩和质量压缩,在把图片压缩到指定大小的同时,尽可能保证图片的清晰度

    这是一个图片压缩的demo,三种压缩方式都有一个简单的实现
    https://github.com/jhwing/ImageCompress
    这是一个社会化分享的sdk,支持微信,微博,qq,三个平台的分享功能,关于图片压缩的算法在这个sdk里
    https://github.com/jhwing/SKShare

    延伸阅读


    http://www.cnblogs.com/hrlnw/p/4403334.html
    http://blog.csdn.net/lsyz0021/article/details/51356670
    https://github.com/bither/bither-android-lib/blob/master/REASON.md
    http://blog.csdn.net/angel1hao/article/details/51890938
    https://github.com/zetbaitsu/Compressor
    https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    相关文章

      网友评论

      • 21fe789340c5:压缩图片有什么用处吗?
        压缩图片可以减小图片占用内存大小,加快图片上传服务器的速度。
        另外说一句,博主写的挺好的
      • 3c90517fda6a:其实看不懂啦……
      • 12e193cbe762:压缩图片有什么用处吗?
      • 顺利的娃娃:不明觉厉
      • 李怡帆_:写得真好。

      本文标题:关于android 图片压缩你需要知道的事

      本文链接:https://www.haomeiwen.com/subject/fgqdettx.html