Created
March 26, 2020 01:42
-
-
Save theeasiestway/1572e99a7f160c32ce04a9d21d177e9b to your computer and use it in GitHub Desktop.
Convert android camera image planes to a byte array for encoding or sending
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var yData = ByteArray(1) | |
var uData = ByteArray(1) | |
var vData = ByteArray(1) | |
for (i in 0 until 3) { | |
val plane = image.planes[i] | |
val bytes = ByteArray(plane.buffer.remaining()) | |
plane.buffer.get(bytes) | |
when(i) { | |
0 -> yData = bytes | |
1 -> uData = bytes | |
2 -> vData = bytes | |
} | |
} | |
val ySize = yData.size | |
val uSize = uData.size | |
val vSize = vData.size | |
val size = ySize + uSize + vSize | |
val resultData = ByteArray(size) | |
System.arraycopy(yData, 0, resultData, 0, ySize) | |
System.arraycopy(uData, 0, resultData, ySize, uSize) | |
System.arraycopy(vData, 0, resultData, ySize + uSize, vSize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment