Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theeasiestway/1572e99a7f160c32ce04a9d21d177e9b to your computer and use it in GitHub Desktop.
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
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