[英]Confuse about Bitmap convert to ByteArray in different way
我已經搜索了3種將位圖對象轉換為ByteArray的方法,但是結果返回了3種大小的ByteArray。 它們之間有什么區別?
我從此處測試的尺寸為300x300的圖像。
這是我的測試代碼和結果。
val bitmapTarget = Glide
.with(_context)
.asBitmap()
.load("https://www.unitag.io/images/generator/templates/classic.png")
.submit()
val bitmap = bitmapTarget.get()
val byteBuffer = ByteBuffer.allocate(bitmap.byteCount)
bitmap.copyPixelsToBuffer(byteBuffer)
val byteFromBuffer = byteBuffer.array() // 360000 bytes
val byteOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOutputStream)
val byteJpeg = byteOutputStream.toByteArray() // 25408 bytes
val byteOutputStream2 = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteOutputStream2)
val bytePng = byteOutputStream2.toByteArray() // 4238 bytes
感謝大伙們。
試試看
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bmp.recycle();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.