封装图片压缩

2019-12-18  本文已影响0人  vpractical

[TOC]

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

相关

分析

实现

使用:

        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)
    }
上一篇 下一篇

猜你喜欢

热点阅读