android图片压缩

1、根据图片宽、高压缩图片

public static Bitmap getBitmap(String filePath) {

final BitmapFactory.Options options =new BitmapFactory.Options();

options.inJustDecodeBounds=true;

BitmapFactory.decodeFile(filePath, options);

// Calculate inSampleSize

options.inSampleSize=calculateInSampleSize1(options, 480, 800);

// Decode bitmap with inSampleSize set

options.inJustDecodeBounds=false;

return BitmapFactory.decodeFile(filePath, options);

}

public static int calculateInSampleSize(BitmapFactory.Options options,

int reqWidth,int reqHeight) {

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int heightRatio = Math.round((float) height

/ (float) reqHeight);

final int widthRatio = Math.round((float) width / (float) reqWidth);

inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

}

return inSampleSize;

}

2、压缩Bitmap质量

public static byte[] Bitmap2Bytes(Bitmap bm)

{

ByteArrayOutputStream baos =new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);

return baos.toByteArray();

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容