Image compressor
Reduce the size of large photos into smaller photos
Android image compression library image compressor is small and effective. With very little or no image quality degradation, a compressor enables you to reduce the size of large photos into smaller photos.
Why Do We Need Image Compression?
Consider a black and white image with a resolution of 1000*1000 and each pixel uses 8 bits to represent the intensity. So the total no of bits req= 1000*1000*8 = 80,00,000 bits per image. Uploading such bits to the server is time-consuming.
So, we need a way to have proper representation to store the information about the image in a minimum number of bits without losing the character of the image. Thus, image compression plays an important role.
How to Implement
Step 1:
Add it to your root build.gradle at the end of repositories:
Step 2:
Add the dependency in your project build.gradle
Let’s compress the image size!
- Compress the Image File at the specified
imagePath
When compressing a picture, add the following block of code to the activity or fragment. Either after taking a picture with a camera or selecting one from a gallery.
compressImage takes 4 parameters where the last one is optional
- context, the current/active state of the application.
- imagePath parameter takes the absolute image path.
- imageName is completely up to the user.
- imageQuality is set to
50
by default. The max can be set to 100.
Compress the Image File at the specified imagePath
and return the compressed ImagePath
With the image name supplied, the code block above returns the path to the compressed picture.
compressedImagePath has the imagePath with the imageName specified.
Example
An illustration of how the code block can be utilized.
In the event that an image is selected from a gallery, make a copy of it and follow the path.
The original picture path is used to compress and rewrite the image.
Thank you for taking the time to read this article. If you found this post to be useful and interesting, please clap and recommend it