Android Image Compression

Source: Internet
Author: User

[Java]
/**
* Parse Bitmap images from files
*
* @ Param path
* @ Param maxWidth
* @ Param maxHeight
* @ Return
*/
Public static Bitmap decodeFile (String path, int maxWidth, int maxHeight ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
Bitmap image = null;
Image = BitmapFactory. decodeFile (path, options );
Double ratio = 1D;
If (maxWidth> 0 & maxHeight <= 0 ){
// Specify the width without specifying the height.
Ratio = Math. ceil (options. outWidth/maxWidth );
} Else if (maxHeight> 0 & maxWidth <= 0 ){
// Specify the height without specifying the width.
Ratio = Math. ceil (options. outHeight/maxHeight );
} Else if (maxWidth> 0 & maxHeight> 0 ){
// The height and width are both limited. At this time, we calculate the maximum size of the image that can be accommodated in this limit, so that the image will not be deformed.
Double _ widthRatio = Math. ceil (options. outWidth/maxWidth );
Double _ heightRatio = (double) Math. ceil (options. outHeight/maxHeight );
Ratio = _ widthRatio> _ heightRatio? _ WidthRatio: _ heightRatio;
}
If (ratio> 1 ){
Options. inSampleSize = (int) ratio;
}
Options. inSampleSize = calcScaleRatio (maxWidth, maxHeight, options. outWidth, options. outHeight );
Options. inJustDecodeBounds = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Image = BitmapFactory. decodeFile (path, options );
Return image;
}

/**
* Parse Bitmap images from files
*
* @ Param path
* @ Param maxWidth
* @ Param maxHeight
* @ Return
*/
Public static Bitmap decodeFile (String path, int maxWidth, int maxHeight ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
Bitmap image = null;
Image = BitmapFactory. decodeFile (path, options );
Double ratio = 1D;
If (maxWidth> 0 & maxHeight <= 0 ){
// Specify the width without specifying the height.
Ratio = Math. ceil (options. outWidth/maxWidth );
} Else if (maxHeight> 0 & maxWidth <= 0 ){
// Specify the height without specifying the width.
Ratio = Math. ceil (options. outHeight/maxHeight );
} Else if (maxWidth> 0 & maxHeight> 0 ){
// The height and width are both limited. At this time, we calculate the maximum size of the image that can be accommodated in this limit, so that the image will not be deformed.
Double _ widthRatio = Math. ceil (options. outWidth/maxWidth );
Double _ heightRatio = (double) Math. ceil (options. outHeight/maxHeight );
Ratio = _ widthRatio> _ heightRatio? _ WidthRatio: _ heightRatio;
}
If (ratio> 1) {www.2cto.com
Options. inSampleSize = (int) ratio;
}
Options. inSampleSize = calcScaleRatio (maxWidth, maxHeight, options. outWidth, options. outHeight );
Options. inJustDecodeBounds = false;
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Image = BitmapFactory. decodeFile (path, options );
Return image;
}


Author: liao3838554

Related Article

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.