Giter VIP home page Giter VIP logo

compressimagebyhuffman's Introduction

使用libjpeg进行图片压缩(哈夫曼编码)(kotlin协程)

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:


    allprojects {
    		repositories {
    			...
    			maven { url 'https://jitpack.io' }
    		}
    	}
	

Step 2. Add the dependency


	dependencies {
	        implementation 'com.github.ewgcat:CompressImageByHuffman:1.0.6'
	}
	

class BaseViewModel : ViewModel() {
    /**
     * 没法在主线程完成的繁重操作
     */
    fun compressImage( path: String?,
                        quality: IntArray,
                        totalSize: Int,
                        desPath: String?,
                        listener: CompressListener?) {
        viewModelScope.launch {
            CompressPictureUtil.compressImageByHuffman(path, quality, totalSize, desPath, listener)
        }
    }
}


    private fun compressImage() {
        vm.compressImage(
            path = path,
            quality = intArrayOf(100),
            totalSize = 100,
            desPath = desPath,
            listener = object : CompressListener {
                override fun startCompress() {
                    showLoading("Compressing")
                }

                override fun completedCompress() {
                    hideLoading()
                    Glide.with(this@MainActivity).load(File(desPath)).into(iv2)
                }
            })
    }
    

注意

1、支持armeabi,armeabi-v7a,arm64-v8a

2、压缩过程是耗时操作,使用kotlin协程切换线程

3、图片压缩有最大极限,如果要指定压缩到多少KB,请结合其他压缩方式使用。

###使用效果

将3.58M的图片无损压缩到100KB

CTO博客介绍

compressimagebyhuffman's People

Contributors

ewgcat avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.