Three image compression methods in Android

  • 2020-06-23 01:55:40
  • OfStack

The existence form of images in Android:

1: File form: base 2 exists on hard disk.
2: Stream form: Base 2 form exists in memory.
3: Bitmap form

3 forms of difference:
File form and stream form: has no effect on image size. That is, if the image on your phone's SD card is streamed into memory, it will be the same size as the original image.
Note: Not in Bitmap form.
Bitmap format: Images take up a lot of memory in an instant.
Here's what the code looks like:


 /**
   *  Picture compression method summary 
   */

  /*
   *  Methods of image compression 01 : Mass compression method 
   */
  private Bitmap compressImage(Bitmap beforBitmap) {

    //  Data from memory buffers can be captured and converted into byte arrays. 
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (beforBitmap != null) {
      //  The first 1 Parameters: image compression format; The first 2 Parameters: the ratio of compression; The first 3 Parameters: compressed data stored to bos In the 
      beforBitmap.compress(CompressFormat.JPEG, 100, bos);
      int options = 100;
      //  Loop to determine if the compressed image is greater than 100kb, If greater than, continue to compress, otherwise do not compress 
      while (bos.toByteArray().length / 1024 > 100) {
        bos.reset();//  Set to null 
        //  The compression options%
        beforBitmap.compress(CompressFormat.JPEG, options, bos);
        //  Reduce each time 10
        options -= 10;

      }
      //  from bos Read the data out   Deposit to ByteArrayInputStream In the 
      ByteArrayInputStream bis = new ByteArrayInputStream(
          bos.toByteArray());
      //  Convert the data into images 
      Bitmap afterBitmap = BitmapFactory.decodeStream(bis);
      return afterBitmap;
    }
    return null;
  }

  /*
   *  Image compression method 02 : Get thumbnails 
   */
  public Bitmap getThumbnail(int id) {
    //  To get the original image 
    Bitmap beforeBitmap = BitmapFactory.decodeResource(
        mContext.getResources(), id);
    //  wide 
    int w = mContext.getResources()
        .getDimensionPixelOffset(R.dimen.image_w);
    //  high 
    int h = mContext.getResources().getDimensionPixelSize(R.dimen.image_h);

    //  Get thumbnails 
    Bitmap afterBitmap = ThumbnailUtils
        .extractThumbnail(beforeBitmap, w, h);
    return afterBitmap;

  }

  /**
   *  Image compression 03
   * 
   * @param id
   *       The size of the image to manipulate 
   * @param newWidth
   *       The width specified by the image 
   * @param newHeight
   *       The height specified by the image 
   * @return
   */
  public Bitmap compressBitmap(int id, double newWidth, double newHeight) {
    //  To get the original image 
    Bitmap beforeBitmap = BitmapFactory.decodeResource(
        mContext.getResources(), id);
    //  The original width and height of the image 
    float beforeWidth = beforeBitmap.getWidth();
    float beforeHeight = beforeBitmap.getHeight();

    //  Calculate the width and height scaling 
    float scaleWidth = 0;
    float scaleHeight = 0;
    if (beforeWidth > beforeHeight) {
      scaleWidth = ((float) newWidth) / beforeWidth;
      scaleHeight = ((float) newHeight) / beforeHeight;
    } else {
      scaleWidth = ((float) newWidth) / beforeHeight;
      scaleHeight = ((float) newHeight) / beforeWidth;
    }

    //  Matrix object 
    Matrix matrix = new Matrix();
    //  Zooming picture action   scaling 
    matrix.postScale(scaleWidth, scaleHeight);
    //  create 1 A new one Bitmap  Cut the image from the original image 
    Bitmap afterBitmap = Bitmap.createBitmap(beforeBitmap, 0, 0,
        (int) beforeWidth, (int) beforeHeight, matrix, true);
    return afterBitmap;

  }


Related articles: