1. 图片压缩1
/**
* 图片按比例压缩方法一
* @param filePath 文件路径
* @param outWidth 输出文件宽度
* @param outHeight 输出文件高度
* @return
*/
public static Bitmap compressImageByScale(String filePath, float outWidth, float outHeight,int limitMaxSize){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = countInSampleSize(options,outWidth,outHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
return bitmap;
}
2. 计算图片比例的缩放值
/**
* 计算图片比例的缩放值
* @param options
* @param outWidth
* @param outHeight
* @return
*/
public static int countInSampleSize(BitmapFactory.Options options,float outWidth,float outHeight){
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int inSampleSize = 1;
if(srcWidth > outWidth && srcWidth > srcHeight){
inSampleSize = (int) (srcWidth / outWidth);
}else if(srcHeight > outHeight && srcHeight > srcWidth){
inSampleSize = (int) (srcHeight / outHeight);
}
return inSampleSize < 0 ? 1 : inSampleSize;
}
3. 图片压缩2
/**
* 图片按比例压缩方法二 输出文件宽高写死
* @param filePath 文件路径
* @return
*/
public static Bitmap compressImageByScale(String filePath,int limitMaxSize){
float outWidth = 720f;
float outHeight = 1280f;
return compressImageByScale(filePath,outWidth,outHeight,limitMaxSize);
}
4. 图片压缩3
/**
* 图片按比例压缩方法三
* @param resId 文件资源id
* @param outWidth 输出文件宽度
* @param outHeight 输出文件高度
* @return
*/
public static Bitmap compressImageByScale(Context context,int resId, float outWidth, float outHeight,int limitMaxSize){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeResource(context.getResources(), resId, options);
options.inSampleSize = countInSampleSize(options,outWidth,outHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId, options);
return bitmap;
}
5. 图片压缩4
/**
* 图片按比例压缩方法四
* 输出文件宽高写死
* @param resId 文件资源id
* @return
*/
public static Bitmap compressImageByScale(Context context,int resId,int limitMaxSize){
float outWidth = 720f;
float outHeight = 1280f;
return compressImageByScale(context,resId,outWidth,outHeight,limitMaxSize);
}
6. 质量压缩 不过没啥用 感觉
/**
* 图片质量压缩
* @param bitmap
* @param limitMaxSize
* @return
*/
public static Bitmap compressImageByQuality(Bitmap bitmap, int limitMaxSize){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG,options,baos);
int baosLength = baos.toByteArray().length;
while ((baosLength / 1024) > limitMaxSize){
baos.reset();
options = options - 10 > 0 ? options - 10 : 0;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
if(options == 0){
break;
}
}
Log.e("compressImage options",options+"");
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap outBitmap = BitmapFactory.decodeStream(isBm, null, null);
return outBitmap;
}
7. 质量压缩与图片压缩结合 效果最好
/**
* 质量压缩结合比例压缩
* @param bitmap
* @return
*/
public static Bitmap compressImageByScale(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
if(baos.toByteArray().length / 1024 > 1024){
baos.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG,60,baos);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(bais,null,options);
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
float outWidth = 720f;
float outHeight = 1280f;
int scale = 1;
if(srcWidth > outWidth && srcWidth > srcHeight){
scale = (int) (srcWidth / outWidth);
}else if(srcHeight > outHeight && srcHeight > srcWidth){
scale = (int) (srcHeight / outHeight);
}
if(scale < 0) {
scale = 1;
}
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bais = new ByteArrayInputStream(baos.toByteArray());
Bitmap outBitmap = BitmapFactory.decodeStream(bais, null, options);
return outBitmap;
}
8. bitmap与String互相转换
/**
* 把bitmap转换成String
* @param bitmap
* @return
*/
public static String bitmapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
/**
* 把String转Bitmap
* @param str
* @return
*/
public static Bitmap stringToBitmap(String str){
Bitmap bitmap = null;
try{
byte[] bitmapArray;
bitmapArray = Base64.decode(str, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
}catch (Exception e) {
}finally {
return bitmap;
}
}