Android图片压缩几种方式总结

图片压缩在Android开发中很常见也很重要,防止图片的OOM也是压缩的重要原因。
首先看下Bitmap图片文件的大小的决定因素:
Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。3个参数,任意减少一个的值,就达到了压缩的效果。
接下来看下Bitmap图片的几种格式的特点:
ALPHA_8
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度
ARGB_4444
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节
ARGB_8888
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
RGB_565
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
如果进行图片格式的压缩的话,一般情况下都是ARGB_8888转为RGB565进行压缩。
写了一个工具类,基本上列举了android上图片的几种基本压缩方式:
1.质量压缩
2.采样率压缩
3.尺寸压缩
4.Matrix压缩
5.图片格式的压缩,例如PNG和JPG保存后的图片大小是不同的
public class Utils {
/**
* 采样率压缩
*
* @param bitmap
* @param sampleSize 采样率为2的整数倍,非整数倍四舍五入,如4的话,就是原图的1/4
* @return 尺寸变化
*/
public static Bitmap getBitmap(Bitmap bitmap, int sampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
Log.i("info", "图片大小:" + bit.getByteCount());//2665296 10661184
return bit;
}
/**
* 图片质量压缩
*
* @param bitmap
* @param quality
* @return 尺寸不变,质量变小
*/
public static Bitmap compressByQuality(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "图片大小:" + bit.getByteCount());//10661184
return bit;
}
/**
* 图片质量压缩
*
* @param src
* @param maxByteSize
* @return
*/
public static Bitmap compressByQuality(Bitmap src, long maxByteSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
src.compress(Bitmap.CompressFormat.JPEG, quality, baos);
while (baos.toByteArray().length > maxByteSize && quality > 0) {
baos.reset();
src.compress(Bitmap.CompressFormat.JPEG, quality -= 5, baos);
}
if (quality < 0) return null;
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bit;
}
public static Bitmap compressByFormat(Bitmap bitmap, int format) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "图片大小:" + bit.getByteCount());//10661184
return bit;
}
/**
* Matrix缩放
*
* @param bitmap
* @param scaleWidth
* @param scaleHeight
* @return 尺寸和大小变化
*/
public static Bitmap getBitmapBySize(Bitmap bitmap, float scaleWidth, float scaleHeight) {
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
Log.i("info", "图片大小:" + bit.getByteCount());
return bit;
}
/**
* 按照图片格式配置压缩
*
* @param path
* @param config ALPHA_8,ARGB_4444,ARGB_8888,RGB_565;
* @return RGB_565比ARGB_8888节省一半内存
*/
public static Bitmap getBitmapByFormatConfig(String path, Bitmap.Config config) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = config;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
Log.i("info", "图片大小:" + bitmap.getByteCount());
return bitmap;
}
/**
* 指定大小缩放
*
* @param bitmap
* @param width
* @param height
* @return
*/
public static Bitmap getBitmapByScaleSize(Bitmap bitmap, int width, int height) {
Bitmap bit = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("info", "图片大小:" + bit.getByteCount());
return bit;
}
/**
* 通过保存格式压缩
*
* @param bitmap
* @param format JPEG,PNG,WEBP
* @return
*/
public static Bitmap getBitmapByFormat(Bitmap bitmap, Bitmap.CompressFormat format) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(format, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "图片大小:" + bit.getByteCount());
return bit;
}
/**
* 文件加载压缩
*
* @param filePath
* @param inSampleSize
* @return
*/
public static Bitmap getBitmap(String filePath, int inSampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);//此时不耗费和占用内存
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static Bitmap getBitmap(String filePath) {
return BitmapFactory.decodeFile(filePath);
}
public static Bitmap view2Bitmap(View view) {
if (view == null) return null;
Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ret);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return ret;
}
public static void saveBitmap(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveBitmap(Bitmap bitmap,Bitmap.CompressFormat format) {
File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(format, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android图片压缩几种方式
# Android图片压缩
# Android图片压缩方法并压缩到指定大小
# Android实现图片压缩(bitmap的六种压缩方式)
# Android开发之图片压缩实现方法分析
# Android开发实现图片大小与质量压缩及保存
# 几种
# 都是
# 采样率
# 整数倍
# 希望能
# 写了
# 谢谢大家
# 率为
# 举了
# 就达
# 也很重要
# 中很
# 加载
# 四舍五入
# param
# Options
相关文章:
零服务器AI建站解决方案:快速部署与云端平台低成本实践
香港服务器租用费用高吗?如何避免常见误区?
建站之星如何快速更换网站模板?
公司门户网站制作流程,华为官网怎么做?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
建站之星安装后如何自定义网站颜色与字体?
如何选购建站域名与空间?自助平台全解析
相册网站制作软件,图片上的网址怎么复制?
历史网站制作软件,华为如何找回被删除的网站?
建站之星后台搭建步骤解析:模板选择与产品管理实操指南
南京网站制作费用,南京远驱官方网站?
网站制作壁纸教程视频,电脑壁纸网站?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
高防服务器:AI智能防御DDoS攻击与数据安全保障
如何注册花生壳免费域名并搭建个人网站?
如何在IIS7上新建站点并设置安全权限?
香港服务器选型指南:免备案配置与高效建站方案解析
建站之星Pro快速搭建教程:模板选择与功能配置指南
完全自定义免费建站平台:主题模板在线生成一站式服务
广东专业制作网站有哪些,广东省能源集团有限公司官网?
免费公司网站制作软件,如何申请免费主页空间做自己的网站?
如何选择香港主机高效搭建外贸独立站?
详解jQuery停止动画——stop()方法的使用
香港服务器部署网站为何提示未备案?
定制建站是什么?如何实现个性化需求?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
如何通过宝塔面板实现本地网站访问?
如何在Tomcat中配置并部署网站项目?
如何在服务器上三步完成建站并提升流量?
如何在企业微信快速生成手机电脑官网?
香港服务器如何优化才能显著提升网站加载速度?
如何确保FTP站点访问权限与数据传输安全?
如何快速生成可下载的建站源码工具?
如何在建站宝盒中设置产品搜索功能?
如何快速搭建支持数据库操作的智能建站平台?
制作网站的模板软件,网站怎么建设?
建站之星安装提示数据库无法连接如何解决?
如何获取免费开源的自助建站系统源码?
网站插件制作软件免费下载,网页视频怎么下到本地插件?
公司网站制作价格怎么算,公司办个官网需要多少钱?
如何在阿里云部署织梦网站?
官网网站制作腾讯审核要多久,联想路由器newifi官网
广州营销型建站服务商推荐:技术优势与SEO优化解析
如何通过万网虚拟主机快速搭建网站?
建站之星CMS五站合一模板配置与SEO优化指南
如何快速使用云服务器搭建个人网站?
如何在服务器上配置二级域名建站?
Android使用GridView实现日历的简单功能
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
IOS倒计时设置UIButton标题title的抖动问题
*请认真填写需求信息,我们会在24小时内与您取得联系。