Last active
March 18, 2020 09:27
-
-
Save sergiandreplace/159c22148dc78b1ae337efe175a1de27 to your computer and use it in GitHub Desktop.
CnDetectorProcessor
This file contains hidden or 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 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