美文网首页
图片优化(一)Bitmap优化

图片优化(一)Bitmap优化

作者: 贾里 | 来源:发表于2019-02-12 16:19 被阅读5次

1.BitmapFactory
2.图片存在的形式
3.图片压缩方式

  • 质量压缩
  • 尺寸压缩
  • 采样率压缩

1.图片工具类:BitmapFactory

BitmapFactory.Options:参数:
inDensity:bitmap的像素密度
inTargetDensity:bitmap最终的像素密度

DensityDpi(屏幕的像素密度) 分辨率 Density
160dpi 320x533 1
px  = dp*Density
手机像素表.png

2.图片存在的几种形式:

  • File
  • 流的形式
  • Bitmap的形式(也是内存形式)
BitmapFactory.decodeFile(pathName)
BitmapFactory.decodeResource(res, id)
BitmapFactory.decodeStream(is)

3.图片压缩方式(Bitmap压缩)

最终调用了Bitmap的压缩算法实现的

3.1.质量压缩

原理:
通过算法抠掉(同化)了图片中的一些某个些点附近相近的像素,达到降低质量介绍文件大小的目的。减小了图片质量,缩小文件大小。

AB
CD
压缩后
AA
AA

注意:
它其实只能实现对file的影响,对加载这个图片出来的bitmap内存是无法节省的,还是那么大。因为bitmap在内存中的大小是按照像素计算的,也就是width*height,对于质量压缩,并不会改变图片的真实的像素(像素大小不会变)。
使用场景:
将图片压缩后保存到本地,或者将图片上传到服务器。根据实际需求来。

public void qualitCompress(View v){
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
        //压缩图片
        compressImageToFile(bitmap, new File(sdFile,"qualityCompress.jpeg"));
}


/**
 * 1. 质量压缩
    设置bitmap options属性,降低图片的质量,像素不会减少
    第一个参数为需要压缩的bitmap图片对象,第二个参数为压缩后图片保存的位置
    设置options 属性0-100,来实现压缩
    * @param bmp
    * @param file
 */     
 public static void compressImageToFile(Bitmap bmp,File file) {
            // 0-100 100为不压缩
            int options = 20;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // 把压缩后的数据存放到baos中
            bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
}

3.2.尺寸压缩

原理:
通过减少单位尺寸的像素值,正真意义上的降低像素。1020*8880--
使用场景:
缓存缩略图的时候(头像处理)

public static void compressBitmapToFileBySize(Bitmap bmp,File file){
        //压缩尺寸倍数,值越大,图片的尺寸就越小
        int ratio = 4;
        //压缩Bitmap到对应尺寸
        Bitmap result = Bitmap.createBitmap(bmp.getWidth()/ratio, bmp.getHeight()/ratio, Bitmap.Config.ARGB_8888);
        
        Canvas canvas = new Canvas(result);
        RectF rect = new RectF(0, 0, bmp.getWidth()/ratio, bmp.getHeight()/ratio);
        canvas.drawBitmap(bmp, null, rect , null);
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //把压缩后的数据存放到baos中
        result.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
}   

3.3采样率压缩

设置图片的采样率,降低图片像素,也是正真意义上的降低像素
通过设置采样率,加载在内存中的时候就已经压缩了

/**
* 设置图片的采样率,降低图片像素
* @param filePath
* @param file
*/
  public static void compressBitmap(String filePath, File file){
            // 数值越高,图片像素越低
            int inSampleSize = 8;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = false;
            //options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
            //采样率,必须是2的倍数
            options.inSampleSize = inSampleSize;
            Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // 把压缩后的数据存放到baos中
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100 ,baos);
            try {
                if(file.exists())
                {
                    file.delete();
                }
                else {
                    file.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
}

相关文章

  • 收集_Android源码文章

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

  • 图片优化(一)Bitmap优化

    1.BitmapFactory2.图片存在的形式3.图片压缩方式 质量压缩 尺寸压缩 采样率压缩 1.图片工具类:...

  • Bitmap

    获取Bitmap大小 Bitmap优化 一、主动释放Bitmap资源 二、主动释放ImageView的图片资源 三...

  • Laya 图片压缩

    参考Web性能优化:图片优化Android bitmap(二) 常见图片格式JPG PNG 一、PNG压缩方式 参...

  • android内存优化

    参考自Android内存优化大全(中)一、Bitmap优化 修改图片的显示大小 不要用ImageView直接显示,...

  • 十、Android性能优化之图片优化

    一. 图片存在的几种形式: 二、Bitmap的优化 图片压缩 BitmapFactory函数 BitmapFact...

  • Andorid性能优化之-图片优化

    图片优化 优化图片Bitmap资源的使用 & 内存管理 图片的内存占据了App的大部分 1.使用完毕后释放图片资源...

  • Bitmap

    Bitmap使用需要注意哪些问题 ? 要选择合适的图片规格(bitmap类型):通常我们优化Bitmap时,当需要...

  • Android进阶之性能优化

    一、性能优化分类 布局优化 绘制优化 内存泄漏优化 响应速度优化 ListView优化 Bitmap优化 线程优化...

  • Android性能优化

    Android性能优化包括布局优化、绘制优化、内存优化、线程优化、响应速度优化、Bitmap优化和ListView...

网友评论

      本文标题:图片优化(一)Bitmap优化

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