介绍
小编给大家分享一下安卓常见的图片压缩方式有哪些,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!
先给出一组数据
原图:宽度:2976;身高:2976
引用>
原图实际:——→字节:2299820 Mb: 2.19328
质量压缩尺寸——→:字节:1599831 kb: 1562.33496
按比例压缩尺寸——→:字节:191707 kb: 187.21387
鲁班压缩,大小——→:字节:143792,kb: 140.42188压缩效果:鲁班压缩比;按比例压缩比;质量压缩
1、质量压缩
, public void getBitmap (String imgPath, String outPath), {,, ,,,,,,,BitmapFactory.Options newOpts =, new BitmapFactory.Options ();,, ,,,,,,,newOpts.inJustDecodeBounds =,假的,,, ,,,,,,,newOpts.inPurgeable =,真的,,, ,,,,,,,newOpts.inInputShareable =,真的,,, ,,,,,,,//,Do not compress , ,,,,,,,newOpts.inSampleSize =, 1,,, ,,,,,,,newOpts.inPreferredConfig =, Config.RGB_565;,, ,,,,,,,storeImage(位图,outPath);,//保存图片 ,,,}注意
<李>
质量压缩不会减少图片的像素,它是在保持像素的前提下改变图片的位深及透明度等,来达到压缩图片的目的,这也是为什么该方法叫质量压缩方法,所以这种方法,很可能不会减少图片的大小
李> <李>如果是bit.compress (CompressFormat.PNG、质量、保);这样的png格式,质量就没有作用了,字节。长度不会变化,因为png图片是无损的,不能进行压缩
李>保存图片
/* * ,,,,*,把位图转化成图片存储在本地 ,,,, ,,,,*,@param 位图 ,,,,*,@param outPath 本地的存储路径 ,,,,*,@throws FileNotFoundException ,,,*/,,,public static boolean storeImage (Bitmap ,位图,String outPath), throws FileNotFoundException { ,,,,,,,FileOutputStream os =, new FileOutputStream (outPath); ,,,,,,,boolean compressResult =, bitmap.compress (Bitmap.CompressFormat.JPEG,, 100,, os); ,,,,,,,return compressResult; ,,,}2,按比例压缩(尺寸压缩,采样率压缩)
/* * ,,,,*,按比例压缩 ,,,, ,,,,*,@param path ,,,原图片路径 ,,,,*,@param targetW 压缩后宽度 ,,,,*,@param targetH 压缩后高度 ,,,,*,@return 压缩后的图片的保存路径 ,,,*/,,,public static String compressScale (String 路径,,,String outPath,, int targetW,, int targetH), throws FileNotFoundException { ,,,,,,,//,获取option , ,,,,,,,BitmapFactory.Options options =, new BitmapFactory.Options (); ,,,,,,,//,inJustDecodeBounds设置为真,这样使用该option 解码出来的位图是空的,,, ,,,,,,,//,只是把长宽存放到选项中,, ,,,,,,,options.inJustDecodeBounds =,真的; ,,,,,,,//,此时位图为null , ,,,,,,,Bitmap Bitmap =, BitmapFactory.decodeFile(路径,选项); ,,,,,,,int inSampleSize =, 1,,//, 1是不缩放,, ,,,,,,,//,计算宽高缩放比例,, ,,,,,,,int inSampleSizeW =, options.outWidth /, targetW; ,,,,,,,int inSampleSizeH =, options.outHeight /, targetH; ,,,,,,,//,最终取大的那个为缩放比例,这样才能适配,例如宽缩放3倍才能适配屏幕,而,, ,,,,,,,//,高不缩放就可以,那样的话如果按高缩放,宽在屏幕内就显示不下了,, ,,,,,,,if (inSampleSizeW 祝辞,inSampleSizeH), { ,,,,,,,,,,,inSampleSize =, inSampleSizeW; ,,,,,,,},{else ,,,,,,,,,,,inSampleSize =, inSampleSizeH; null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullAndroid常见的图片压缩方式有哪些