bitmap生产缩略图

Android BitMap数据源生产缩略图 
/**
* 生成缩略图
* 缩略图是将原图等比压缩,压缩后宽、高中较小的一个等于198像素
*/
private Bitmap getThumb(Bitmap bm){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm .compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapByte = stream.toByteArray();

final BitmapFactory.Options options = new BitmapFactory.Options();
int reqWidth, reqHeight, width = bm.getWidth(), height = bm.getHeight();
if (width > height){
reqWidth = 240;
reqHeight = (reqWidth * height)/width;
}else{
reqHeight = 160;
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;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Matrix mat = new Matrix();
Log.d(TAG, "data.length========"+bitmapByte.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length, options);
Log.d(TAG, "klx====bitmap.getWidth()===="+bitmap);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);
}
posted @ 2021-02-05 16:25  怎么了~  阅读(176)  评论(0编辑  收藏  举报