Created
September 13, 2020 10:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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