Summary of Three Compression Methods of Android Little Knowledge Pictures

  • 2021-10-16 04:54:54
  • OfStack

Format a picture

At present, the commonly used picture formats of Android are png, jpeg and webp.

png: Lossless compressed picture format, support Alpha channel, Android cutting materials mostly use this format

jpeg: Lossy compressed picture format, does not support transparent background, suitable for large picture compression with rich colors such as photos, not suitable for logo

webp: It is a picture format that provides both lossy compression and lossless compression. It is derived from the video coding format VP8. From Google official website, lossless webp is 26% smaller than png on average, lossless webp is 25% ~ 34% smaller than jpeg on average, lossless webp supports Alpha channel, lossless webp is also supported under 1 condition, lossless webp is supported after Android4.0 (API 14), and lossless and transparent are supported after Android4.3 (API18)

Using webp can effectively reduce the disk space occupied by pictures while maintaining the clarity of pictures

Memory size of Bitmap in Android: Picture length x Picture width x number of bytes per pixel

1. Compress method of Bitmap (mass compression):


public boolean compress(CompressFormat format, int quality, OutputStream stream)

Parameter format: Indicates the compression format of the image. Currently, there are CompressFormat. JPEG, CompressFormat. PNG, CompressFormat. WEBP.

Parameter quality: Image compression ratio, 0-100. 0 is compressed by 100%, and 100 means no compression.

Parameter stream: Write the output stream of compressed data.

Common usage:


public static Bitmap compress(Bitmap bitmap){

 ByteArrayOutputStream baos = new ByteArrayOutputStream();

 bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos);

 byte[] bytes = baos.toByteArray();

 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

}

In the above method, bitmap is mass compressed by compress method of bitmap, 10% compressed and 90% uncompressed.

The size of the picture does not change, because quality compression will not reduce the pixels of the picture, but change the bit depth and transparency of the picture on the premise of keeping the pixels, so as to achieve the purpose of compressing the picture, which is why this method is called quality compression method. If the length, width and pixels of the picture are unchanged, the memory size occupied by bitmap will not change.

The smaller the quality value, the smaller the compressed baos (usage scenario: when sharing on WeChat, it is necessary to limit the byte array size of the picture, so the compress method of bitmap can be used to compress the quality of the picture).

2. inJustDecodeBounds and inSampleSize parameters of BitmapFactory. Options (sampling compression ratio):

inJustDecodeBounds: When inJustDecodeBounds is set to true, BitmapFactory will return an empty (null) Bitmap object when decoding pictures through decodeXXXX, which avoids memory allocation for Bitmap, but it can return the width and height of Bitmap and MimeType.

inSampleSize: When it is less than 1, it will be treated as 1. If it is greater than 1, the width and height of bitmap will be reduced proportionally (1/inSampleSize), and the resolution will be reduced. When it is greater than 1, this value will be treated as a multiple of 2. For example, width=100, height=100, inSampleSize=2, then bitmap will be processed as width=50, height=50, width and height will be reduced to 1/2, and the number of pixels will be reduced to 1/4.

Common usage:


public static Bitmap inSampleSize(Bitmap bitmap,int reqWidth,int reqHeight){

 final BitmapFactory.Options options = new BitmapFactory.Options();

 options.inJustDecodeBounds = true;

 BitmapFactory.decodeByteArray(data, 0, data.length, options);

 options.inSampleSize = calculateInSampleSize(options, reqWidth,

   reqHeight);

 options.inJustDecodeBounds = false;

 return BitmapFactory.decodeByteArray(data, 0, data.length, options);

}

public static int calculateInSampleSize(BitmapFactory.Options options,

          int reqWidth, int reqHeight) {

 final int picheight = options.outHeight;

 final int picwidth = options.outWidth;

 int targetheight = picheight;

 int targetwidth = picwidth;

 int inSampleSize = 1;

 if (targetheight > reqHeight || targetwidth > reqWidth) {

  while (targetheight >= reqHeight

    && targetwidth >= reqWidth) {

   inSampleSize += 1;

   targetheight = picheight / inSampleSize;

   targetwidth = picwidth / inSampleSize;

  }

 }

 return inSampleSize;

}
}

In the inSampleSize method, inJustDecodeBounds is set to false first, the picture is decoded by decodeXXXX method of BitmapFactory, and the empty Bitmap object (null) is returned. At the same time, the width and height of bitmap are obtained, and then the appropriate inSampleSize is calculated according to the width and height of the original bitmap and the target width and height by calculateInSampleSize method. Finally, inJustDecodeBounds is set to true, and the picture is decoded by decodeXXXX method of BitmapFactory (use scenario: for example, when reading local pictures, the memory overflow caused by excessive Bitmap is prevented).

3. Compress the picture through Matrix


Matrix matrix = new Matrix();

matrix.setScale(0.5f, 0.5f);

bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),bit.getHeight(), matrix, true);

}

Usage scenario: When customizing View, scaling, rotating, shifting and tilting the picture are common, such as scaling the picture and rounded pictures.

Summarize


Related articles: