android中bitmap压缩的几种方法的解读
来源:互联网 发布:python ubuntu tts 编辑:程序博客网 时间:2024/04/30 16:21
微信的缩略图要求是不大于32k,这就需要对我的图片进行压缩。试了几种方法,一一道来。
代码如下
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100 , baos);
- int options = 100 ;
- while ( baos.toByteArray().length / 1024 > 32 ) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- options -= 10 ;
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , null );
2、采样率压缩法:
代码如下
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, out);
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- int be = 2;
- newOpts.inSampleSize = be;
- ByteArrayInputStream isBm = new ByteArrayInputStream(out.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , null );
第二个使用的是这个方法,可以将图片压缩到足够小,但是也有一些问题。因为采样率是整数,所以不能很好的保证图片的质量。如我们需要的是在2和3采样率之间,用2的话图片就大了一点,但是用3的话图片质量就会有很明显的下降。这样也无法完全满足我的需要。不过这个方法的好处是大大的缩小了内存的使用,在读存储器上的图片时,如果不需要高清的效果,可以先只读取图片的边,通过宽和高设定好取样率后再加载图片,这样就不会过多的占用内存。如下
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = true ;
- Bitmap bitmap = BitmapFactory.decodeFile(path,newOpts);
- newOpts.inJustDecodeBounds = false ;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
- //计算出取样率
- newOpts.inSampleSize = be;
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
3、缩放法:
以上俩个方法都无法满足要求,只好考虑用缩放来实现。本不想用这种方法来实现,不过网上看到的方法基本都是上面俩种。缩放法其实很简单,设定好matrix,在createBitmap就可以了。但是我们并不知道缩放比例,而是要求了图片的最终大小。直接用大小的比例来做的话肯定是有问题的,用大小比例的开方来做会比较接近,但是还是有差距。但是只要再做一下微调应该就可以了,微调的话就是修改过的图片大小比最终大小还大的话,就进行0.8的压缩再比较,循环直到大小合适。这样就能得到合适大小的图片,而且也能比较保证质量。代码如下
ByteArrayOutputStream out = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 85, out); float zoom = (float)Math.sqrt(size * 1024 / (float)out.toByteArray().length); Matrix matrix = new Matrix(); matrix.setScale(zoom, zoom); Bitmap result = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); out.reset(); result.compress(Bitmap.CompressFormat.JPEG, 85, out); while(out.toByteArray().length > size * 1024){ System.out.println(out.toByteArray().length); matrix.setScale(0.9f, 0.9f); result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true); out.reset(); result.compress(Bitmap.CompressFormat.JPEG, 85, out); }
感觉在bitmap压缩这块应该有更有效更方便的方法,可惜网上没有找到,自己对这方面的理解可能也不够。有更好的方法,或者文章有错误的地方,希望大家来指正。
0 0
- android中bitmap压缩的几种方法的解读
- android中bitmap压缩的几种方法的解读
- android中bitmap压缩的几种方法的解读
- android中bitmap压缩的几种方法的解读
- Android之bitmap压缩的几种方法的解读
- android中bitmap压缩的几种方法详解
- Android中bitmap的压缩
- Android中压缩图片的几种方法
- android中压缩图片的几种方法比较
- Bitmap压缩的几种方式
- Android压缩图片的几种方法
- Android压缩图片的几种方法
- android 压缩图片的几种方法
- Android压缩图片的几种方法
- Android压缩图片的几种方法
- Android图片压缩的几种方法
- android bitmap压缩几种色彩详解
- Bitmap对象的几种获取方法
- 设计模式之十九:命令模式(Command)
- Struts2+Spring3 基于注解的配置
- 设计模式六大原则(4):接口隔离原则
- 【Unity3d】如何解决在开发中添加了新场景后,需要手工维护BuildSettings中场景列表的问题
- Android SDK国内下载方式
- android中bitmap压缩的几种方法的解读
- 嵌入式linux入门之c语言学习注意事项总结(一)
- 面板数据分析plm
- 贝勒爷教你如何在Mac上安装Microsoft Office
- 设计模式六大原则(5):迪米特法则
- 最短路径
- C# 反射机制(转)
- 怎么看射频信号大小
- MD5算法原理