- 首页 > it技术 > >
/**
* 生成缩略图
* 缩略图是将原图等比压缩,压缩后宽、高中较小的一个等于198像素
*/
private Bitmap getThumb(String path){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int reqWidth, reqHeight, width=options.outWidth, height=options.outHeight;
if (width > height){
reqWidth = 198;
reqHeight = (reqWidth * height)/width;
}else{
reqHeight = 198;
reqWidth = (width * reqHeight)/height;
}
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
try{
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Matrix mat = new Matrix();
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
ExifInterface ei =new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
mat.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mat.postRotate(180);
break;
}
//Log.e("test","bitmap.getWidth():"+bitmap.getWidth()+",,bitmap.getHeight():"+bitmap.getHeight());
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
}catch (IOException e){
return null;
}
}
推荐阅读