Skip to content

Instantly share code, notes, and snippets.

@pedrol2b
Created May 31, 2024 14:37
Show Gist options
  • Save pedrol2b/d1a8b713b096a9b4de2d89fd19102d75 to your computer and use it in GitHub Desktop.
Save pedrol2b/d1a8b713b096a9b4de2d89fd19102d75 to your computer and use it in GitHub Desktop.
Calculate avarage luminance from an YUV Frame
fun getAverageLuminance(frame: Frame): Double {
require(frame.pixelFormat == PixelFormat.YUV) { "Unsupported pixel format. Expected YUV." }
var totalLuminance = 0.0
val yBuffer = frame.image.planes[0].buffer
while (yBuffer.hasRemaining()) {
@Suppress("MagicNumber")
totalLuminance += yBuffer.get().toInt() and 0xFF
}
return totalLuminance / (frame.width * frame.height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment