Android中 图片压缩常用方法

总的来看,图片的压缩,可以从尺寸、质量两个方向来操作。

1、质量压缩

2、尺寸压缩(2的幂次方比例、任意比例、任意尺寸)


一、质量压缩-改变图片的位深以及透明度:

1、质量压缩需要注意,改变的是图片的质量quality,本身由于没有改变图片的分辨率以及图片格式性质(RGB_565、ARGB_8888、ARGB_4444)等,所以图片在内存中占有的大小不变,但是输出文件保存时,文件确实变小了。

private void compressQuality()
     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
     mSrcSize = bm.getByteCount() + "byte"
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
      byte[] bytes = bos.toByteArray(); 
     mSrcBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
 }
质量压缩不会减少图片的像素,它是在 保持像素的前提下改变图片的位深及透明度 ,来达到压缩图片的目的,图片的长,宽,像素都不会改变,那么bitmap所占内存大小是不会变的。

参数介绍:

  • format是压缩后的图片的格式,可取值:Bitmap.CompressFormat .JPEG、~.PNG、~.WEBP。
  • quality的取值范围为[0,100],值越小,经过压缩后图片失真越严重,当然图片文件也会越小。(PNG格式的图片会忽略这个值的设定)
  • stream指定压缩的图片输出的地方,比如某文件。

2、质量压缩-改变像素占用的位数

private void compressRGB565()
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options); 
 }
这是通过 压缩像素占用的内存 来达到压缩的效果,一般不建议使用ARGB_4444,因为画质实在是辣鸡,如果对透明度没有要求,建议可以改成RGB_565,相比ARGB_8888将节省一半的内存开销。

二、尺寸压缩:

1、直接设置2的幂次方比例压缩

API:

其中有两个参数:
  • pathName是图片文件的路径。

  • opts 就是所谓的采样率,它里边有很多属性可以设置,我们通过设置属性来达到根据自己的需要,压缩出指定的图片。
private void compressSampling()
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2
     mSrcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
}
采样率压缩其原理其实也是缩放bitamp的尺寸,通过调节其inSampleSize参数,比如调节为2,宽高会为原来的1/2,内存变回原来的1/4.


2、通过缩放长款一定的比例达到压缩

private void compressMatrix()
     Matrix matrix = new Matrix(); 
     matrix.setScale( 0.5f, 0.5f); 
     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
     mSrcBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); 
     bm = null
 }

其实也是对长、宽做一定比例的压缩。注意防止比例失真。这样就可以把图片压缩为原来的1/3,1/5等这样的普通比例。


3、直接输出固定长款像素数的图片

private void compressScaleBitmap()
     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test); 
     mSrcBitmap = Bitmap.createScaledBitmap(bm, 600, 900, true); 
     bm = null
 }
将图片的大小压缩成用户的期望大小,来减少占用内存。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android系统提供了一些API可以用来压缩图片,其常用的是BitmapFactory类的decodeFile方法和Bitmap类的compress方法。 1. BitmapFactory类的decodeFile方法 这个方法可以将一个文件转换成Bitmap对象,可以通过设置inSampleSize参数来实现压缩。inSampleSize表示采样率,即每隔多少个像素点取一个点,它的值必须是2的幂次方,如果不是,则会自动向下取整。例如,如果inSampleSize设置为2,则图片的宽高都会缩小为原来的一半。 示例代码: ``` public static Bitmap decodeFile(String filePath, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; } ``` 使用方法: ``` Bitmap bitmap = ImageUtils.decodeFile(filePath, 480, 800); ``` 2. Bitmap类的compress方法 这个方法可以将一个Bitmap对象压缩成一个文件,可以通过设置quality参数来控制压缩质量,范围是0-100,100表示不压缩。另外,还可以设置format参数来控制压缩格式,默认为JPEG格式。 示例代码: ``` public static boolean compress(Bitmap bitmap, String filePath, int quality) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(filePath); return bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 使用方法: ``` boolean success = ImageUtils.compress(bitmap, filePath, 80); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值