Android image compression techniques,

Source: Internet
Author: User

Android image compression techniques,

Please respect others' labor achievements. Reprinted with the source: Android Image Compression skills

Http://blog.csdn.net/fengyuzhengfan/article/details/41759835

When you need to upload images from the Android client to the server, you often need to compress the images. For image compression methods, we will share several common methods:

Method 1: crop to compress

I used to describe how to crop photos in the "crop photos for Android development" article. If you are interested, please take a look.


Method 2: Reduce the image quality (that is, reduce the image quality) to achieve the goal of compression.

This method is also a common method. The following describes how to downgrade images:

To reduce the image quality, we can use the Bitmap method: boolean android. graphics. Bitmap. compress (CompressFormat format, int quality, OutputStream stream)

The format parameter indicates the compressed format, and the quality indicates the image quality after compression (0 indicates the lowest, and 100 indicates that the image quality is not compressed ), stream indicates the output stream to save the compressed image.

The detailed code is as follows:

/*** Quality of multi-threaded compressed images * @ author JPH * @ param bitmap image in memory * @ param imgPath image storage path * @ date 11:30:43 */public static void compressImageByQuality (final Bitmap bitmap, final String imgPath) {new Thread (new Runnable () {// enable multithreading for compression processing @ Overridepublic void run () {// TODO Auto-generated method stubByteArrayOutputStream baos = new ByteArrayOutputStream (); options = 100; bitmap. compress (Bitmap. compressFormat. JPEG, options, baos); // quality compression method, which stores the compressed data in baos (100 indicates no compression, 0 indicates the minimum compression) while (baos. toByteArray (). length/1024> 100) {// cyclically determine if the size of the compressed image is greater than kb and the size of the image is greater than that of the compressed baos. reset (); // reset baos so that the next write overwrites the previous content options-= 10; // each time the image quality is reduced by 10if (options <0) options = 0; // if the image quality is less than 10, compress the image quality to the minimum bitmap. compress (Bitmap. compressFormat. JPEG, options, baos); // Save the compressed image to baos if (options = 0) break; // if the image quality has been minimized, do not compress.} try {FileOutputStream fos = new FileOutputStream (new File (imgPath); // Save the compressed image locally in the specified path fos. write (baos. toByteArray (); fos. flush (); fos. close ();} catch (Exception e) {e. printStackTrace ();}}}). start ();}

Method Analysis:

Since this method contains I/O operations and recursive calls are time-consuming, I use multiple threads for processing.


Method 3: scale down the image pixels proportionally to compress the image.

This method uses the android. graphics. BitmapFactory. Options. Options () method to load the image to the memory at the specified usage and output it to the local device to compress the pixel.

Code details:

/*** Scale down the image pixels proportionally to compress the image. * @ author JPH * @ param imgPath * @ date 2014-12-5 11:30:59 */public static void compressImageByPixel (String imgPath) {BitmapFactory. options newOpts = new BitmapFactory. options (); newOpts. inJustDecodeBounds = true; // read-only edge, without reading the content Bitmap bitmap = BitmapFactory. decodeFile (imgPath, newOpts); newOpts. inJustDecodeBounds = false; int width = newOpts. outWidth; int height = newOpts. outHeight; Float maxSize = 1000f; // The default 1000 pxint be = 1; if (width> height & width> maxSize) {// scaling ratio, calculate be = (int) (newOpts. outWidth/maxSize);} else if (width 

Method 4: compress the image proportionally before downgrading

This method mainly combines the second and third methods. The following is the detailed code:

/*** Quality of multi-threaded compressed images * @ author JPH * @ param bitmap image in memory * @ param imgPath image storage path * @ date 11:30:43 */public static void compressImageByQuality (final Bitmap bitmap, final String imgPath) {new Thread (new Runnable () {// enable multithreading for compression processing @ Overridepublic void run () {// TODO Auto-generated method stubByteArrayOutputStream baos = new ByteArrayOutputStream (); options = 100; bitmap. compress (Bitmap. compressForm At. JPEG, options, baos); // quality compression method, which stores the compressed data in baos (100 indicates no compression, 0 indicates the minimum compression) while (baos. toByteArray (). length/1024> 100) {// cyclically determine if the size of the compressed image is greater than kb and the size of the image is greater than that of the compressed baos. reset (); // reset baos so that the next write overwrites the previous content options-= 10; // each time the image quality is reduced by 10if (options <0) options = 0; // if the image quality is less than 10, compress the image quality to the minimum bitmap. compress (Bitmap. compressFormat. JPEG, options, baos); // Save the compressed image to baos if (options = 0) break; // if the image quality has been minimized, no compression.} try {FileOutputStream f OS = new FileOutputStream (new File (imgPath); // Save the compressed image locally and specify the path fos. write (baos. toByteArray (); fos. flush (); fos. close ();} catch (Exception e) {e. printStackTrace ();}}}). start ();} /*** scale down the image pixels proportionally to compress the image. * @ author JPH * @ param imgPath * @ date 2014-12-5 11:30:59 */public static void compressImageByPixel (String imgPath) {BitmapFactory. options newOpts = new BitmapFactory. options (); newOpts. inJustDecodeB Ounds = true; // read-only edge. Bitmap bitmap = BitmapFactory is not read. decodeFile (imgPath, newOpts); newOpts. inJustDecodeBounds = false; int width = newOpts. outWidth; int height = newOpts. outHeight; float maxSize = 1000f; // The default 1000 pxint be = 1; if (width> height & width> maxSize) {// zoom ratio, calculate be = (int) (newOpts. outWidth/maxSize);} else if (width 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.