Android图片压缩几种方式总结
Android图片压缩几种方式总结
图片压缩在Android开发中很常见也很重要,防止图片的OOM也是压缩的重要原因。
首先看下Bitmap图片文件的大小的决定因素:
Bitmap所占用的内存=图片长度x图片宽度x一个像素点占用的字节数。3个参数,任意减少一个的值,就达到了压缩的效果。
接下来看下Bitmap图片的几种格式的特点:
ALPHA_8
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度
ARGB_4444
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节
ARGB_8888
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
RGB_565
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
如果进行图片格式的压缩的话,一般情况下都是ARGB_8888转为RGB565进行压缩。
写了一个工具类,基本上列举了android上图片的几种基本压缩方式:
1.质量压缩
2.采样率压缩
3.尺寸压缩
4.Matrix压缩
5.图片格式的压缩,例如PNG和JPG保存后的图片大小是不同的
publicclassUtils{ /** *采样率压缩 * *@parambitmap *@paramsampleSize采样率为2的整数倍,非整数倍四舍五入,如4的话,就是原图的1/4 *@return尺寸变化 */ publicstaticBitmapgetBitmap(Bitmapbitmap,intsampleSize){ BitmapFactory.Optionsoptions=newBitmapFactory.Options(); options.inSampleSize=sampleSize; ByteArrayOutputStreambaos=newByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[]bytes=baos.toByteArray(); Bitmapbit=BitmapFactory.decodeByteArray(bytes,0,bytes.length,options); Log.i("info","图片大小:"+bit.getByteCount());//266529610661184 returnbit; } /** *图片质量压缩 * *@parambitmap *@paramquality *@return尺寸不变,质量变小 */ publicstaticBitmapcompressByQuality(Bitmapbitmap,intquality){ ByteArrayOutputStreambaos=newByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG,quality,baos); byte[]bytes=baos.toByteArray(); Bitmapbit=BitmapFactory.decodeByteArray(bytes,0,bytes.length); Log.i("info","图片大小:"+bit.getByteCount());//10661184 returnbit; } /** *图片质量压缩 * *@paramsrc *@parammaxByteSize *@return */ publicstaticBitmapcompressByQuality(Bitmapsrc,longmaxByteSize){ ByteArrayOutputStreambaos=newByteArrayOutputStream(); intquality=100; src.compress(Bitmap.CompressFormat.JPEG,quality,baos); while(baos.toByteArray().length>maxByteSize&&quality>0){ baos.reset(); src.compress(Bitmap.CompressFormat.JPEG,quality-=5,baos); } if(quality<0)returnnull; byte[]bytes=baos.toByteArray(); Bitmapbit=BitmapFactory.decodeByteArray(bytes,0,bytes.length); returnbit; } publicstaticBitmapcompressByFormat(Bitmapbitmap,intformat){ ByteArrayOutputStreambaos=newByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos); byte[]bytes=baos.toByteArray(); Bitmapbit=BitmapFactory.decodeByteArray(bytes,0,bytes.length); Log.i("info","图片大小:"+bit.getByteCount());//10661184 returnbit; } /** *Matrix缩放 * *@parambitmap *@paramscaleWidth *@paramscaleHeight *@return尺寸和大小变化 */ publicstaticBitmapgetBitmapBySize(Bitmapbitmap,floatscaleWidth,floatscaleHeight){ Matrixmatrix=newMatrix(); matrix.postScale(scaleWidth,scaleHeight); Bitmapbit=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false); Log.i("info","图片大小:"+bit.getByteCount()); returnbit; } /** *按照图片格式配置压缩 * *@parampath *@paramconfigALPHA_8,ARGB_4444,ARGB_8888,RGB_565; *@returnRGB_565比ARGB_8888节省一半内存 */ publicstaticBitmapgetBitmapByFormatConfig(Stringpath,Bitmap.Configconfig){ BitmapFactory.Optionsoptions=newBitmapFactory.Options(); options.inPreferredConfig=config; Bitmapbitmap=BitmapFactory.decodeFile(path,options); Log.i("info","图片大小:"+bitmap.getByteCount()); returnbitmap; } /** *指定大小缩放 * *@parambitmap *@paramwidth *@paramheight *@return */ publicstaticBitmapgetBitmapByScaleSize(Bitmapbitmap,intwidth,intheight){ Bitmapbit=Bitmap.createScaledBitmap(bitmap,width,height,true); Log.i("info","图片大小:"+bit.getByteCount()); returnbit; } /** *通过保存格式压缩 * *@parambitmap *@paramformatJPEG,PNG,WEBP *@return */ publicstaticBitmapgetBitmapByFormat(Bitmapbitmap,Bitmap.CompressFormatformat){ ByteArrayOutputStreambaos=newByteArrayOutputStream(); bitmap.compress(format,100,baos); byte[]bytes=baos.toByteArray(); Bitmapbit=BitmapFactory.decodeByteArray(bytes,0,bytes.length); Log.i("info","图片大小:"+bit.getByteCount()); returnbit; } /** *文件加载压缩 * *@paramfilePath *@paraminSampleSize *@return */ publicstaticBitmapgetBitmap(StringfilePath,intinSampleSize){ BitmapFactory.Optionsoptions=newBitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeFile(filePath,options);//此时不耗费和占用内存 options.inSampleSize=inSampleSize; options.inJustDecodeBounds=false; returnBitmapFactory.decodeFile(filePath,options); } publicstaticBitmapgetBitmap(StringfilePath){ returnBitmapFactory.decodeFile(filePath); } publicstaticBitmapview2Bitmap(Viewview){ if(view==null)returnnull; Bitmapret=Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888); Canvascanvas=newCanvas(ret); DrawablebgDrawable=view.getBackground(); if(bgDrawable!=null){ bgDrawable.draw(canvas); }else{ canvas.drawColor(Color.WHITE); } view.draw(canvas); returnret; } publicstaticvoidsaveBitmap(Bitmapbitmap){ Filefile=newFile(Environment.getExternalStorageDirectory()+"/img.jpg"); try{ FileOutputStreamfileOutputStream=newFileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } } publicstaticvoidsaveBitmap(Bitmapbitmap,Bitmap.CompressFormatformat){ Filefile=newFile(Environment.getExternalStorageDirectory()+"/img.jpg"); try{ FileOutputStreamfileOutputStream=newFileOutputStream(file); bitmap.compress(format,100,fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!