Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Created September 13, 2020 10:45
Show Gist options
  • Save magdamiu/037d5ac9bcd9c45c0b3339003eeb9d4e to your computer and use it in GitHub Desktop.
Save magdamiu/037d5ac9bcd9c45c0b3339003eeb9d4e to your computer and use it in GitHub Desktop.
Smile, it’s CameraX! [analysis and extensions] | Analysis - Step 2: create a custom analyser
class PurpleColorAnalyser : ImageAnalysis.Analyzer {
private var lastAnalyzedTimestamp = 0L
private fun ByteBuffer.toByteArray(): ByteArray {
rewind()
val data = ByteArray(remaining())
get(data)
return data
}
override fun analyze(image: ImageProxy) {
val currentTimestamp = System.currentTimeMillis()
val oneSecond = TimeUnit.SECONDS.toMillis(1)
if (currentTimestamp - lastAnalyzedTimestamp >= oneSecond) {
val buffer = image.planes[0].buffer
val data = buffer.toByteArray()
val pixels = data.map { it.toInt() and 0x9370DB }
val averagePurplePixels = pixels.average()
Log.e("PURPLE", "Average purple pixels: $averagePurplePixels")
lastAnalyzedTimestamp = currentTimestamp
}
image.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment