有时候我们在日常开发过程中,由于项目需求,需要上传图片,例如上传头像等等。但是有时候,由于原图过大我们又不能让用户上传原图,需要对图片的进行压缩,才能上传。这里为大家提供一些常用的图片压缩算法,帮助大家在日常开发过程中解决图片上传时,图片压缩的问题。上一篇的博文http://blog.csdn.net/loveyaozu/article/details/51160482中其实已经为大家提供了图片的压缩算法。
下面单独贴出来给大家
/**
* 图片压缩
*
* @param bmp
* @param file
*/
public static void compressBmpToFile(File file,int height,int width) {
Bitmap bmp = decodeSampledBitmapFromFile(file.getPath(), height, width);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length / 1024 > 30) {
baos.reset();
if (options - 10 > 0) {
options = options - 10;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
if (options - 10 <= 0) {
break;
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 质量压缩
* @author syz 2015-1-5 下午1:29:58
* @param image
* @param maxkb
* @return
*/
public static Bitmap compressBitmap(Bitmap image,int maxkb) {
//L.showlog(压缩图片);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
int options = 100;
// 循环判断如果压缩后图片是否大于(maxkb)50kb,大于继续压缩
while (baos.toByteArray().length / 1024 > maxkb) {
// 重置baos即清空baos
baos.reset();
if(options-10>0){
// 每次都减少10
options -= 10;
}
// 这里压缩options%,把压缩后的数据存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
// 把压缩后的数据baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// 把ByteArrayInputStream数据生成图片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
/**
*
* @param res
* @param resId
* @param reqWidth
* 所需图片压缩尺寸最小宽度
* @param reqHeight
* 所需图片压缩尺寸最小高度
* @return
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
/**
*
* @param filepath
* 图片路径
* @param reqWidth
* 所需图片压缩尺寸最小宽度
* @param reqHeight
* 所需图片压缩尺寸最小高度
* @return
*/
public static Bitmap decodeSampledBitmapFromFile(String filepath,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filepath, options);
}
/**
*
* @param bitmap
* @param reqWidth
* 所需图片压缩尺寸最小宽度
* @param reqHeight
* 所需图片压缩尺寸最小高度
* @return
*/
public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap,
int reqWidth, int reqHeight) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
byte[] data = baos.toByteArray();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
/**
* 计算压缩比例值(改进版 by touch_ping)
*
* 原版2>4>8...倍压缩
* 当前2>3>4...倍压缩
*
* @param options
* 解析图片的配置信息
* @param reqWidth
* 所需图片压缩尺寸最小宽度O
* @param reqHeight
* 所需图片压缩尺寸最小高度
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int picheight = options.outHeight;
final int picwidth = options.outWidth;
int targetheight = picheight;
int targetwidth = picwidth;
int inSampleSize = 1;
if (targetheight > reqHeight || targetwidth > reqWidth) {
while (targetheight >= reqHeight
&& targetwidth>= reqWidth) {
inSampleSize += 1;
targetheight = picheight/inSampleSize;
targetwidth = picwidth/inSampleSize;
}
}
Log.i("===","最终压缩比例:" +inSampleSize + "倍");
Log.i("===", "新尺寸:" + targetwidth + "*" +targetheight);
return inSampleSize;
}