美文网首页
封装图片压缩

封装图片压缩

作者: vpractical | 来源:发表于2019-12-18 19:49 被阅读0次

[TOC]

demo地址:https://github.com/vpractical/CatCompress

相关

  • optimize_coding为false,导致图片压缩效果差

  • Android中最容易导致OOM的情况,一个是内存泄露,一个就是Bitmap

  • Bitmap.Config

    • ALPHA_8: 每个像素占用1字节(8位),存储的是透明度信息
      从API 13开始不推荐使用,在android4.4上面,设置的ARGB_4444会被系统使用ARGB_8888替换
    • ARGB_8888: 默认的选项,每像素占用4字节,ARGB分别占8位,支持1600万种颜色,质量最高,当然内存占用也高
    • RGB_565: 每像素占用2字节,RGB分别占5,6,5位。支持65535种颜色,不支持alpha
  • bitmap占用内存 = 图像像素总和(w * h)再 * 每像素(bitmapconfig)占用的字节数.
    以一张10241024的图片为例,使用ARGB_8888,占用的内存为10241024*4=4M

分析

  • 实际场景中,多为取相册图片和获取拍摄图片进行压缩
  • 压缩分为宽高压缩compressByPixel和质量压缩compressByQuality
  • 根据需要压缩图片的数量,选择单线程or多线程方案
  • 配置压缩属性
  • 传入待压缩图片地址集,返回源图和压缩后图片的地址映射集合,压缩后的图片保存在本地

实现

使用:

        val config = CompressConfig
                .get(this)
                .maxPixel(1000)
                .maxSize(200)
                .form(Bitmap.Config.ARGB_8888)
                .cacheDir(COMPRESS_PATH)
                .enablePixelCompress(true)
                .enableQualityCompress(true)
                .enableDeleteOriginalImage(isDelete)
                .enableShowLoading(true)

        val time = System.currentTimeMillis()
        val callback = object : CompressCallback {
            override fun compressSuccess(list: java.util.ArrayList<Image>) {
                val cur = (System.currentTimeMillis() - time).toInt()
                Log.e("----", "压缩完成${list.size};耗时 = $cur")
                for (it in list) {
                    if (!TextUtils.isEmpty(it.compressPath)) {

                    } else {
                        Log.e("----", "压缩失败,compressPath有null值${it.isCompress}")
                    }
                }
            }

            override fun compressFailed(list: java.util.ArrayList<Image>, msg: String) {
                Log.e("----", "压缩失败${list.size}---$msg")
            }
        }

        if (isMulit) {
            //多线程方案
            CompressCat2
                    .get(this)
                    .config(config)
                    .callback(callback)
                    .compress(list)
        } else {
            //单线程方案
            CompressCat
                    .get(this)
                    .config(config)
                    .callback(callback)
                    .compress(list)
        }

核心代码:

/**
     * 像素压缩
     */
    private fun compressByPixel(path: String, listener: CompressSingleListener) {
        try {
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeFile(path, options)
            options.inJustDecodeBounds = false
            val w = options.outWidth
            val h = options.outHeight
            val max = config.maxPixel
            var ratio = 1 //图片大小与期望大小的比例
            if (h in max..w) {
                ratio = (max + h) / max
            } else if (w in max..h) {
                ratio = (max + w) / max
            }

            if (ratio < 1) {
                ratio = 1
            }

            options.inSampleSize = ratio
            options.inPreferredConfig = config.form
            options.inPurgeable = true
            options.inInputShareable = true // 当系统内存不够时候图片自动被回收,和inPurgeable同时设置有效
            val bitmap = BitmapFactory.decodeFile(path, options)

            Log.e("----core:pixel----", "w=$w;h=$h;ration=$ratio;-----w=${bitmap.width};h=${bitmap.height};size=${bitmap.byteCount / 1024}")

            if (config.enableQualityCompress) {
                compressByQuality(path, bitmap, listener)
            } else {
                val file = getCacheFile(File(path))
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, FileOutputStream(file))
                listener.compressSuccess(path)
            }

        } catch (e: Exception) {
            e.printStackTrace()
            listener.compressFailed(path, "压缩像素异常: ${e.printStackTrace()}")
        }
    }
/**
     * 质量压缩
     */
    private fun compressByQuality(path: String, bitmap: Bitmap, listener: CompressSingleListener) {
        val baos = ByteArrayOutputStream()
        val file = getCacheFile(File(path))
        val fos = FileOutputStream(file)
        try {
            var option = 100
            bitmap.compress(Bitmap.CompressFormat.JPEG, option, baos)
            while (baos.toByteArray().size > config.maxSize) {
                baos.reset()
                option -= 4
                bitmap.compress(Bitmap.CompressFormat.JPEG, option, baos)
                if (option - 4 <= 0) {
                    //已经质量压缩到这个比例下最小
                    break
                }
            }
            Log.e("----core:quality----", "option=$option;-----size=${baos.toByteArray().size / 1024}")
            fos.write(baos.toByteArray())
            listener.compressSuccess(file.absolutePath)
        } catch (e: Exception) {
            e.printStackTrace()
            listener.compressFailed(path, "压缩质量异常: ${e.printStackTrace()}")
        } finally {
            fos.flush()
            fos.close()
            baos.flush()
            baos.close()
            bitmap.recycle()
        }
    }


    private fun getCacheFile(file: File): File {
        return File(config.cacheDir, "/compress_" + file.name)
    }

相关文章

  • 封装图片压缩

    [TOC] demo地址:https://github.com/vpractical/CatCompress 相关...

  • 图片压缩——Siren

    简易图片压缩封装——Siren 图片压缩方式简略介绍image.png 两个源码比较鲜明易懂的框架 鲁班压缩 ht...

  • 图片压缩封装类

    }

  • 图片压缩插件

    利用canvas封装的压缩图片插件,解决了iphone照片图片方向问题。(笔者在getBase64()回调中,调用...

  • 压缩图片及文件base64和File格式互转

    项目中有用到上传图片、压缩图片、图片格式转化等功能,现将封装的方法整理如下: input file 转为 base...

  • php图片压缩居中裁剪

    用于业务需求,需要进行图片处理,图片压缩生成缩略图,超过规定尺寸要居中裁剪,现封装一个图片处理方法,直接调用即可:...

  • 图片压缩组件

    图片压缩 图片压缩

  • iOS 图片压缩方法

    两种图片压缩方法 两种图片压缩方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 通过...

  • iOS 图片压缩限制大小最优解

    iOS 图片压缩限制大小最优解 图片的两种压缩方法 1.1 压缩图片质量 1.2 压缩图片尺寸 压缩图片使图片文件...

  • iOS 图片压缩限制大小最优解

    概要: 图片的两种压缩方法1.1 压缩图片质量1.2 压缩图片尺寸压缩图片使图片文件小于指定大小2.1 压缩图片质...

网友评论

      本文标题:封装图片压缩

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