Android图片压缩几种方式总结
发布时间 - 2026-01-11 01:55:12 点击率:次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
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何发送系统通知?(Notification渠道示例)
大同网页,大同瑞慈医院官网?
微博html5版本怎么弄发语音微博_语音录制入口及时长限制操作【教程】
如何解决hover在ie6中的兼容性问题
Laravel中的Facade(门面)到底是什么原理
深入理解Android中的xmlns:tools属性
微信小程序 scroll-view组件实现列表页实例代码
Laravel如何实现图片防盗链功能_Laravel中间件验证Referer来源请求【方案】
1688铺货到淘宝怎么操作 1688一键铺货到自己店铺详细步骤
如何获取PHP WAP自助建站系统源码?
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
jquery插件bootstrapValidator表单验证详解
如何快速搭建高效WAP手机网站吸引移动用户?
Laravel怎么在Controller之外的地方验证数据
零基础网站服务器架设实战:轻量应用与域名解析配置指南
Laravel PHP版本要求一览_Laravel各版本环境要求对照
如何实现建站之星域名转发设置?
Laravel如何升级到最新版本?(升级指南和步骤)
Laravel怎么发送邮件_Laravel Mail类SMTP配置教程
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
Laravel与Inertia.js怎么结合_使用Laravel和Inertia构建现代单页应用
如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】
Laravel怎么在Blade中安全地输出原始HTML内容
如何用wdcp快速搭建高效网站?
Laravel如何安装使用Debugbar工具栏_Laravel性能调试与SQL监控插件【步骤】
宙斯浏览器视频悬浮窗怎么开启 边看视频边操作其他应用教程
EditPlus 正则表达式 实战(3)
Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区
使用Dockerfile构建java web环境
HTML5空格和margin有啥区别_空格与外边距的使用场景【说明】
Python结构化数据采集_字段抽取解析【教程】
高性能网站服务器部署指南:稳定运行与安全配置优化方案
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
如何用好域名打造高点击率的自主建站?
如何在IIS服务器上快速部署高效网站?
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能
如何在HTML表单中获取用户输入并结合JavaScript动态控制复利计算循环
如何快速生成高效建站系统源代码?
免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?
iOS发送验证码倒计时应用
Laravel Session怎么存储_Laravel Session驱动配置详解
JS经典正则表达式笔试题汇总
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)
香港服务器网站推广:SEO优化与外贸独立站搭建策略
浅谈javascript alert和confirm的美化
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
JS碰撞运动实现方法详解
高防服务器租用指南:配置选择与快速部署攻略

