[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