Android compresses images to less than kb and keeps them efficient.

Source: Internet
Author: User

At present, mobile phones generally have 8 million pixels in cameras, like my Galaxy Nexus, which is about 5 million pixels. Uploading such a large photo to the server is not only a waste of traffic, but also a waste of time.

When developing Android enterprise applications, images are often uploaded to the server. This is the case for a project currently maintained by our company. This project interacts with the server through the private apn. The connectivity is good, but the moving speed is too slow. When the Customer is using the software, because the uploaded information may contain multiple images, the upload may often fail. To solve this problem, we decided to compress the image to less than kb, and ensure that the image is not distorted (about KB after the image is compressed ). So I re-studied the image compression technology of Android.
Shows the directory structure of Android:


Shows the third-party library jar package used:

The ksoap2-android-xxx.jar is Android used to call webservice, gson-xx.jar is to convert JavaBean into Json data format.
This blog explains how to compress images. The core code is as follows:


Originally posted code. As a result, the page of the eoe blog is too narrow and the code is very messy. Let's give a connection and download the source code by yourself.
Https://github.com/feicien/StudyDemo/tree/master/FileUploadDemo

Compression principle: compress an image. We need to know the original size of the image and compress it according to the compression ratio we set.
In this way, we need to do three things:
1. Get the length and width of the original image.

  1. BitmapFactory. Options options = newBitmapFactory. Options ();
  2. Options. inJustDecodeBounds = true;
  3. BitmapFactory. decodeFile (filePath, options );
  4. Int height = options. outHeight;
  5. Int width = options. outWidth;

The above code decode the image. The value of inJustDecodeBounds is set to true. You can still calculate the size of the image without reading the image into the memory, which meets our first step.
2. Calculate the compression ratio

  1. Int height = options. outHeight;
  2. Int width = options. outWidth;
  3. Int inSampleSize = 1;
  4. Int reqHeight = 800;
  5. Int re qwidth = 480;
  6. If (height> reqHeight | width> reqWidth ){
  7. Finalint heightRatio = Math. round (float) height/(float) reqHeight );
  8. Finalint widthRatio = Math. round (float) width/(float) reqWidth );
  9. InSampleSize = heightRatio <widthRatio? HeightRatio: widthRatio;
  10. }

Generally, the resolution of a mobile phone is 480*800. Therefore, after compression, the expected bandwidth of the image is set to 480 and the height is set to 800. These two values are only the expected width and height, in fact, the actual width and height after compression will be larger than expected. If the original height or bandwidth of the image is about the expected bandwidth and height, we need to calculate the zooming ratio. Otherwise, the image is not scaled. HeightRatio is a multiple of the original and compressed image height. widthRatio is a multiple of the original and compressed image width. InSampleSize is the smallest of heightRatio and widthRatio. inSampleSize is the zoom value. The value of inSampleSize is 1, indicating that the width and height are not scaled. The value of 2 indicates that the width and height after compression are 1/2 of the original size.
3. Scale and compress the image

  1. // Create a bitmap object in the memory.
  2. Options. inSampleSize = calculateInSampleSize (options, 480,800 );
  3. Options. inJustDecodeBounds = false;
  4. Bitmap bitmap = BitmapFactory. decodeFile (filePath, options );
  5.  
  6. ByteArrayOutputStream baos = newByteArrayOutputStream ();
  7. Bm. compress (Bitmap. CompressFormat. JPEG, 60, baos );
  8. Byte [] B = baos. toByteArray ();

The first three lines of code have actually obtained a scaled bitmap object. If you show an image in the application, you can use this bitmap object. Because of network traffic problems. We need to sacrifice the quality of the image in exchange for some space. Here we call bm. compress () method for compression. The second parameter of this method, if it is 100, indicates no compression. Here I set it to 60. You can also set it more as needed, during the experiment, I set it to 30, and the images will not be distorted.
Compression Effect: This demo can compress images of around MB to around kb without distortion.
As follows:

 

Author: Dust

Address: http://my.eoe.cn/isnull/archive/564.html

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.