在移动设备上进行图片相关软件的开发,一定得先压缩图片,毕竟移动设备的内存有限,稍不注意就会导致内存溢出,刚好用到这里,就学习了一下相关的压缩图片,现在总结出来和大家共享。
1.图片存在的几种形式:
- File,存在于我们的磁盘中,我们通常说的图片大小。
- Stream即流的形式,比如我们上传网络图片。
- Bitmap,就是我们通常指内存中图片的大小。
2. 什么是质量压缩?
图片的质量压缩,会改变图片在磁盘中的大小(File文件的大小),不能改变图片在加载时,在内存中的大小。
原理是:通过算法扣掉(同化)了 图片中的一些某个点附近相近的像素,达到降低质量 减少 文件大小的目的。
应用场景:图片的上传。
3.什么是尺寸压缩?
图片的尺寸压缩是指:按照一定的倍数对图片减少单位尺寸的像素值,可以改变图片在内存中的大小,不改变图片在磁盘中的大小。
原理是:通过减少单位尺寸的像素值,真正意义上的降低像素值。
应用场景:用户头像的缩略图。
质量压缩
/**
* 这里我们生成了一个Pic文件夹,在下面放了我们质量压缩后的图片,用于和原图对比
* 压缩图片使用Bitmap.compress(),这里是质量压缩
*/
public void bitmapCompress(Uri uriClipUri) {
try {
//裁剪后的图像转成BitMap
//photoBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriClipUri));
photoBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uriClipUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//创建路径
String path = Environment.getExternalStorageDirectory()
.getPath() + "/Pic";
//获取外部储存目录
file = new File(path);
//创建新目录, 创建此抽象路径名指定的目录,包括创建必需但不存在的父目录。
file.mkdirs();
//以当前时间重新命名文件
long i = System.currentTimeMillis();
//生成新的文件
file = new File(file.toString() + "/" + i + ".png");
Log.e("fileNew", file.getPath());
//创建输出流
OutputStream out = null;
try {
out = new FileOutputStream(file.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//压缩文件,返回结果,参数分别是压缩的格式,压缩质量的百分比,输出流
boolean bCompress = photoBitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
try {
photoBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),Uri.fromFile(file));
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(photoBitmap);
}
尺寸压缩
/**
* 压缩图片使用,采用BitmapFactory.decodeFile。这里是尺寸压缩
*/
public void bitmapFactory(Uri imageUri){
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(imageUri, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
c.close();
// 配置压缩的参数
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //获取当前图片的边界大小,而不是将整张图片载入在内存中,避免内存溢出
BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false;
inSampleSize的作用就是可以把图片的长短缩小inSampleSize倍,所占内存缩小inSampleSize的平方
options.inSampleSize = caculateSampleSize(options,500,50);
Bitmap bm = BitmapFactory.decodeFile(imagePath, options); // 解码文件
imageView.setImageBitmap(bm);
}
/**
* 计算出所需要压缩的大小
* @param options
* @param reqWidth 我们期望的图片的宽,单位px
* @param reqHeight 我们期望的图片的高,单位px
* @return
*/
private int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int sampleSize = 1;
int picWidth = options.outWidth;
int picHeight = options.outHeight;
if (picWidth > reqWidth || picHeight > reqHeight) {
int halfPicWidth = picWidth / 2;
int halfPicHeight = picHeight / 2;
while (halfPicWidth / sampleSize > reqWidth || halfPicHeight / sampleSize > reqHeight) {
sampleSize *= 2;
}
}
return sampleSize;
}
-
质量压缩无法避免oom,但可以改变图片在磁盘中或者说是File文件的大小,尺寸压缩可以避免OOM,但不改变图片本身的大小,只改变加载是在内存中的大小,即bitmap.
-
质量压缩我们的主要方法是:MediaStore.Images.Media.getBitmap或者BitmapFactory.decodeStream;尺寸压缩我们用到的方法是:BitmapFactory.decodeFile