在 Android 中将位图压缩为特定的字节大小 [英] Compress bitmap to a specific byte size in Android
问题描述
有没有办法将 Bitmap 压缩到特定的字节大小?例如,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.
推荐答案
您可以通过以下方式轻松计算位图的大小宽*高*每像素字节数=大小
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屋!