Android LuBan and Compressor Image Compression Methods

  • 2021-08-31 09:18:24
  • OfStack

1. LuBan compression problem https://github.com/Curzibn/Luban

Before choosing the compressed picture library, the star found on github is the most, and the use method is to pass in the picture array and call back the results returned in the asynchronous thread. Next, there is a problem. The quality of compressed pictures is blurred. Although you can set how many KB1 are not compressed, there is no big problem in normal mobile phone screen compression. However, on the flat panel, the compression of the same picture will be blurred.


Luban.with(this)
    .load(photos)                  //  Pass on the list of pictures to be compressed 
    .ignoreBy(100)                 //  Ignore the size of uncompressed pictures 
    .setTargetDir(getPath())            //  Set the storage location of compressed files 
    .setCompressListener(new OnCompressListener() { // Set callback 
     @Override
     public void onStart() {
      // TODO  Called before compression begins, and can be started within the method  loading UI
     }

     @Override
     public void onSuccess(File file) {
      // TODO  Called after successful compression, returning the compressed picture file 
     *** Here, judge whether the compression is finished, and according to the returned picture, ++ With the original array size Comparison ***
     }

     @Override
     public void onError(Throwable e) {
      // TODO  Called when there is a problem with the compression process 
     }
    }).launch();  // Start compression 

2. Compressor https://github.com/zetbaitsu/Compressor

This compression is a time-consuming operation in the main thread, which requires writing an asynchronous thread and passing messages to the main thread through handler.

. setMaxWidth (640). setMaxHeight (480) The higher these two values, the smaller the compression force and the unclear picture.

. setQuality (75) This method only sets the picture quality and does not affect the size of the compressed picture KB

. setCompressFormat (Bitmap. CompressFormat. WEBP) WEBP image format is introduced by Google with strong compression and high quality, but IOS does not recognize it, so it is necessary to convert the picture into byte stream and then into PNG format

The compression of PNG (Bitmap. CompressFormat. PNG) will cause the picture to become larger and consume too much memory, and the mobile phone will respond slowly

. setCompressFormat (Bitmap. CompressFormat. JPEG) JPEG compression; The compression speed is faster than that of PNG, and the mass is 1, which basically belongs to the compression ratio of 1/10


              try {

                File file  = new Compressor(activity)
                      .setMaxWidth(640)
                      .setMaxHeight(480)
                      .setQuality(100)
                      .setCompressFormat(Bitmap.CompressFormat.JPEG)
                      .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                          Environment.DIRECTORY_PICTURES).getAbsolutePath())
                      .compressToFile(new File(filePath));

              String imageString=file.getPath();
                paths.add(imageString);
              } catch (IOException e) {
                e.printStackTrace();
              }

Here, it is judged whether the size of paths is equal to the size of the picture array, and whether it is called handler to pass messages to the main thread for other operations


Related articles: