android图片压缩的3种方法实例
android 图片压缩方法:
第一:质量压缩法:
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;
}
第二:图片按比例大小压缩方法(根据路径获取图片并压缩):
{
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图片压缩):
{
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种方法实例的更多相关文章
- android图片压缩方法
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- Android 图片压缩各种方式
前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...
- Android 图片压缩器
概述 Android 图片压缩器:一款高效的图片压缩器库,支持批量压缩,异步压缩.多线程多任务压缩,压缩比设置等特性. 详细 代码下载:http://www.demodashi.com/demo/12 ...
- Android图片压缩上传(二)
之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...
- 【转】c# Image获得图片路径的三种方法 winform
代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...
- Android 抗锯齿的两种方法
Android 抗锯齿的两种方法 (其一:paint.setAntiAlias(ture);paint.setBitmapFilter(true)) 在Android中,目前,我知道有两种出现锯齿 ...
- android emulator启动的两种方法详解
android emulator启动的两种方法详解 转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...
- c# pictureBox1.Image的获得图片路径的三种方法 winform
代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...
随机推荐
- android Bitmap围绕一个点进行旋转
在项目中需要使用定位功能,也就是一个点围绕一个圆心进行旋转,查看了canvas的函数也就只有一个 canvas.drawBitmap(bitmap, matrix, paint)通过使用Matrix来 ...
- Android跟蓝牙耳机建立连接有两种方式
Android 跟蓝牙耳机建立连接有两种方式. 1. Android 主动跟蓝牙耳机连BluetoothSettings 中和蓝牙耳机配对上之后, BluetoothHeadsetService 会收 ...
- 使用webview来查看网站
1.权限 <uses-permission android:name="android.permission.INTERNET"/> 2.视图 <Relative ...
- [置顶] 深入浅出MongoDB(三)环境搭建
上次的博文深入浅出MongoDB(二)概述中我们已经将MongoDB的相关概念讲解了一下,接下来我们继续进行MongoDB学习.在学习之前,大家首先需要在自己的电脑上安装MongoDB. 1.安装 安 ...
- linux eclipse中运行android AVD 错误
当使用android的AVD时提示以下错误: Starting emulator for AVD 'NexusOne' ERROR: 32-bit Linux Android emulator bin ...
- 从 Qt 的 delete 说开来
原地址:http://blog.csdn.net/dbzhang800/article/details/6300025 在C++中学习过程中,我们都知道: delete 和 new 必须 配对使用(一 ...
- 1、Zookeeper熟悉和用途综述
集群 配置: 192.168.32.80 192.168.32.81 192.168.32.82 server 1: zjtest7-redis:/opt/zookeeper/conf# cat zo ...
- SAP超时问题
其他常用的参数如下: login/system_client 登录时默认的Client号 login/password_expiration_time 密码有效期 login/fails_to_use ...
- OC-多线程安全隐患及一般解决办法
1.多线程的安全隐患1.1>一块资源可能被多个线程共享,也就是多个线程可能会访问同一块资源,如多个线程访问同一个对象,变量,文件等当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题1. ...
- Windows NT 技术简介
Windows NT 技术简介 NT:New Technoly(新技术,因比DOS.WIN9X采用了很多新技术而得名) Windows NT基本介绍 WindowsNT是Microsoft推出的面向工 ...