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. 【引用】Linux 内核驱动--多点触摸接口

    本文转载自James<Linux 内核驱动--多点触摸接口>   译自:linux-2.6.31.14\Documentation\input\multi-touch-protocol.t ...

  2. 转:Bootstrap研究 精巧的网格布局系统

    本网格布局系统属于Scaffolding(框架,布局)部分.在Scaffolding里面有(固定)网格布局(Grid System)和流式网格布局(Fluid Grid System).本文讨论第一种 ...

  3. 一步一步重写 CodeIgniter 框架 (11) —— 使用 CodeIgniter 函数库

    在完成了CI框架的类库扩展后,很自然我们就会想到函数库的扩展.函数库的扩展在 CI 中称为 helper 函数与类有不同的地方,它不能继承,只能覆盖或者添加新的函数,或者直接完全新定义的一组函数. 由 ...

  4. sass玩转颜色总结笔记

    变量: $color:#f00; 1.变浅和加深颜色,sass使用HSL标准来变浅或加深颜色 lighten($color,10%); darken($color,30%);             ...

  5. C语言数据结构-创建链表的四种方法

    结点类型: typedef int datatype; typedef struct NODE{ datatype data; struct NODE *next; }Node,*LinkList; ...

  6. 给VS自动添加注释

    找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...

  7. 几种能在O(n*log(n))时间排序

    线性时间排序   各种排序算法总结已经介绍了几种能在O(n*log(n))时间内培训n个数的算法.归并排序和堆排序达到了最坏情况下的上界:快速排序在平均情况下达到该上界.这些算法都有一个有趣的性质:在 ...

  8. 在大型项目上,Python 是个烂语言吗

    Robert Love, Google Software Engineer and Manager on Web Search. Upvoted by Kah Seng Tay, I was the ...

  9. MDCC为移动开发者服务:一看、一聊、一聚

    MDCC为移动开发者服务:一看.一聊.一聚-CSDN.NET     MDCC为移动开发者服务:一看.一聊.一聚    发表于2013-11-05 20:54| 2698次阅读| 来源CSDN| 6 ...

  10. [免费活动通知]RAD Studio XE8 技术研讨会(上海、成都)

     活动类型:免费研讨会 报名链接: http://forms.embarcadero.com/AP15Q3CNRADStudioDeepDiveSeminar 上海 2015 年 8 月 13 日 ...