封装图片压缩

[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) {
                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, 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)
    }

你可能感兴趣的:(封装图片压缩)