SoFunction
Updated on 2025-04-11

Detailed explanation of Android image compression examples

Detailed explanation of Android image compression examples

When sharing on WeChat, since the shared thumbnail requirements must not be greater than 32K, otherwise WeChat cannot be adjusted. So I summarized the compression problem of Android pictures. Most of the information comes from the sharing of everyone on the Internet. I just improved or modified it. In the spirit of continuing to share, it is also convenient for me to remember it. So I summarized it as follows.

There are two main ways to compress android picture: 1. Compress image resolution 2. Compress image quality

1. Let’s look at the compressed image resolution first, which is easy to understand. For example, the original image compression of 1280*768 is 640*384. Without saying nonsense, just upload the code:



/**
    * Compress image resolution proportionally
    * @param inBitmap
    * @param outHeight The output image height can be calculated based on this proportion.
    * @param needRecycled Whether to recycle inBitmap
    * @return
    */ 
  public static Bitmap createScaledBitmapByOutHeight(Bitmap inBitmap, int outHeight, boolean needRecycled) {      
    int bitmapHeight = (); 
    int bitmapWidth = (); 
    int outWidth = bitmapWidth * outHeight / bitmapHeight; 
     
    return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled); 
  }  
   
  /**
    * Compress image resolution proportionally
    * @param inBitmap
    * @param outHeight The output image width can be calculated based on this proportion.
    * @param needRecycled Whether to recycle inBitmap
    * @return
    */ 
  public static Bitmap createScaledBitmapByOutWidth(Bitmap inBitmap, int outWidth, boolean needRecycled) {     
    int bitmapHeight = (); 
    int bitmapWidth = (); 
    int outHeight = bitmapHeight * outWidth / bitmapWidth; 
     
    return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled); 
  } 
   
  /**
    * Specify the output width and height zoom picture
    * @param inBitmap
    * @param outWidth
    * @param outHeight
    * @param needRecycled
    * @return
    */ 
  public static Bitmap createScaledBitmap(Bitmap inBitmap, int outWidth, int outHeight, boolean needRecycled) {         
     
    Bitmap thumbBmp = (inBitmap, outWidth, outHeight, true); 
    if (needRecycled) { 
      (); 
    } 
     
    return thumbBmp; 
  } 

The first two methods can specify the desired width or height and scale the resolution of the picture to scale. The third method can specify the desired width or height at will and scale the picture.

The above code is to scale the input bitmap, and you can also load and scale the picture from the resource or file, as follows:

/**
    * Load and compress images from resources
    * @param res
    * @param resId
    * @param outWidth Target Width
    * @param outHeight Target height
    * @return
    */ 
  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,  
      int outWidth, int outHeight) {  
    final  options = new ();  
     = true; // Fake it, to get the image size    (res, resId, options);  
     = calculateInSampleSize(options, outWidth, outHeight);  
    // Use the obtained inSampleSize value to parse the image again     = false; 
    // = Config.RGB_565; 
    return (res, resId, options);  
  }  
   
  /**
    * Load and compress pictures from file
    * @param res
    * @param resId
    * @param outWidth Target Width
    * @param outHeight Target height
    * @return
    */ 
  public static Bitmap decodeSampledBitmapFromFile(String pathName, int outWidth, int outHeight) { 
    final  options = new ();  
     = true; // Fake it, to get the image size    (pathName, options); 
     = calculateInSampleSize(options, outWidth, outHeight);  
    // Use the obtained inSampleSize value to parse the image again     = false; 
    // = Config.RGB_565; 
    return (pathName, options); 
  }  
   
  /**
    * calculate 
    * @param options
    * @param reqWidth
    * @param reqHeight
    * @return
    */ 
  public static int calculateInSampleSize( options,  
      int reqWidth, int reqHeight) {  
    // Height and width of the source image    final int height = ;  
    final int width = ;  
    int inSampleSize = 1;  
    if (height > reqHeight || width > reqWidth) {  
      // Calculate the ratio of actual width and height to target aspect and height      final int heightRatio = ((float) height / (float) reqHeight);  
      final int widthRatio = ((float) width / (float) reqWidth);  
      // Choose the minimum ratio of width and high school as the inSampleSize value, so that the width and height of the final picture can be guaranteed.      // It will definitely be greater than or equal to the width and height of the target.      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
    }  
    return inSampleSize;  
  }  

2. The quality of compressed pictures

/**
    * Compress image quality, compress the image to outSize
    * @param inBitmap Original bitmap
    * @param outSize compressed to size
    * @param needRecycled Whether to recycle bitmap
    * @return
    */ 
  public static Bitmap compressImage(Bitmap inBitmap, int outSize, boolean needRecycled) {  
      
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    (, 100, baos); 
    int quality = 100;  
    while (().length / 1024 > outSize) { 
      if (quality <= 0) { 
        ByteArrayInputStream outBais = new ByteArrayInputStream(()); 
        return (outBais, null, null);// If the quaLity is 0 and has not reached within 32k, return the minimum value; if necessary, combine resolution compression      } 
      (); 
      //In PNG format, this compression does not work (quality: 0-100. If the target size is too small, sometimes quality compression may not necessarily achieve the effect, and resolution compression is required)      (, quality, baos); 
      ("AN", "bitmap size:"+ ().length / 1024 + "k"); 
      quality -= 10; 
    } 
    if (needRecycled) { 
      (); 
    } 
     
     
    ByteArrayInputStream bais = new ByteArrayInputStream(()); 
    Bitmap outBitmap= (bais, null, null);//ByteArrayInputStream to bitmap     
    return outBitmap;  
  } 


It should be noted that the compress method is only valid for JPEG format. For PNG format, the second parameter quality will be ignored, that is, the compression will not work. This compression only affects the quality of the image and will not change the size of the image.

Of course, if necessary, the above two compression methods can be used in combination.

The above is a detailed explanation of the implementation method of Android image compression. If you have any questions, please leave a message or go to the community of this website to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this website!