Android图片压缩
来源:互联网 发布:js返回根目录 编辑:程序博客网 时间:2024/06/05 21:14
多种压缩方式常用的有尺寸压缩、质量压缩以及通过JNI调用libjpeg库来进行压缩,三种方式结合使用实现指定图片内存大小,清晰度达到最优,下面就先分别介绍下这几种压缩方式。
1. 质量压缩
设置bitmap options属性,降低图片的质量,像素不会减少
第一个参数为需要压缩的bitmap图片对象,第二个参数为压缩后图片保存的位置
设置options 属性0-100,来实现压缩
public static void compressImageToFile(Bitmap bmp,File file) { // 0-100 100为不压缩 int options = 100; ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 把压缩后的数据存放到baos中 bmp.compress(Bitmap.CompressFormat.JPEG, options, baos); try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
2. 尺寸压缩
通过缩放图片像素来减少图片占用内存大小
public static void compressBitmapToFile(Bitmap bmp, File file){ // 尺寸压缩倍数,值越大,图片尺寸越小 int ratio = 2; // 压缩Bitmap到对应尺寸 Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888); Canvas canvas = new Canvas(result); Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio); canvas.drawBitmap(bmp, null, rect, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 把压缩后的数据存放到baos中 result.compress(Bitmap.CompressFormat.JPEG, 100 ,baos); try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
设置图片的采样率,降低图片像素
public static void compressBitmap(String filePath, File file){ // 数值越高,图片像素越低 int inSampleSize = 2; BitmapFactory.Options options = new BitmapFactory.Options(); //采样率 options.inSampleSize = inSampleSize; Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 把压缩后的数据存放到baos中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100 ,baos); try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
0 0
- android图片压缩方法
- android图片压缩方法
- android图片压缩
- android图片压缩
- Android图片压缩
- android图片压缩
- Android BitmapFactory 图片压缩
- android图片压缩
- android图片压缩总结
- android图片压缩一:
- Android 图片压缩二:
- Android图片压缩总结
- Android开发压缩图片
- Android图片压缩
- android 的图片压缩
- Android关于图片压缩
- android 图片压缩
- android图片压缩上传
- android DialogFragment 创建对话框(官方推荐)
- 复选框读取数据,分割字符串转换成数组,显示复选框选中。
- java 类的加载、链接和初始化(静态属性的加载顺序)
- 1.1web初探
- android RecyclerView使用及详解
- Android图片压缩
- android ScrollView常用属性
- Android通知(Notification)使用详解
- js 全局函数一览
- Android Studio突然不显示logcat日志的解决办法
- 09:移动路线
- ZooKeeper的安装与部署
- jQuery toggle trigger方法使用
- java 接口回调机制