// 對于圖片壓縮,主要針對大圖片救拉,這里的壓縮是采取先解碼尺寸彻采,然后計算壓縮比腐缤,整數(shù)倍壓縮方式,所以是近似結果肛响;
public?static?Bitmap?getZoomImage(String?filename,?int?outWidth,?int?outHeight)?{
FileInputStream?fs?=?null;
BufferedInputStream?bs?=?null;
try?{
fs?=?new?FileInputStream(filename);
bs?=?new?BufferedInputStream(fs);
BitmapFactory.Options?options?=?setBitmapOption(filename,?outWidth,?outHeight);
return?BitmapFactory.decodeStream(bs,?null,?options);
}?catch?(Exception?e)?{
LogUtil.i(TAG,?e);
}?finally?{
try?{
bs.close();
fs.close();
}?catch?(Exception?e)?{
LogUtil.i(TAG,?e);
}
}
return?null;
}
private static BitmapFactory.Options setBitmapOption(String file, int outWidth, int outHeight) {
BitmapFactory.Options?opt?=?new?BitmapFactory.Options();
opt.inJustDecodeBounds?=?true;
//?設置只是解碼圖片的邊距岭粤,此操作目的是度量圖片的實際寬度和高度
BitmapFactory.decodeFile(file,?opt);
int?origWidth?=?opt.outWidth;?//?獲得圖片的實際高和寬
int?origHeight?=?opt.outHeight;
opt.inDither?=?false;
opt.inPreferredConfig?=?Bitmap.Config.RGB_565;
LogUtil.i(TAG,?"原始信息??"?+?StringUtils.formatKeyValue("origWidth",?""?+?origWidth)?+?StringUtils.formatKeyValue("origHeight",?""?+?origHeight));
LogUtil.i(TAG,?"目標信息??"?+?StringUtils.formatKeyValue("outWidth",?""?+?outWidth)?+?StringUtils.formatKeyValue("outHeight",?""?+?outHeight));
//?設置加載圖片的顏色數(shù)為16bit,默認是RGB_8888特笋,表示24bit顏色和透明通道剃浇,但一般用不上
opt.inSampleSize?=?1;
//?設置縮放比,1表示原比例,2表示原來的四分之一....
//?計算縮放比
if?(origWidth?!=?0?&&?origHeight?!=?0?&&?outWidth?!=?0?&&?outHeight?!=?0)?{
int?sampleSize?=?(int)?(Math.max((float)?origWidth?/?(float)?outWidth,?(float)?origHeight?/?(float)?outHeight)?+?0.5f);
if?(sampleSize?<?1)?{
sampleSize?=?1;
}
opt.inSampleSize?=?sampleSize;
}
LogUtil.i(TAG,?StringUtils.formatKeyValue("inSampleSize",?""?+?opt.inSampleSize));
opt.inJustDecodeBounds?=?false;//?最后把標志復原
return?opt;
}