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 ();}