第一:我们先看下质量压缩方法:

  1. private Bitmap compressImage(Bitmap image) {
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  4. int options = 100;
  5. while ( baos.toByteArray().length / 1024>100) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩
  6. baos.reset();//重置baos即清空baos
  7. image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
  8. options -= 10;//每次都减少10
  9. }
  10. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  11. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
  12. return bitmap;
  13. }

第二:图片按比例大小压缩方法(根据路径获取图片并压缩):

  1. private Bitmap getimage(String srcPath) {
  2. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  3. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  4. newOpts.inJustDecodeBounds = true;
  5. Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空
  6. newOpts.inJustDecodeBounds = false;
  7. int w = newOpts.outWidth;
  8. int h = newOpts.outHeight;
  9. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  10. float hh = 800f;//这里设置高度为800f
  11. float ww = 480f;//这里设置宽度为480f
  12. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  13. int be = 1;//be=1表示不缩放
  14. if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
  15. be = (int) (newOpts.outWidth / ww);
  16. } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
  17. be = (int) (newOpts.outHeight / hh);
  18. }
  19. if (be <= 0)
  20. be = 1;
  21. newOpts.inSampleSize = be;//设置缩放比例
  22. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  23. bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
  24. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  25. }

第三:图片按比例大小压缩方法(根据Bitmap图片压缩):

  1. private Bitmap comp(Bitmap image) {
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  4. if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
  5. baos.reset();//重置baos即清空baos
  6. image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中
  7. }
  8. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  9. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  10. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  11. newOpts.inJustDecodeBounds = true;
  12. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  13. newOpts.inJustDecodeBounds = false;
  14. int w = newOpts.outWidth;
  15. int h = newOpts.outHeight;
  16. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  17. float hh = 800f;//这里设置高度为800f
  18. float ww = 480f;//这里设置宽度为480f
  19. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  20. int be = 1;//be=1表示不缩放
  21. if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
  22. be = (int) (newOpts.outWidth / ww);
  23. } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
  24. be = (int) (newOpts.outHeight / hh);
  25. }
  26. if (be <= 0)
  27. be = 1;
  28. newOpts.inSampleSize = be;//设置缩放比例
  29. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  30. isBm = new ByteArrayInputStream(baos.toByteArray());
  31. bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  32. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  33. }

android 比较靠谱的图片压缩的更多相关文章

  1. (转)Android学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

  2. Android 调用jepg库进行图片压缩,保持图片不失真

    1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...

  3. android apk瘦身之 图片压缩 tinypng

    参考地址: http://blog.csdn.net/jy692405180/article/details/52409369 http://www.tuicool.com/articles/BraI ...

  4. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  5. Android 拍照后保证保证图片不失真,进行压缩

    今天在网上找了一下参考,得出把图片压缩至KB 其他不想多说.直接上代码 拍完照后调用下面代码 BitmapUtils.compressBitmap(photoPath, photoPath, 640) ...

  6. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩).   第一:质量压缩方法:   ? 1 2 3 ...

  7. Android webview实现上传图片的效果(图片压缩)

    mainactivity代码 package com.bwie.webviewupload; import java.io.ByteArrayInputStream; import java.io.B ...

  8. Android中的图片压缩

    1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...

  9. Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...

随机推荐

  1. Lintcode: Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  2. Oracle-数据库

    Oracle 1.特点 关系型数据库 采用二维表的行使管理数据库 具有行和列  表间存在关联关系 2.安装 数据库(11g) 版本类型 32位    64位  安装类型 桌面类 本机开发 服务器类 生 ...

  3. android测试(转)

    1.冒烟测试 跟web端的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做我们的测试工具.就跟我之前博客所 ...

  4. sql server 查询性能最差的sql语句

    SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_logical_ ...

  5. [py]安装ipython

    系统:crunch bang11+python2.7.3 准备工具: sudo apt-get install python-pip python-dev build-essential 安装setu ...

  6. ubuntu下配置SVN服务器

    自己买的阿里云服务器.可是我老感觉没有SVN上传代码下载代码太不方便!决定配置个SVN服务器! 1.安装Subversion $ sudo apt-get install subversion $ s ...

  7. OpenStack 的防火墙规则流程

    Contents [hide] 1 发现的问题 2 解决过程 3 删除临时错误数据 4 其实前面的解决办法是错的 发现的问题 3台虚拟机在同一宿主机,防火墙配置都一样,但是他们的网络表现不一致,有的能 ...

  8. ubuntu apache开启重写模块

    http://www.iblue.cc/2011/09/ubuntu-apache%E5%BC%80%E5%90%AF%E9%87%8D%E5%86%99%E6%A8%A1%E5%9D%97/ Ubu ...

  9. ThinkPHP 3.2.3的 R 方法

    R方法是可以调用其他的Controller中的方法, 例如 我想在Mit/DebugController.class.php中调用Foo/DoController.class.php中的share方法 ...

  10. 不同版本(2.3,2.4,2.5)的Servlet web.xml 头信息

    Servlet 2.3<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app    P ...