Android image scaling and compression Summary (inSampleSize, Matrix comparison)

Source: Internet
Author: User

Android image scaling and compression Summary (inSampleSize, Matrix comparison)

In Android, image scaling and compression are often required. Three image scaling methods are listed below:

I. Scaling Images

1. inSampleSize (sampling rate)

Advantage: High Efficiency and fast resolution

Disadvantage: the sampling rate inSampleSize can only be the power of 2 (for example, inSampleSize = 15, the actual value is 8; inSampleSize = 17, the actual value is 16; the actual value is settled to the power of 2). Therefore, this method cannot specify the image size accurately.


2. Matrix

Advantage: You can precisely specify the scaling size of an image.

Disadvantage: it is generated on the basis of the original bitmap, which occupies memory and has low efficiency.


3. ThumbnailUtils

2.2 newly added classes actually combine and encapsulate the above two methods.


Ii. Image Compression

Image compression is usually used for network transmission to save network traffic. Generally, images need to be compressed into byte [] arrays.

Public static byte [] BitmapToByte (Bitmap bitmap) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); bitmap. compress (CompressFormat. PNG, 80, baos); // The 80 parameter indicates the proportion to be compressed. return baos. toByteArray ();}


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.