Skip to content

Instantly share code, notes, and snippets.

@sergiandreplace
Last active March 18, 2020 09:27
Show Gist options
  • Save sergiandreplace/159c22148dc78b1ae337efe175a1de27 to your computer and use it in GitHub Desktop.
Save sergiandreplace/159c22148dc78b1ae337efe175a1de27 to your computer and use it in GitHub Desktop.
CnDetectorProcessor
class CnDetectorProcessor(private val context: Context, private val onCnDetected: ((cn: String, checksum: String) -> Unit)) :
Detector.Processor<TextBlock?> {
private val pattern = Pattern.compile("^C?\\.?N?\\.? ?(\\d\\d\\d\\d\\d\\d)\\.(\\d) ?[0O]?\$")
private var detected = false
override fun release() {}
override fun receiveDetections(detections: Detections<TextBlock?>) {
detections.detectedItems.forEach { _, item ->
if (detected) return
item?.value?.let(::checkCn)
}
}
private fun checkCn(rawCn: String) {
val matcher = pattern.matcher(rawCn)
if (matcher.matches()) {
val cn = matcher.group(1)!!
val checksum = matcher.group(2)!!
onCnDetected.invoke(cn, checksum)
detected = true
}
}
fun restart() {
detected = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment