android 图片压缩方法:

第一:质量压缩法:

private Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
image.compress(Bitmap.CompressFormat.JPEG, 100,
baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options =
100;
        while ( baos.toByteArray().length / 1024>100) {   
//循环判断如果压缩后图片是否大于100kb,大于继续压缩       
           
baos.reset();//重置baos即清空baos
            options -=
10;//每次都减少10
            image.compress(Bitmap.CompressFormat.JPEG, options,
baos);//这里压缩options%,把压缩后的数据存放到baos中

}
       
ByteArrayInputStream isBm = new
ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
       
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null,
null);//把ByteArrayInputStream数据生成图片
        return bitmap;
   
}

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

private Bitmap getimage(String srcPath)
{
        BitmapFactory.Options newOpts = new
BitmapFactory.Options();
        //开始读入图片,此时把options.inJustDecodeBounds
设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap =
BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空

newOpts.inJustDecodeBounds = false;
        int w =
newOpts.outWidth;
        int h = newOpts.outHeight;
       
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
        float hh =
800f;//这里设置高度为800f
        float ww = 480f;//这里设置宽度为480f
       
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
       
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be =
(int) (newOpts.outWidth / ww);
        } else if (w < h && h >
hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight /
hh);
        }
        if (be <= 0)
            be = 1;
       
newOpts.inSampleSize = be;//设置缩放比例
       
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap =
BitmapFactory.decodeFile(srcPath, newOpts);
        return
compressImage(bitmap);//压缩好比例大小后再进行质量压缩
    }

图片比例压缩时, 我看到一个算法,说比较快。。
be = (int) ((w / STANDARD_WIDTH + h/
STANDARD_HEIGHT) / 2);
结论二:图片比例压缩倍数 就是 (宽度压缩倍数+高度压缩倍数)/2..

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

private Bitmap comp(Bitmap image)
{

ByteArrayOutputStream baos = new
ByteArrayOutputStream();       
       
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        if(
baos.toByteArray().length / 1024>1024)
{//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出   
           
baos.reset();//重置baos即清空baos
           
image.compress(Bitmap.CompressFormat.JPEG, 50,
baos);//这里压缩50%,把压缩后的数据存放到baos中
        }
        ByteArrayInputStream
isBm = new ByteArrayInputStream(baos.toByteArray());
       
BitmapFactory.Options newOpts = new BitmapFactory.Options();
       
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
       
newOpts.inJustDecodeBounds = true;
        Bitmap bitmap =
BitmapFactory.decodeStream(isBm, null, newOpts);
       
newOpts.inJustDecodeBounds = false;
        int w =
newOpts.outWidth;
        int h = newOpts.outHeight;
       
//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
        float hh =
800f;//这里设置高度为800f
        float ww = 480f;//这里设置宽度为480f
       
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
       
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be =
(int) (newOpts.outWidth / ww);
        } else if (w < h && h >
hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight /
hh);
        }
        if (be <= 0)
            be = 1;
       
newOpts.inSampleSize = be;//设置缩放比例
        newOpts.inPreferredConfig =
Config.RGB_565;//降低图片从ARGB888到RGB565
       
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        isBm = new
ByteArrayInputStream(baos.toByteArray());
        bitmap =
BitmapFactory.decodeStream(isBm, null, newOpts);
        return
compressImage(bitmap);//压缩好比例大小后再进行质量压缩
    }

android图片压缩的3种方法实例的更多相关文章

  1. android图片压缩方法

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  2. Android 图片压缩各种方式

       前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...

  3. Android 图片压缩器

    概述 Android 图片压缩器:一款高效的图片压缩器库,支持批量压缩,异步压缩.多线程多任务压缩,压缩比设置等特性. 详细 代码下载:http://www.demodashi.com/demo/12 ...

  4. Android图片压缩上传(二)

    之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...

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

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

  6. 【转】c# Image获得图片路径的三种方法 winform

    代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...

  7. Android 抗锯齿的两种方法

    Android 抗锯齿的两种方法 (其一:paint.setAntiAlias(ture);paint.setBitmapFilter(true))   在Android中,目前,我知道有两种出现锯齿 ...

  8. android emulator启动的两种方法详解

    android emulator启动的两种方法详解    转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...

  9. c# pictureBox1.Image的获得图片路径的三种方法 winform

    代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...

随机推荐

  1. JSON数组分配输出每个li

    有这么一个JSON数组,需求是只需要输出每个数组里面的某个值,不需要全部输出来. var data = [ { ", "Cost":"13,642.41&quo ...

  2. C#委托好处知多少

    1.性能 性能是泛型的一个主要优点. 直接上例子,通过实例可以让我们很好的理解这一点. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start() ...

  3. [置顶] Android自定义控件大全

    1,自定义Edittext, TextView,带底线的Edittext, TextView 2.自定义圆形ImageView,圆角ImageView 3,下拉刷新LinearLayout 4,多点触 ...

  4. 我的Python成长之路---第一天---Python基础(3)---2015年12月26日(雾霾)

    四.变量和常量 变量是用来存储程序运行期间需要临时保存可以不断改变的数据的标识符.Python有自身的内存回收机制,所以在开发过程中不用考虑变量的销毁等 Python中的变量名命名有如下规则: 1.变 ...

  5. 动态规划之一ones

    n给一个整数n,要你找一个值为n的表达式,这个表达式只有1 + * ( ) 够成.并且1不能连续,比如11+1就不合法. n输入n,(1<=n<=10000) n输出最少需要多少个1才能构 ...

  6. Linux 命令整理

    一.文件目录命令 1.建立目录:mkdir 目录名 2.删除空目录:rmdir 目录名 3.无条件删除子目录: rm -rf 目录名 4.改变当前目录:cd 目录名 (进入用户home目录:cd ~; ...

  7. keepalived 结合mysql 自动切换

    启动keepalived:/usr/local/sbin/keepalived -D -d -S 0 master ip:192.168.32.6 master:/root/sbin# cat /et ...

  8. set global read_only=0; 关闭只读,可以读写 set global read_only=1; 开始只读模式

    mysql> set global read_only=0; Query OK, 0 rows affected (0.00 sec) mysql> show variables like ...

  9. python核心编程--笔记

    python核心编程--笔记 的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找pyt ...

  10. VirtualBox安装及使用说明和虚拟机安装XP系统图文教程

    virtualbox是一款开源的虚拟机软件,它能够支持多种操作系统的安装如:Solaris.Windows.DOS.Linux.OS/2 Warp.BSD等系统作为client操作系统,而且最新版本号 ...