Android image compression upload Foundation

In Android program development, we often see the scene of uploading pictures. Here is a technical point. We need to compress the pictures and then upload them. This can reduce the consumption of traffic and improve the upload speed of pictures.

There are a lot of information on how to compress Android on the Internet, but most of them are code fragments that explain the compression steps without a practical tool class library. So how to package the compression algorithm into a utility library? What problems will be encountered, such as:

1. How many images need to be compressed

2. Is the compressed image overwritten or saved to another directory

3. If it is saved as a directory, do you need to delete the original picture

4. If the size of the compressed image is changed, will it be reduced according to the scale of the original image or specified directly

5. If there is rotation problem in the original drawing, do you need to correct it

6. Is multi graph compression concurrent or linear

7. Can service be used for compression processing, local or remote to start the service

8. How to use thread pool to compress a large number of images

Based on the above considerations, I intend to write a series of articles to solve these problems step by step (forget your continuous attention), and integrate service, multi-threaded use and compression algorithms into one project. This is better not only in practical application, but also as learning materials. Finally, I will open source the code and iterative process involved in this series to GitHub. Welcome to star and submit bugs.

Of course, some friends may say that the number of pictures uploaded at one time in the actual application will not be too much. Is it a little too much to consider these problems? Well, if you really think so, you can ignore this series of articles.

In the actual demand, the compression is basically carried out according to the aspect ratio of the original drawing. It is rare to directly specify the size, so this series of articles is also carried out for this proportional compression.

In a word, there are two main concerns about image compression:

1. Zoom the size of the picture to achieve the purpose of compression

2. Quality compression of pictures

The size of the picture is scaled to achieve the purpose of compression

In view of this situation and the problem of image rotation, you can refer to my android article dealing with the problem of camera rotation and thinking about memory occupation.

It should be noted that the actual outwidth and actual outheight of the final output image should be calculated according to the aspect ratio of the original image. Finally, the sampling value samplesize should be calculated through the actual outwidth and actual outheight.

The core code is as follows:

To facilitate your understanding of the above code, take an extreme example:

If the width of the original picture is srcwidth = 40 and the height is srcheight = 20. The width of the desired output is maxwidth = 300 and the height is maxheight = 10. Then srcratio = 40:20 = 2, outratio = 300:10 = 30. Obviously srcratio < outratio, then the size of our actual final output picture should be subject to maxheight (10), that is, actual outheight = maxheight. Finally, calculate the actual outwidth = actual outheight * srcratio = 10 * 40 / 20 = 20 according to the ratio of the original picture, The final actual outwidth = 20. The aspect ratio of the final output picture is 20:10 = 2, which is the same as that of the original picture. Other situations are similar. I won't explain them in detail here.

Quality compression of pictures

In this case, the API interface in Android's bitmap class has the compress method

It should not be difficult to understand the three parameters. You can view the official doc document. The compress method mainly controls the pixel quality input into the stream through quality.

This is more appropriate for the scenario where the space occupied by the image to be output is not greater than a certain value, because we can judge whether the compressed size is greater than the fixed value through the loop. If it is satisfied, reduce the quality and continue to perform the compress operation. The core code is as follows:

Compressing a large image takes time, so you should consider putting the compression into the background thread. If there is no need for high concurrency, you can solve the problem by using asynctask.

Core code:

After proper encapsulation, the code can be executed in the activity

To start the compression task

Write at the end

In order to achieve the best compression results, the above two schemes can be carried out at the same time. If compression takes a long time, you need to put the compression process into the background thread for execution.

I have written a simple demo program, which has the following functions:

1. Turn on the camera to take photos

2. Specify the storage location of photos

3. Compress the photos to the specified directory

4. Use asynctask to perform compression

5. Display the compressed photos and related information to the foreground activity

Because this version uses asynctask asynchronous tasks to perform compression, and because of the fragmentation of Android versions, some versions of asynctask are multithreaded and some versions are single threaded. In short, this version is applicable to the case where there are not many compression tasks at one time. If you need to process compression tasks with large data, you need to consider using thread pool.

In addition, how to use service and multithreading together will be explained in detail in the next article.

The demo open source GitHub address is as follows:

LGImageCompressor

The above is the basic knowledge of Android image compression and upload introduced by Xiaobian. I hope it will be helpful to you. If you want to know more information, please pay attention to the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>