当前位置: 首页 > news >正文

Android Bitmap压缩

Android View截屏长图拼接(RecyclerView)

我们在实际使用中,往往图片转化成Bitmap,对Bitmap操作的时候(如:截屏分享等),可能Bitmap会过大,导致无视实现对应功能。那么我们就需要对Bitmap进行压缩处理。

 public static Bitmap compressBitmap(Bitmap bitmap, int maxSize) {Bitmap newBitmap;ByteArrayOutputStream baos = new ByteArrayOutputStream();// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());BitmapFactory.Options newOpts = new BitmapFactory.Options();newOpts.inJustDecodeBounds = false;//为true的时候表示只读边,不读内容newOpts.inPurgeable = true;// 同时设置才会有效newOpts.inInputShareable = true;//当系统内存不够时候图片自动被回收// Do not compressnewOpts.inPreferredConfig = Bitmap.Config.RGB_565;//如果使用ARGB-8888连续压缩两个10M图分分钟oom,而使用4444比565还糊。。long length = baos.toByteArray().length;if (length / 1024 > 4 * 1024) {int a = 2;while (length / 1024 / a > 4 * 1024) {a = a + 1;}newOpts.inSampleSize = a;} else {newOpts.inSampleSize = 1;}newBitmap = BitmapFactory.decodeStream(inputStream, new Rect(), newOpts);if (maxSize > 100) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();int options = 100;newBitmap.compress(Bitmap.CompressFormat.PNG, options, byteArrayOutputStream);if (byteArrayOutputStream.toByteArray().length / 1024 > maxSize) {while (byteArrayOutputStream.toByteArray().length / 1024 > maxSize) {// Clean up osbyteArrayOutputStream.reset();// interval 10options -= 5;newBitmap.compress(Bitmap.CompressFormat.PNG, options, byteArrayOutputStream);}byte[] bytes = byteArrayOutputStream.toByteArray();newBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);}FileFileUtils.closeSilently(byteArrayOutputStream);}FileFileUtils.closeSilently(baos);FileFileUtils.closeSilently(inputStream);return newBitmap;}public static void closeSilently(Closeable c) {if (c == null) return;try {c.close();} catch (Throwable t) {// Do nothing}}

http://www.lryc.cn/news/141925.html

相关文章:

  • 不同子网络中的通信过程
  • Ubuntu Touch OTA-2 推出,支持 Fairphone 3 和 F(x)tec Pro1 X
  • 【网络】数据链路层——MAC帧协议 | ARP协议
  • 【Spring Boot】Spring Boot自动加载机制:简化应用程序的启动
  • centos7搭建apache作为文件站后,其他人无法访问解决办法
  • 【开个空调】语音识别+红外发射
  • 【hibernate validator】(二)声明和验证Bean约束
  • Redis持久化机制之RDB,AOF与混合AOF
  • 为啥外卖小哥宁愿600一月租电动车,也不花2、3千买一辆送外卖!背后的原因......
  • 分布式定时任务框架Quartz总结和实践(2)—持久化到Mysql数据库
  • Linux 服务器搭建配置,开发效率一飞冲天 - Centos 篇
  • Day46|leetcode 139.单词拆分
  • 深入理解高并发编程 - Thread 类的 stop () 和 interrupt ()
  • C语言之三子棋游戏实现篇
  • jupyter notebook 插件nbextensions的安装
  • Spring boot 集成单元测试
  • 基于C++的QT实现贪吃蛇小游戏
  • Spring Boot整合RabbitMQ之路由模式(Direct)
  • 行式存储与列式存储
  • windows上sqlserver的ldf日志文件和数据mdf文件分别放到不同的磁盘
  • vue3+uni——watch监听props中的数据(组件参数接收与传递defineProps、defineEmits)
  • mybatis与spring集成与spring aop集成pagehelper插件
  • Mybatis基础
  • TypeScript-- 配置Typescript环境(1)ts 转js,tsc --watch 实时编译
  • Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库
  • Lottery抽奖项目学习第二章第一节:环境、配置、规范
  • OpenCV之reshape函数
  • 【JavaEE】Spring事务-@Transactional参数介绍-事务的隔离级别以及传播机制
  • 微信小程序canvas type=2d生成海报保存到相册、文字换行溢出显示...、文字删除线、分享面板
  • C++卷积神经网络