Git Product home page Git Product logo

tgsen / scompressor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sharrychoo/scompressor

0.0 1.0 0.0 2.99 MB

A framework that help u compress picture more easier.(Core is libjpeg-turbo 2.0.2)

Home Page: https://sharrychoo.github.io/blog/android/2019/06/17/Android-%E5%9B%BE%E7%89%87%E5%8E%8B%E7%BC%A9%E6%A1%86%E6%9E%B6-SCompressor.html

Java 30.96% CMake 0.43% C++ 13.90% C 54.72%

scompressor's Introduction


About

A framework that help u compress picture more easier.(Core is libjpeg-turbo 2.0.2)


Feature

  • Support custom input source.
  • Support custom output type.
  • Support JPEG arithmetic coding.
    • Arithmetic coding compress ratio will higher 10% than Huffman coding.
  • Support dynamic detect file alpha channel.
  • Support assign output file type
    • Current support: JPEG, PNG, WEBP

Current Version


How to integration

Step 1

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

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

Step 2

Add it in your module build.gradle at the end of dependencies

dependencies {
    ...
    implementation 'com.github.SharryChoo:SCompressor:x.x.x'
}

How to use

1. Initialize

Initialize at application create.

SCompressor.init(this, authority);
  • this: Application Context
  • authority: help framework find URI for giving path.

2. Setup input source

// inject input data source
SCompressor.with(xxx)
       ......

Framework default support input source have: Bitmap, String(file path), URI(file uri)

If u pass other input data source, u need implement InputAdapter and invoke SCompressor.addInputAdapter.

3. Setup options

SCompressor.with(xxx)
        // range of 0 ~ 100.
        .setQuality(70)
        // set desire output size.
        .setDesireSize(500, 1000)
        // default is true: If u don't set desire size, it will auto down sample.
        .setAutoDownSample(true)
        // if true will use arithmetic coding when compress jpeg
        // the compress ratio will higher 10% than Huffman 
        .setArithmeticCoding(true)
        // Set target length after compress to file.
        .setDesireLength(500 * 1024)
        // Set target file type
        .setCompressFormat(
             // Config without alpha channel
             CompressFormat.JPEG,
             // Config with alpha channel
             CompressFormat.WEBP
        )
        ......

4. Assign output type

SCompressor.with(xxx)
        // options
        ......
        // support output type
        .asBitmap()
        .asByteArray()
        .asUri()
        .asFilePath()
        .asFile()
        // Other outputSource
        .as(Class<?>)
        ......

SCompressor default support output type have: Bitmap, byte[], Uri, String(file path), File

If u custom output source, u need implement OutputAdapter and invoke add it.

5. Call

5.1 Asynchronous Call

// normal async call.
SCompressor.with(xxx)
        .setQuality(70)
        .asBitmap()
        .asyncCall(new CompressCallback<Bitmap>() {
            @Override
            public void onSuccess(@NonNull Bitmap compressedData) {
                // it will callback on UI Thread
            }

            @Override
            public void onFailed(@NonNull Throwable e) {
                // it will callback on UI Thread
            }
        });
        
// lambda async call.
SCompressor.with(xxx)
        .setQuality(70)
        .asBitmap()
        .asyncCall(new CompressCallbackLambda<Bitmap>() {
            @Override
            public void onCompressComplete(@NonNull Bitmap compressedData) {
                 // it will callback on UI Thread
            }
        });

5.2 Synchronous call

Bitmap bitmap = SCompressor.with(xxx)
        .setInputPath(inputPath)
        .setQuality(70)
        .asBitmap()
        .syncCall()

6. Other

If u use custom input or output source, U need implement Writer or Adapter and add it.

6.1 InputAdapter

Adapter u assigned special input source to InputStream.

// Implementation Input Adapter
public class InputFileUriAdapter implements InputAdapter<Uri> {

    @Override
    public InputStream adapt(Context context, String authority, @NonNull InputSource<Uri> inputSource) throws Throwable {
        return context.getContentResolver().openInputStream(inputSource.getSource());
    }

    @Override
    public boolean isAdapter(@NonNull Class adaptedType) {
        return Uri.class.isAssignableFrom(adaptedType);
    }

}

// Add to scompressor.
SCompressor.addInputAdapter(new InputFileUriAdapter());

6.2 OutputAdapter

Adapter compressed file to u desire outputType

// Implementation Output Adapter
public class OutputFilePathAdapter implements OutputAdapter<String> {
    @Override
    public String adapt(Context context, String authority, @NonNull File compressedFile) {
        return compressedFile.getAbsolutePath();
    }

    @Override
    public boolean isAdapter(@NonNull Class adaptedType) {
        return adaptedType.getName().equals(String.class.getName());
    }
}


// Add to scompressor.
SCompressor.addOutputAdapter(myOutputAdapter);

Thanks

scompressor's People

Contributors

sharrychoo 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.