SoFunction
Updated on 2025-03-11

Android LuBan and Compressor image compression method

1. LuBan compression problem/Curzibn/Luban

I chose the compressed image library before, and the most star I found on github was to pass in the image array and callback in the asynchronous thread. Next, there was a problem. The quality of the compressed image was blurred. Although it could be set to not compress at all, there was no big problem in compressing the screen of a normal mobile phone. But on the tablet, the compression will blur in the same picture.

(this)
    .load(photos)                  // List of pictures to be compressed by the successor    .ignoreBy(100)                 // Ignore the size of the uncompressed image    .setTargetDir(getPath())            // Set the compressed file storage location    .setCompressListener(new OnCompressListener() { //Set callback     @Override
     public void onStart() {
      // Called before TODO compression starts, you can start loading UI within the method     }

     @Override
     public void onSuccess(File file) {
      // Called after TODO compression is successful, returning to the compressed picture file     ***Here is a way to determine whether the compression is completed,According to the returned image++With the original arraysizeCompare***
     }

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

2,Compressor    /zetbaitsu/Compressor

This compression is a time-consuming operation in the main thread. You need to write an asynchronous thread by yourself and pass messages to the main thread through the handler.

The higher the two values ​​.setMaxWidth(640).setMaxHeight(480) the smaller the compression force, and the picture is not clear.

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

.setCompressFormat() WEBP image format is launched by Google. It has strong compression and high quality, but the IOS does not recognize it. It needs to convert the image to byte stream and then to PNG format.

.setCompressFormat()PNG format compression will cause the picture to become larger and consume too much memory, and the phone will respond slowly.

.setCompressFormat()JPEG compression; the compression speed is faster than PNG, the quality is average, and basically belongs to 1/10 of the compression ratio

              try {

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

              String imageString=();
                (imageString);
              } catch (IOException e) {
                ();
              }

Here we determine whether the size of paths is equal to the size of the image array, and whether to call handler to pass messages to the main thread to sit for other operations

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.