zetbaitsu / Compressor

An android image compression library.
7.04k stars 961 forks source link

Imporve custom compress speed via merging the multi Constraints and other optimizations as follows: #178

Open yuruiyin opened 3 years ago

yuruiyin commented 3 years ago

Issue description:

compressedImage = Compressor.compress(this@MainActivity, imageFile) {
                    destination(destFile)  
                    resolution(2000, 2000)
                    format(Bitmap.CompressFormat.WEBP)
                    quality(100)
                    size(2_000_000) // 5M
                }

if each constraint spend 2s, then the compression of the above will spend 10s(the sum of each time cost). The result is not what we want.

Solution:

  1. Merge multi ResolutionConstraints and QualityConstraints and FormatConstraints (without DestinationConstraint and SizeConstraint) into only one DefaultConstraint.
  2. Move the SizeConstraint to the last post since it can reduce the number of compress times in this type of SizeConstraint.
  3. In SizeConstraint, we can avoid performing the compression if the bitmap compress format is png.
  4. If there is a DestinationConstraint, we can reduce the number of copy file times via using the dest file from DestinationConstraint in the method of copyToCache.
yuruiyin commented 3 years ago

@zetbaitsu hi, plz help review, thanks~