Git Product home page Git Product logo

jpegx's Introduction

LibJpegTurbo

由于Android端原生对Bitmap的处理支持有限,提供的图片压缩方式效果并不太好。我们知道Android的图像引擎是Skia,Skia对图片的处理就是有利用到Libjpeg,但是显然Libjpeg已经过去了太久,目前大多对Jpeg的处理都是用到LibJpegTurbo

对LibJpegTurbo感兴趣的可以自己前往官网查看,其编译方式也很简单,使用前查看其文档example.txt可以帮助快速入手。

JpegX

JpegX是一个Android端利用LibJpegTurbo的轻量级图片处理框,支持多种数据类型压缩, 支持同步、异步调用,支持多线程分区压缩(暂未完成)

支持的数据类型

Bitmap byte[] File File path

目前暂时只支持Jpeg,后续可能会加入其他图片格式的支持

快速接入:

Step 1. 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.xluu233:JpegX:$last_release'
	}

简单调用:

可以直接调用JNI方法,以Bitmap压缩到Jpeg文件为例:

JpegTurbo.compressBitmap(bitmap,outputFilePath)

同步调用

val outFilePath : String ?= JpegTurbo.setParams(
        input = bitmap,
        output = outputFile,
        async = false
).compress<String>()

异步调用:

通过CompressListener接口监听


JpegTurbo.setParams(
    input = bitmap,
    output = outputFile
).compress(object : CompressListener<String>{
    override fun onStart() {
        Log.d(TAG,"compress started")
    }

    override fun onCompleted(success: Boolean, result: String?) {
        Log.d(TAG,"output file path is ${result}")
    }
})

更多方法详解:

JpegTurbo类中提供了setParams()方法进行设置参数,compress()进行压缩和设置回调。以下为不同数据类型的调用方式:

setParams()参数介绍:

参数
备注
input 输入源 (必要)支持输入类型:Bitmap, ByteArray, File, File路径(String)
output 输出源 (可选)支持输出类型:Bitmap, ByteArray, File路径(String)
outputType 输出类型 (可选) 输出类型会根据输出源自动判断,如果没有设置输出源,必须手动设置输出类型
width 宽度 (可选)
height 高度 (可选)
maxSize 输出文件最大值 (可选)仅对输出类型为File有效
quality 压缩质量 (可选)范围0-100,默认60
async 开启异步压缩 (可选)默认开启
multiThread 开启多线程分区压缩 (可选)默认关闭

注意:

通过Formats枚举类设置输出类型,类型有Formats.Byte, Formats.Bitmap, Formats.File

设置不同输出类型时的CompressListener回调类型也不一样,Byte对应为ByteArraybyte[],File对应为StringBitmap对应回调类型Bitmap

1. 输入源为Jpeg文件

压缩File,可以设置输出文件路径,没有则将覆盖原文件

  • 输出为ByteArray
        JpegTurbo.setParams(
                input = file,
                outputType = Formats.Byte
        ).compress(object :CompressListener<String>{
            override fun onStart() {
                Log.d(TAG,"onStart")
            }

            override fun onCompleted(success: Boolean, result: String?) {
                Log.d(TAG,"onCompleted")
            }
        })
  • 输出为Bitmap
        JpegTurbo.setParams(
                input = file,
                outputType = Formats.Bitmap
        ).compress(object :CompressListener<Bitmap>{
            override fun onStart() {
                Log.d(TAG,"onStart")
            }

            override fun onCompleted(success: Boolean, result: Bitmap?) {
                Log.d(TAG,"onCompleted")
            }
        })
  • 输出为Jpeg文件
        //异步压缩
        JpegTurbo.setParams(
                input = file,
                output = outputFile
        ).compress(object :CompressListener<String>{
            override fun onStart() {
                Log.d(TAG,"onStart")
            }

            override fun onCompleted(success: Boolean, result: String?) {
                Log.d(TAG,"onCompleted")
            }
        })
        
        //同步压缩并覆盖原文件
        val result_path:String ?= JpegTurbo.setParams(
                input = file,
                outputType = Formats.File
        ).compress<String>
        

2. 输入源为byte数组

压缩 Byte必须指定width,height参数

  • 输出Bitmap
        JpegTurbo.setParams(
                input = byte,
                width = 1080,
                height = 1920,
                outputType = Formats.Bitmap
        ).compress(object :CompressListener<Bitmap>{
            override fun onStart() {
                Log.d(TAG,"onStart")
            }

            override fun onCompleted(success: Boolean, result: Bitmap?) {
                Log.d(TAG,"onCompleted")
            }
        })
  • 输出File
        JpegTurbo.setParams(
                input = byte,
                width = 1080,
                height = 1920,
                output = outputFile
        ).compress(object :CompressListener<String>{
            override fun onStart() {
                Log.d(TAG,"onStart")
            }

            override fun onCompleted(success: Boolean, result: String?) {
                Log.d(TAG,"onCompleted")
            }
        })
  • 同步输出Byte

        val result:ByteArray ?= JpegTurbo.setParams(
                input = byte,
                width = 1080,
                height = 1920,
                outputType = Formats.Byte
        ).compress<ByteArray>

        //如果result为null,说明压缩失败

输入源为Bitmap的时候,调用方法与上面基本一致。


本库最开始的目的是学习Libjpegturbo的使用,目前Android端的图片压缩框架很多,比如Luban,但我依旧是重复造了一个轮子🤣,自己动手的过程中才会进步,目前还有诸多不完善之处,欢迎提交issue和pull request。求大佬们一个star🧡💛 💚

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.