Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.