美文网首页
android bitmap压缩方案

android bitmap压缩方案

作者: 钉某人 | 来源:发表于2018-08-04 10:43 被阅读0次

思考的问题:

  • 一张jpg格式图片,在电脑上图片大小303.96kb,加载到手机内存的大小是多少
    在android手机中,通过代码将图片加载进内存,手机会先解析图片文件本身的数据格式,然后还原成位图,也就是bitmap对象,bitmap的大小取决与像素的数据格式以及分辨率

这篇文章来将android中的bitmap压缩的几种方案。

Bitmap的占用内存 = 图片长度 X 图片宽度 X 一个像素点占用的字节数。
常见的Bitmap的压缩格式:ALPHA_8 ARGB_4444 ARGB_8888 RGB_565。(A代表透明度,R代表红色,G代表绿色,B代表蓝色)

  • ALPHA_8:是8位的位图,一个像素占用一个字节,是单个透明通道,只有A值,不存储颜色信息。
  • ARGB_4444:是16位的位图,一个像素占用2个字节,A=4,R=4,G=4,B=4.
  • ARGB_8888:是32位的位图,一个像素占用4个字节,A=8,R=8,G=8,B=8
  • RGB_565:是16位的位图,一个像素占2个字节,没有透明度,R=5,G=6,B=5

1.质量压缩

质量压缩只能改变图片的位深以及透明度,不能改变图片的宽高和像素,bitmap所占的内存大小并不会改变,但是字节数随着质量的变小而变小了,所以质量压缩适合去传递二进制的图片数据,图片存储在磁盘上的大小会根据这个值变化,上传服务器存储图片,在图片清晰度可以的情况下使用这种方式。注意质量压缩只针对与jpeg格式的图片,png的图片是无损的。

  /**
     * 质量压缩
     * png图片是无损的,不能进行质量压缩
     */
    public static Bitmap compressQuality(Context context, int resId, int quality) {
         /*原bitmap与压缩后的bitmap的声明*/
        Bitmap bitmapOut = null;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
        /*获取图片的格式类型*/
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(context.getResources(), resId, options);
        String mimeType = options.outMimeType;
        Log.i(TAG, "图片格式:" + mimeType);
        if (quality < 0 || quality > 100) {
            Log.e(TAG, "图片质量要在0-100之间");
            return null;
        }

        /*分别对jpeg与png进行质量压缩处理*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (TextUtils.equals(mimeType,"image/jpeg")) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        }
        byte[] bytes = baos.toByteArray();
        bitmapOut = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

        Log.i(TAG, "原图片的大小:" + (bitmap.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmap.getWidth() + " 高度为" + bitmap.getHeight());

        Log.i(TAG, "压缩后图片的大小:" + (bitmapOut.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmapOut.getWidth() + " 高度为" + bitmapOut.getHeight()
                + " bytes.length=" + (bytes.length / 1024) + "KB"
                + " quality=" + quality);
        return bitmapOut;
    }

2.采样率压缩

设置inSampleSize的值,如果inSampleSize=2的话,宽高都会变成原来的1/2,占用的内存就会变成原来的1/4.
注意:有的时候在解码图片的时候,避免Bitmap的内存分配,只想获取到bitmap的宽高以及mimeType信息,此时可以设置options.inJustDecodeBounds=true,这样BitmapFactory解码图片时返回为Null的Bitmap对象,但同时获取到的宽高等数据。

 /**
     * 采样率压缩
     */
    public static Bitmap compressSampling(Context context,int resId,int sampleSize){
        Bitmap bitmapOut = null;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = sampleSize;
        bitmapOut = BitmapFactory.decodeResource(context.getResources(),resId,options);
        Log.i(TAG, "原图片的大小:" + (bitmap.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmap.getWidth() + " 高度为" + bitmap.getHeight());

        Log.i(TAG, "压缩后图片的大小:" + (bitmapOut.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmapOut.getWidth() + " 高度为" + bitmapOut.getHeight()
               );
        return bitmapOut;
    }

3.缩放法压缩

使用矩阵对图片进行缩放,

 /**
     * 缩放法压缩
     */
    public static Bitmap compressScale(Context context,int resId,float scaleX,float scaleY){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),resId);

        Matrix matrix = new Matrix();
        matrix.setScale(scaleX,scaleY);
        Bitmap bitmapOut = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        Log.i(TAG, "compressScale--压缩后图片的大小:" + (bitmapOut.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmapOut.getWidth() + " 高度为" + bitmapOut.getHeight()
        );
        return bitmapOut;
    }

4.RGB_565方法,修改Bitmap的压缩格式

RGB_565是16位的位图,ARGB_888是32位的位图,通过修改压缩格式来压缩,内存会变为ARGB_8888内存的一半,但是宽高并没有改变。

/**
    * 设置压缩格式来压缩
    */
   public static Bitmap compressConfig(Context context,int resId){
       Bitmap bitmapOut = null;
       BitmapFactory.Options options = new BitmapFactory.Options();
       options.inPreferredConfig = Bitmap.Config.RGB_565;
       bitmapOut = BitmapFactory.decodeResource(context.getResources(),resId,options);
       Log.i(TAG, "compressConfig--压缩后图片的大小:" + (bitmapOut.getByteCount() / 1024 / 1024)
               + "M 宽度为" + bitmapOut.getWidth() + " 高度为" + bitmapOut.getHeight()
       );
       return bitmapOut;
   }

5.指定图片的宽高

随便指定宽高的话,会出现图片拉伸的情况。

 /**
     * 创建新的Bitmap,并指定图片的宽高
     */
    public static Bitmap compressCreateScaleBitmap(Context context,int resId,int width,int height){
        Bitmap bitmapOut = null;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),resId);
        bitmapOut = Bitmap.createScaledBitmap(bitmap,width,height,true);
        Log.i(TAG, "compressCreateScaleBitmap--压缩后图片的大小:" + (bitmapOut.getByteCount() / 1024 / 1024)
                + "M 宽度为" + bitmapOut.getWidth() + " 高度为" + bitmapOut.getHeight()
        );
        bitmap.recycle();
        return bitmapOut;
    }

https://github.com/DingMouRen/BitmapCompress

相关文章

  • 收集_Android源码文章

    一、Bitmap: Android bitmap压缩优化方案Android性能优化系列之Bitmap图片优化 二、...

  • android bitmap压缩方案

    思考的问题: 一张jpg格式图片,在电脑上图片大小303.96kb,加载到手机内存的大小是多少在android手机...

  • 2019-11-05

    Bitmap Bitmap 细说Bitmap bitmap的六种压缩方式,Android图片压缩 1.先讲讲屏幕密...

  • 浅析Android平台图像压缩方案

    在介绍Android平台的压缩方案之前,先了解一下Bitmap的几个主要概念。 像素密度 像素密度指的是每英寸像素...

  • Bitmap图片压缩

    Android中图片是以bitmap形式存在的,这篇文章主要介绍了Android实现图片压缩(bitmap的六种压...

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • Android NDK编译libjpeg-turbo压缩图片

    Android开发中,我们经常要面对图片压缩,大部分人使用Android Bitmap进行压缩,还有一些使用lib...

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

    关键词: Bitmap,质量压缩,比例压缩,采样率压缩,微信分享 前言 android 系统的图片压缩大体上有三种...

  • android图片压缩

    先生成一个长宽被缩放的bitmap 再压缩到100k以下 关于Bitmap的compress方法,android ...

  • libjpeg-turbo 图片压缩

    android7.0之前,Bitmap.compress不支持哈夫曼压缩算法,压缩效率不高,因此引入libTurb...

网友评论

      本文标题:android bitmap压缩方案

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