您的位置:首页 > 其它

生成规格大小的图片,质量压缩

2014-07-18 02:58 387 查看
  

代码来自与 :    http://www.apkbus.com/forum.php?mod=viewthread&tid=171706 

packagecom.maizi.util;

importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;

importandroid.content.res.Resources;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;

/***
*
*@Description:(这里用一句话描述这个类的作用)图片处理类
*@author麦子
*@date2014-7-18下午1:05:58
*@version1.01
*/
publicclassBitmapUtils{

/***
*
*@date2014-7-18下午1:10:12获取缩放的比列值
*@Description:(这里用一句话描述这个方法的作用)
*@author麦子
*/
publicstaticintcalculateInSampleSize(BitmapFactory.Optionsoptions,intreqWidth,intreqHeight){
intinSampleSize=1;
if(options!=null){
//源图片的高度和宽度
finalintheight=options.outHeight;
finalintwidth=options.outWidth;
if(height>reqHeight||width>reqWidth){
finalintheightRatio=Math.round((float)height/(float)reqHeight);
finalintwidthRatio=Math.round((float)width/(float)reqWidth);
//选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
//一定都会大于等于目标的宽和高。
inSampleSize=heightRatio<widthRatio?heightRatio:widthRatio;
returninSampleSize;
}
}
returninSampleSize;
}

/***
*
*@date2014-7-18下午1:22:05获取规定规格的图片
*@Description:(这里用一句话描述这个方法的作用)
*@author麦子
*/
publicstaticBitmapdecodeSampledBitmapFromPath(Resourcesres,intresId,intreqWidth,intreqHeight){
Bitmapbitmap=null;
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(res,resId,options);
options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds=false;
bitmap=BitmapFactory.decodeResource(res,resId,options);
returnbitmap;
}

/**
*
*@date2014-7-18下午2:43:34
*@Description:(这里用一句话描述这个方法的作用)质量压缩
*@author麦子
*/
publicstaticBitmapcompressImage(Bitmapimage,intsize){
ByteArrayOutputStreambaos=newByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
intoptions=100;
while(baos.toByteArray().length/1024>size){	//循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG,options,baos);//这里压缩options%,把压缩后的数据存放到baos中
options-=10;//每次都减少10
}
ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream数据生成图片
returnbitmap;
}

}
</pre><prename="code"class="java">在页面中对ImageView进行图像设置的时候,不要吧Bitmap转成BitmapDrawable,然后通过setBackground这个方法来渲染ImageView图片显示,这样的方式会有问题,<spanstyle="font-family:Arial,Helvetica,sans-serif;">如果图片的规格比这个原来需要的规格要小的时候,图片总是会自动的进行缩小,为什么会这样,不是很清楚。<prename="code"class="java">setBackground和setImageBitmap区别到底是什么?</span>
<prename="code"class="java">/**
*
*@date2014-7-19上午11:52:02按比例缩放原来的Bitmap,生成规格Bitmap
*@Description:(这里用一句话描述这个方法的作用)
*@author麦子
*/
publicstaticBitmapcreateBitmapBySize(Bitmapbitmap,intwidth,intheight){
BitmapnewBitmap=null;
if(bitmap!=null){
newBitmap=Bitmap.createScaledBitmap(bitmap,width,height,true);
if(bitmap!=null&&!bitmap.isRecycled()){
bitmap.recycle();
}
}
returnnewBitmap;
}
/**
*
*@date2014-7-20上午9:21:02
*@Description:(这里用一句话描述这个方法的作用)图片缩放
*@author麦子
*/
publicstaticBitmapzoomBitmap(Bitmapbitmap,intwidth,intheight){
if(bitmap==null){
returnnull;
}
intw=bitmap.getWidth();
inth=bitmap.getHeight();
Matrixmatrix=newMatrix();
floatscaleWidth=((float)width/w);
floatscaleHeight=((float)height/h);
matrix.postScale(scaleWidth,scaleHeight);
Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,true);
returnnewbmp;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息