public static Bitmap getimage(String srcPath, int picSize) {
float hh = 0;
float ww = 0;
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;// 读入图片基本信息(只是获取图片的头部信息,包含宽高)不真正加载图片
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
int w = newOpts.outWidth;
int h = newOpts.outHeight;
LogTools.logI(TAG, "获取相机宽高:" + w + "X" + h);
SharedPreferences settings = CustomApplication.getAppContext().getSharedPreferences("settings",
Context.MODE_PRIVATE);
String photo_dpi = settings.getString(SettingsConstant.PHOTO_DPI, "800X480");
if (photo_dpi.equals("800X480")) {
if (w > h) {// 横屏
ww = 800f;// 这里设置高度为800f
hh = 480f;// 这里设置宽度为480f
} else {
hh = 800f;// 这里设置高度为800f
ww = 480f;// 这里设置宽度为480f
}
} else if (photo_dpi.equals("1280X800")) {
if (w > h) {// 横屏
ww = 1280f;
hh = 800f;
} else {
hh = 1280f;
ww = 800f;
}
}
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 横屏
be = (int) (w / ww);
} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 竖屏
be = (int) (h / hh);
}
if (be <= 1) {
if (photo_dpi.equals("1280X800") && (w > 2000 || h > 2000)) {
be = 2;
} else if (photo_dpi.equals("800X480") && (w > 1200 || h > 1200)) {
be = 2;
} else {
be = 1;
}
}
newOpts.inSampleSize = be;// 设置缩放比例
newOpts.inJustDecodeBounds = false;// 重新读入图片
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
int hhh = bitmap.getHeight();
int www = bitmap.getWidth();
LogTools.logI(TAG, "生成图片分辨率宽高:" + www + "X" + hhh + " 压缩比:" + be);
return compressImage(bitmap, picSize, srcPath);// 压缩好比例大小后再进行质量压缩
}
这里通过计算:be=3,结果却不是真实缩放了3 而是2
06-17 15:30:51.908: I/ImageUtil(481): 获取相机宽高:<span style="color:#ff6666;">4608X3456</span>
06-17 15:30:52.269: I/ImageUtil(481): 生成图片分辨率宽高:<span style="color:#ff6666;">2304X1728 </span>压缩比:3
06-17 15:30:52.568: I/ImageUtil(481): 循环压缩:521kb
06-17 15:30:52.709: I/ImageUtil(481): 循环压缩:398kb
06-17 15:30:52.843: I/ImageUtil(481): 循环压缩:322kb
06-17 15:30:52.969: I/ImageUtil(481): 循环压缩:274kb
06-17 15:30:52.982: I/ImageUtil(481): 最终:274kb
06-17 15:30:53.182: I/PzActivity(481): 保存图片大小:389kb
06-17 15:30:54.976: I/Timeline(481): Timeline: Activity_launch_request time:115629989
这里明明代码是计算是3,但后面真正压缩还是2.。。难道这是一个BUG。希望看见这篇博文的大牛们指点迷津...