/**
* 压缩图片到指定宽度
* 的大小
* @param bmp
* @param getWidthImae
* @return
*/
public static Bitmap sizeCompress(Bitmap bmp,float getWidthImae) {
Bitmap scaledBitmap = null;
if(bmp!=null){
// 尺寸压缩倍数,值越大,图片尺寸越小
int w = bmp.getWidth();
int h = bmp.getHeight();
float ratio = w/getWidthImae;
// 压缩Bitmap到对应尺寸
Bitmap result = Bitmap.createBitmap((int)(w/ratio), (int)(h/ratio), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, (int)(w/ratio), (int)(h/ratio));
canvas.drawBitmap(bmp, null, rect, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
result.compress(Bitmap.CompressFormat.JPEG, 100, baos);
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
scaledBitmap = BitmapFactory.decodeStream(isBm);
}
return scaledBitmap;
}
网友评论