You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bitmap转ByteArray三种方式生成不同大小字节数组的差异探究

Bitmap转ByteArray的三种方式为何生成的字节数组大小差异悬殊?

我最近尝试用三种不同方法将Bitmap对象转换为ByteArray,却发现最终得到的字节数组大小相差非常大,想请教各位大佬这几种方式之间的本质差异是什么。

我测试所用的图片尺寸为300x300,下面是我的测试代码和对应的结果:

val bitmapTarget = Glide
    .with(_context)
    .asBitmap()
    .load("https://www.unitag.io/images/generator/templates/classic.png")
    .submit()
val bitmap = bitmapTarget.get()

// 方法1:通过ByteBuffer复制像素
val byteBuffer = ByteBuffer.allocate(bitmap.byteCount)
bitmap.copyPixelsToBuffer(byteBuffer)
val byteFromBuffer = byteBuffer.array() // 结果:360000 bytes

// 方法2:以JPEG格式压缩
val byteOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOutputStream)
val byteJpeg = byteOutputStream.toByteArray() // 结果:25408 bytes

// 方法3:以PNG格式压缩
val byteOutputStream2 = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteOutputStream2)
val bytePng = byteOutputStream2.toByteArray() // 结果:4238 bytes

可以看到三种方式的结果大小差了好几个数量级,希望有人能帮我解释每种方式的原理,以及为什么会有这么大的差异,谢谢各位!

内容的提问来源于stack exchange,提问作者Thong M Truong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:33:39