在Android中将位图压缩为特定的字节大小 [英] Compress bitmap to a specific byte size in Android

查看:97
本文介绍了在Android中将位图压缩为特定的字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将位图压缩为特定的字节大小?例如1.5MB.问题是到目前为止,我所看到的所有示例都是调整宽度和高度的大小,但是我的要求是调整字节的大小.那可能吗? 此外,压缩位图的最直接,最正确的方法是什么?我对这个主题不是很新手,并且希望从一开始就朝正确的方向前进.

Is there a way to compress Bitmap to a specific byte size? For example, 1.5MB. The matter is all the examples I have seen so far were resizing width and height, but my requirement is to resize the bytes. Is that possible? Also, what is the most straightforward and right way to compress the Bitmap? I am quite novice to this topic and would like to go right direction from the beginning.

推荐答案

您可以通过以下方法很容易地计算位图的大小: width * height * bytes per pixel = size

You can calculate the size of a bitmap quite easily by width * height * bytes per pixel = size

其中bytes per pixel由您的颜色模型定义,其中RGBA_F16是8个字节,而ARGB_8888是4个字节,依此类推.有了这个,您应该能够弄清楚您想要图像的宽度,高度和颜色编码.

Where bytes per pixel is defined by your color model say RGBA_F16 is 8 bytes while ARGB_8888 is 4 bytes and so on. With this you should be able to figure out what width and height and color encoding you want for your image.

请参见 https://developer.android.com/reference/android/graphics/Bitmap.Config 获取位值.

另请参阅 https://developer.android.com/topic/performance /graphics/manage-memory 了解有关位图内存管理的更多信息.

Also see https://developer.android.com/topic/performance/graphics/manage-memory for more about bitmap memory management.

这篇关于在Android中将位图压缩为特定的字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆