Current location - Health Preservation Learning Network - Slimming men and women - Image compression, those things
Image compression, those things
In the process of project development, we may manipulate pictures more or less. Generally speaking, app involves memory leakage or apk volume increase, and most of the reasons are the problems of pictures, or the size of pictures is relatively large, or more pictures are loaded at one time. So personally, as long as you want to slim down the app, reduce the size of the app, avoid memory leakage, etc., and at the same time, in order to avoid OOM memory leakage, almost all apps will compress the big picture, which is beyond doubt, so as long as the picture is handled well, it can be said that most problems have been solved, so this paper will make a simple summary of the reasons for image compression and common image compression methods.

The format of files and streams has no effect on the size of pictures. That is to say, if the SD card of a mobile phone is 100k, then it is also 100k to read it in the form of a stream. If the picture exists as a bitmap, the memory will increase instantly.

File format: file.length ()

The form of the stream: read the picture file into the memory input stream and see its number of bytes;

Bitmap:bitmap.getByteCount()

If the picture is to be uploaded, then the size of several m's will definitely not work and must be compressed;

If the picture is to be displayed on an android device, ImageView will eventually load bitmap objects, so it is necessary to consider how much memory a single bitmap object occupies;

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

There are two common encoding methods for bitmap, ARGB 8888 and RGB——565. The former has 4 bytes per pixel and the latter has 2 bytes. ARGB_8888 is more commonly used.

1080 * 1920 * 4 = 7.9M

Keep the compression of the aspect ratio of the original image, but the image becomes smaller and displays all the contents of the original image. Use BitmapFactory.options to generate thumbnails by setting the zoom ratio through options;

InSimpleSize (sampling rate):

Advantages: high efficiency and fast analysis speed;

Disadvantages: inSimpleSize can only be a power of 2, and the picture size cannot be specified accurately;

The ratio of width to height can be different. You can create a new bitmap through the Bitmap.createBitmap () method. This bitmap can select a part of the picture instead of the whole compression, which is similar to uploading an avatar on QQ, allowing you to choose any zoom form on the picture, and the control is very flexible.

It means that the size and clarity of the picture look the same as the original picture, but the size is reduced. The principle of reduction is to sacrifice other things that can't be seen directly, such as resolution. It looks the same as the original image, but it will be distorted immediately after enlargement, unlike the original image, which is not clear after enlargement.