Skip to content

Instantly share code, notes, and snippets.

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 {
val data = ByteArray(remaining())
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 = { it.toInt() and 0x9370DB }
val averagePurplePixels = pixels.average()
Log.e("PURPLE", "Average purple pixels: $averagePurplePixels")
lastAnalyzedTimestamp = currentTimestamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment