生成规格大小的图片,质量压缩
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; }
相关文章推荐
- android 图片质量,大小压缩
- 截图、生成指定大小的图片、压缩图片
- ios-->截图、生成指定大小图片以及压缩
- Android图片压缩-大小与质量压缩
- Android图片质量压缩以及缩略图生成
- android 中图片的质量大小和宽高压缩
- 提供下载:批量图片压缩(生成缩略图或指定大小图片)及文件批量改名器
- 397_压缩图片到一定大小(质量)
- 利用Opencv保存图片,并生成JPG格式,调整压缩质量。
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- 关于图片的压缩(大小压缩和质量压缩)BitmapFactory.Options详解
- Android图片质量压缩以及缩略图生成
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- 如何获取JPEG图片质量和预测压缩图片大小
- java 等比例 改变图片的质量(等大小压缩图片)
- android图片大小的要求质量压缩图片
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- ios-->截图、生成指定大小图片以及压缩
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量