Android图片压缩(质量压缩和尺寸压缩)
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width*height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
最后把自己总结的工具类贴出来:
importjavaioByteArrayInputStream;
importjavaioByteArrayOutputStream;
importjavaioFile;
importjavaioFileNotFoundException;
importjavaioFileOutputStream;
importjavaioIOException;
importandroidgraphicsBitmap;
importandroidgraphicsBitmapConfig;
importandroidgraphicsBitmapFactory;
/**
*Imagecompressfactoryclass
*
*@author
*
*/
publicclassImageFactory{
/**
*Getbitmapfromspecifiedimagepath
*
*@paramimgPath
*@return
*/
publicBitmapgetBitmap(StringimgPath){
//Getbitmapthroughimagepath
BitmapFactoryOptionsnewOpts=newBitmapFactoryOptions();
newOptsinJustDecodeBounds=false;
newOptsinPurgeable=true;
newOptsinInputShareable=true;
//Donotcompress
newOptsinSampleSize=1;
newOptsinPreferredConfig=ConfigRGB_565;
returnBitmapFactorydecodeFile(imgPath,newOpts);
}
/**
*Storebitmapintospecifiedimagepath
*
*@parambitmap
*@paramoutPath
*@throwsFileNotFoundException
*/
publicvoidstoreImage(Bitmapbitmap,StringoutPath)throwsFileNotFoundException{
FileOutputStreamos=newFileOutputStream(outPath);
bitmapcompress(BitmapCompressFormatJPEG,100,os);
}
/**
*Compressimagebypixel,thiswillmodifyimagewidth/height
*Usedtogetthumbnail
*
*@paramimgPathimagepath
*@parampixelWtargetpixelofwidth
*@parampixelHtargetpixelofheight
*@return
*/
publicBitmapratio(StringimgPath,floatpixelW,floatpixelH){
BitmapFactoryOptionsnewOpts=newBitmapFactoryOptions();
//开始读入图片,此时把optionsinJustDecodeBounds设回true,即只读边不读内容
newOptsinJustDecodeBounds=true;
newOptsinPreferredConfig=ConfigRGB_565;
//Getbitmapinfo,butnoticethatbitmapisnullnow
Bitmapbitmap=BitmapFactorydecodeFile(imgPath,newOpts);
newOptsinJustDecodeBounds=false;
intw=newOptsoutWidth;
inth=newOptsoutHeight;
//想要缩放的目标尺寸
floathh=pixelH;//设置高度为240f时,可以明显看到图片缩小了
floatww=pixelW;//设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
intbe=1;//be=1表示不缩放
if(w>h&&w>ww){//如果宽度大的话根据宽度固定大小缩放
be=(int)(newOptsoutWidth/ww);
}elseif(w<h&&h>hh){//如果高度高的话根据宽度固定大小缩放
be=(int)(newOptsoutHeight/hh);
}
if(be<=0)be=1;
newOptsinSampleSize=be;//设置缩放比例
//开始压缩图片,注意此时已经把optionsinJustDecodeBounds设回false了
bitmap=BitmapFactorydecodeFile(imgPath,newOpts);
//压缩好比例大小后再进行质量压缩
//returncompress(bitmap,maxSize);//这里再进行质量压缩的意义不大,反而耗资源,删除
returnbitmap;
}
/**
*Compressimagebysize,thiswillmodifyimagewidth/height
*Usedtogetthumbnail
*
*@paramimage
*@parampixelWtargetpixelofwidth
*@parampixelHtargetpixelofheight
*@return
*/
publicBitmapratio(Bitmapimage,floatpixelW,floatpixelH){
ByteArrayOutputStreamos=newByteArrayOutputStream();
imagecompress(BitmapCompressFormatJPEG,100,os);
if(ostoByteArray()length/1024>1024){//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactorydecodeStream)时溢出
osreset();//重置baos即清空baos
imagecompress(BitmapCompressFormatJPEG,50,os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStreamis=newByteArrayInputStream(ostoByteArray());
BitmapFactoryOptionsnewOpts=newBitmapFactoryOptions();
//开始读入图片,此时把optionsinJustDecodeBounds设回true了
newOptsinJustDecodeBounds=true;
newOptsinPreferredConfig=ConfigRGB_565;
Bitmapbitmap=BitmapFactorydecodeStream(is,null,newOpts);
newOptsinJustDecodeBounds=false;
intw=newOptsoutWidth;
inth=newOptsoutHeight;
floathh=pixelH;//设置高度为240f时,可以明显看到图片缩小了
floatww=pixelW;//设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
intbe=1;//be=1表示不缩放
if(w>h&&w>ww){//如果宽度大的话根据宽度固定大小缩放
be=(int)(newOptsoutWidth/ww);
}elseif(w<h&&h>hh){//如果高度高的话根据宽度固定大小缩放
be=(int)(newOptsoutHeight/hh);
}
if(be<=0)be=1;
newOptsinSampleSize=be;//设置缩放比例
//重新读入图片,注意此时已经把optionsinJustDecodeBounds设回false了
is=newByteArrayInputStream(ostoByteArray());
bitmap=BitmapFactorydecodeStream(is,null,newOpts);
//压缩好比例大小后再进行质量压缩
//returncompress(bitmap,maxSize);//这里再进行质量压缩的意义不大,反而耗资源,删除
returnbitmap;
}
/**
*Compressbyquality,andgenerateimagetothepathspecified
*
*@paramimage
*@paramoutPath
*@parammaxSizetargetwillbecompressedtobesmallerthanthissize(kb)
*@throwsIOException
*/
publicvoidcompressAndGenImage(Bitmapimage,StringoutPath,intmaxSize)throwsIOException{
ByteArrayOutputStreamos=newByteArrayOutputStream();
//scale
intoptions=100;
//Storethebitmapintooutputstream(nocompress)
imagecompress(BitmapCompressFormatJPEG,options,os);
//Compressbyloop
while(ostoByteArray()length/1024>maxSize){
//Cleanupos
osreset();
//interval10
options-=10;
imagecompress(BitmapCompressFormatJPEG,options,os);
}
//Generatecompressedimagefile
FileOutputStreamfos=newFileOutputStream(outPath);
foswrite(ostoByteArray());
fosflush();
fosclose();
}
/**
*Compressbyquality,andgenerateimagetothepathspecified
*
*@paramimgPath
*@paramoutPath
*@parammaxSizetargetwillbecompressedtobesmallerthanthissize(kb)
*@paramneedsDeleteWhetherdeleteoriginalfileaftercompress
*@throwsIOException
*/
publicvoidcompressAndGenImage(StringimgPath,StringoutPath,intmaxSize,booleanneedsDelete)throwsIOException{
compressAndGenImage(getBitmap(imgPath),outPath,maxSize);
//Deleteoriginalfile
if(needsDelete){
Filefile=newFile(imgPath);
if(fileexists()){
filedelete();
}
}
}
/**
*Ratioandgeneratethumbtothepathspecified
*
*@paramimage
*@paramoutPath
*@parampixelWtargetpixelofwidth
*@parampixelHtargetpixelofheight
*@throwsFileNotFoundException
*/
publicvoidratioAndGenThumb(Bitmapimage,StringoutPath,floatpixelW,floatpixelH)throwsFileNotFoundException{
Bitmapbitmap=ratio(image,pixelW,pixelH);
storeImage(bitmap,outPath);
}
/**
*Ratioandgeneratethumbtothepathspecified
*
*@paramimage
*@paramoutPath
*@parampixelWtargetpixelofwidth
*@parampixelHtargetpixelofheight
*@paramneedsDeleteWhetherdeleteoriginalfileaftercompress
*@throwsFileNotFoundException
*/
publicvoidratioAndGenThumb(StringimgPath,StringoutPath,floatpixelW,floatpixelH,booleanneedsDelete)throwsFileNotFoundException{
Bitmapbitmap=ratio(imgPath,pixelW,pixelH);
storeImage(bitmap,outPath);
//Deleteoriginalfile
if(needsDelete){
Filefile=newFile(imgPath);
if(fileexists()){
filedelete();
}
}
}
}
android图片压缩总结
一.图片的存在形式
1.文件形式(即以二进制形式存在于硬盘上)
2.流的形式(即以二进制形式存在于内存中)
3.Bitmap形式
这三种形式的区别:文件形式和流的形式对图片体积大小并没有影响,也就是说,如果你手机SD卡上的如果是100K,那么通过流的形式读到内存中,也一定是占100K的内存,注意是流的形式,不是Bitmap的形式,当图片以Bitmap的形式存在时,其占用的内存会瞬间变大,我试过500K文件形式的图片加载到内存,以Bitmap形式存在时,占用内存将近10M,当然这个增大的倍数并不是固定的
检测图片三种形式大小的方法:
文件形式:file.length()
流的形式:讲图片文件读到内存输入流中,看它的byte数
Bitmap: bitmap.getByteCount()
二.常见的压缩方式
1.将图片保存到本地时进行压缩,即将图片从Bitmap形式变为File形式时进行压缩,
特点是: File形式的图片确实被压缩了,但是当你重新读取压缩后的file为Bitmap是,它占用的内存并没有改变
方法说明:该方法是压缩图片的质量,注意它不会减少图片的像素,比方说,你的图片是300K的,1280*700像素的,经过该方法压缩后,File形式的图片是在100以下,以方便上传服务器,但是你BitmapFactory.decodeFile到内存中,变成Bitmap时,它的像素仍然是1280*700,计算图片像素的方法是bitmap.getWidth()和bitmap.getHeight(),图片是由像素组成的,每个像素又包含什么呢?熟悉PS的人知道,图片是有色相,明度和饱和度构成的.
publicstaticvoidcompressBmpToFile(Bitmapbmp,Filefile){
ByteArrayOutputStreambaos=newByteArrayOutputStream();
intoptions=80;//个人喜欢从80开始,
bmp.compress(Bitmap.CompressFormat.JPEG,options,baos);
while(baos.toByteArray().length/1024>100){
baos.reset();
options-=10;
bmp.compress(Bitmap.CompressFormat.JPEG,options,baos);
}
try{
FileOutputStreamfos=newFileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
}catch(Exceptione){
e.printStackTrace();
}
}
该方法的官方文档也解释说,它会让图片重新构造,但是有可能图像的位深(即色深)和每个像素的透明度会变化,JPEGonlysupportsopaque(不透明),也就是说以jpeg格式压缩后,原来图片中透明的元素将消失.所以这种格式很可能造成失真
既然它是改变了图片的显示质量,达到了对File形式的图片进行压缩,图片的像素没有改变的话,那重新读取经过压缩的file为Bitmap时,它占用的内存并不会少.(不相信的可以试试)
因为:bitmap.getByteCount()是计算它的像素所占用的内存,请看官方解释:Returnsthenumberofbytesusedtostorethisbitmap'spixels.
2. 将图片从本地读到内存时,进行压缩,即图片从File形式变为Bitmap形式
特点:通过设置采样率,减少图片的像素,达到对内存中的Bitmap进行压缩
先看一个方法:该方法是对内存中的Bitmap进行质量上的压缩,由上面的理论可以得出该方法是无效的,而且也是没有必要的,因为你已经将它读到内存中了,再压缩多此一举,尽管在获取系统相册图片时,某些手机会直接返回一个Bitmap,但是这种情况下,返回的Bitmap都是经过压缩的,它不可能直接返回一个原声的Bitmap形式的图片,后果可想而知
方法说明:该方法就是对Bitmap形式的图片进行压缩,也就是通过设置采样率,减少Bitmap的像素,从而减少了它所占用的内存
privateBitmapcompressBmpFromBmp(Bitmapimage){
ByteArrayOutputStreambaos=newByteArrayOutputStream();
intoptions=100;
image.compress(Bitmap.CompressFormat.JPEG,100,baos);
while(baos.toByteArray().length/1024>100){
baos.reset();
options-=10;
image.compress(Bitmap.CompressFormat.JPEG,options,baos);
}
ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray());
Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null);
returnbitmap;
}
再看一个方法:
privateBitmapcompressImageFromFile(StringsrcPath){
BitmapFactory.OptionsnewOpts=newBitmapFactory.Options();
newOpts.inJustDecodeBounds=true;//只读边,不读内容
Bitmapbitmap=BitmapFactory.decodeFile(srcPath,newOpts);
newOpts.inJustDecodeBounds=false;
intw=newOpts.outWidth;
inth=newOpts.outHeight;
floathh=800f;//
floatww=480f;//
intbe=1;
if(w>h&&w>ww){
be=(int)(newOpts.outWidth/ww);
}elseif(w<h&&h>hh){
be=(int)(newOpts.outHeight/hh);
}
if(be<=0)
be=1;
newOpts.inSampleSize=be;//设置采样率
newOpts.inPreferredConfig=Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable=true;//同时设置才会有效
newOpts.inInputShareable=true;//。当系统内存不够时候图片自动被回收
bitmap=BitmapFactory.decodeFile(srcPath,newOpts);
//returncompressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
//其实是无效的,大家尽管尝试
returnbitmap;
}
分享个按照图片尺寸压缩:
publicstaticvoidcompressPicture(StringsrcPath,StringdesPath){
FileOutputStreamfos=null;
BitmapFactoryOptionsop=newBitmapFactoryOptions();
//开始读入图片,此时把optionsinJustDecodeBounds设回true了
opinJustDecodeBounds=true;
Bitmapbitmap=BitmapFactorydecodeFile(srcPath,op);
opinJustDecodeBounds=false;
//缩放图片的尺寸
floatw=opoutWidth;
floath=opoutHeight;
floathh=1024f;//
floatww=1024f;//
//最长宽度或高度1024
floatbe=0f;
if(w>h&&w>ww){
be=(float)(w/ww);
}elseif(w<h&&h>hh){
be=(float)(h/hh);
}
if(be<=0){
be=0f;
}
opinSampleSize=(int)be;//设置缩放比例,这个数字越大,图片大小越小
//重新读入图片,注意此时已经把optionsinJustDecodeBounds设回false了
bitmap=BitmapFactorydecodeFile(srcPath,op);
intdesWidth=(int)(w/be);
intdesHeight=(int)(h/be);
bitmap=BitmapcreateScaledBitmap(bitmap,desWidth,desHeight,true);
try{
fos=newFileOutputStream(desPath);
if(bitmap!=null){
bitmapcompress(BitmapCompressFormatJPEG,100,fos);
}
}catch(FileNotFoundExceptione){
eprintStackTrace();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。