2016-01-21 17:56 2125人阅读 收藏 举报
一:在什么时候我们需要对图片质量压缩?
一般情况下,我们在网上上传图片的时候要压缩图片的质量(体积),因为有的时候服务器对图片大小有限制
二: 在什么时候我们需要对图片尺寸压缩?
我们从服务器请求的图片通常要转化为Bitmap对象,如果该图片尺寸越大,通常所需要的Bitmap也越大,为防止oom,所以通常我们要对图片进行尺寸压缩
三: 在Bitmap中我们要用到BitmapFactory.Options这个类,所以对这个类的几个属性先进行说明一下
options.inJustDecodeBounds = true;//设置为true后,在通过BitmapFactory返回对象的时候,并不会真的返回你一个bitmap,而是会返给这个bitmap的宽和高,防止oom
options.inPurgeable = true;//设置为true 当系统内存不足时可以回收bitmap对象,但是当需要重新编译的时候可以访问存储bitmap的原始数据
opts.inInputShareable = true;// 设置是否深拷贝,与inPurgeable结合使用
options.inPreferredConfig = Config.RGB_565;//默认值ARGB_8888改为RGB_565,节约一半内存。
Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位
Bitmap.Config ARGB_8888:每个像素占四位,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位
Bitmap.Config RGB_565:每个像素占四位,即R=5,G=6,B=5,没有透明度,那么一个像素点占5+6+5=16位
Bitmap.Config ALPHA_8:每个像素占四位,只有透明度,没有颜色
四:图片尺寸压缩的写法
图片压缩的原理是抽取像素点,分辨率(像素密度大小不变),只改变图片的尺寸
-
-
-
-
-
-
-
- @SuppressWarnings("finally")
- public static Bitmap getImageThumbnail(String imagePath, int maxWidth,
- int maxHeight, boolean isDeleteFile) {
- Bitmap bitmap = null;
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- options.inPurgeable = true;
- options.inInputShareable = true;
- options.inPreferredConfig = Config.RGB_565;
- bitmap = BitmapFactory.decodeFile(imagePath, options);
- options.inJustDecodeBounds = false;
- options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
- FileInputStream inputStream = null;
- File file = new File(imagePath);
- try {
- if(file != null && file.exists()){
- inputStream = new FileInputStream(file);
- bitmap = BitmapFactory.decodeStream(inputStream, null,options);
- if(isDeleteFile){
- file.delete();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (inputStream != null)
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return bitmap;
- }
- }
五:图片质量压缩的写法
- 图片质量压缩的,会使图片占用内存减小(file),像素数不变,所生成bitmap对象大小不变
-
"code" class="java">
-
-
-
-
-
-
- private static Bitmap compressImage(Bitmap image,int size,int options) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- image.compress(Bitmap.CompressFormat.JPEG, 80, baos);
-
- while (baos.toByteArray().length / 1024 > size) {
- options -= 10;
- baos.reset();
-
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- }
-
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
-
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
-
"code" class="java">六:两种方法的混合使用
-
"code" class="java">
-
-
-
-
-
-
-
- public static Bitmap compressBitmap(String imgUrl,int reqWidth,int size,int quality){
-
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(imgUrl, options);
- int height = options.outHeight;
- int width = options.outWidth;
- int reqHeight;
- reqHeight = (reqWidth * height) / width;
-
- options.inSampleSize = calculateInSampleSize(
- options, reqWidth, reqHeight);
- options.inJustDecodeBounds = false;
- Bitmap bm = BitmapFactory.decodeFile(
- imgUrl, options);
- Bitmap mBitmap = compressImage(Bitmap.createScaledBitmap(
- bm, 480, reqHeight, false),size,quality);
- return mBitmap;
- }
-
-
-
-
-
-
-
- private 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) {
- if (width > height) {
- inSampleSize = Math.round((float) height / (float) reqHeight);
- } else {
- inSampleSize = Math.round((float) width / (float) reqWidth);
- }
- }
- return inSampleSize;
- }