Android image compression and optimization examples

preface

Image compression is already a rotten street in Android technology. Last week, I saw two open source libraries and compared the compression of my own projects. I found some new things and recorded them.

Why compress

1. Cause of volume

If your picture is to be uploaded, it must not be a few m in size. Moreover, if the picture resolution is greater than the device resolution, it makes no sense.

2. Memory reason

If a picture is to be displayed on an Android device, ImageView will eventually load a bitmap object, so we need to consider the memory occupation of a single bitmap object. How to calculate the memory occupation of a picture? In fact, it is the sum of the memory occupation of all pixels:

Bitmap memory size = picture length x picture width x bytes per pixel

The decisive factor is the last parameter. Bitmap 'has two common encoding methods: ARGB_ 8888 and RGB_ 565,ARGB_ 8888 4 bytes per pixel, RGB_ 565 is 2 bytes, and ARGB is generally used_ 8888 this. Then the common 1080 * 1920 picture memory occupation is:

1920 x 1080 x 4 = 7.9M

Compression principle

It can be concluded from the above that image compression should be carried out at the same time from two aspects: first, reduce the resolution, and then reduce the quality of each pixel, that is, the memory occupation.

Resolution compression

Suppose an original image is 3840x2400 and I want to compress it into 1920x1080. In fact, it is impossible to compress this value 100%. Because the aspect ratio should be ensured for image compression, imagine that an 800x100 horizontal view may be pressed into a 20x200 vertical view? impossible.. The common algorithm here is to ensure a shorter edge within the range of 1920x1080, and then compress the whole graph in proportion:

Here, the aspect ratio of the original graph is 3840 / 2400 = 1.6, the aspect ratio of the target graph is 1920 / 1080 = 1.78 > 1.6, and the shorter edge is high. Therefore, it should be compressed according to a high proportion.

2400 / 1080 = 2.22, so the real target value is 1728x1080, the compression ratio is rounded to 2, and then compressed through the following code:

There seems to be no problem. Look at the measured results. The original figure is 3840 * 2400, with a size of 2.2m. I choose four resolutions as the target value for compression:

It can be seen that none of the four compressed images has reached the target value, and the deviation is large. The reason is that the attribute options.insamplesize can only be the nth power of 2. If it is calculated to be 7, Android will take the approximate value of 8, and so on, so this value cannot be compressed to the target value. After taking a look at the compressor, an open source library, he processed it and redraws the compressed image on the canvas according to the target size to get a new bitmap:

Core code:

The pictures compressed by the compressor open source library are compared as follows:

It can be seen that it can be compressed to the real target value every time. (note that it is not the target value, and pay attention to distinguish between the target value and the real target value)

Mass compression

Bitmap has a compression method (compressformat, int quality, OutputStream stream). Quality is the compression quality. The smaller the value, the more severe the compression.

However, we generally do not directly set this value, but customize a compressed size, such as 300KB, and then dynamically calculate the quality. Core code:

Compression practice

At present, the mature open source libraries include Luban: https://github.com/Curzibn/Luban

This open source library algorithm is relatively complex. According to the comparison before and after the rendering, the compression of wechat circle of friends is inversely calculated. The final effect is similar to that of wechat. You can use this if you have high compression requirements. However, the method call is asynchronous, and the result is fed back in the form of callback, which is not very good..

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

This open source library is optimized and improved on the ordinary compression algorithm. The source code is easy to understand and recommended! The following is the compression test of different target values of three large pictures with compressor (BV is the compression of our project, just ignore it), and the quality parameter is set to 80%

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>