Skip to content

Instantly share code, notes, and snippets.

@iaverypadberg
Last active April 21, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaverypadberg/9329a4012b0d03a865149f63120a609c to your computer and use it in GitHub Desktop.
Save iaverypadberg/9329a4012b0d03a865149f63120a609c to your computer and use it in GitHub Desktop.
ImageProxy to Bitmap
/*
A function to change an ImageProxy to a bitmap
@param the image input
@return can be null or a bitmap
*/
fun preProcessImg(image : ImageProxy):Bitmap?{
val planes = image!!.planes
val yBuffer = planes[0].buffer
val uBuffer = planes[1].buffer
val vBuffer = planes[2].buffer
val ySize = yBuffer.remaining()
val uSize = uBuffer.remaining()
val vSize = vBuffer.remaining()
val nv21 = ByteArray(ySize + uSize + vSize)
yBuffer[nv21, 0, ySize]
vBuffer[nv21, ySize, vSize]
uBuffer[nv21, ySize + vSize, uSize]
val yuvImage = YuvImage(nv21, ImageFormat.NV21, image.width, image.height, null)
val out = ByteArrayOutputStream()
yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 75, out)
val imageBytes = out.toByteArray()
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment