Skip to content

Instantly share code, notes, and snippets.

@pengj
Created August 11, 2021 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pengj/10d8d4015e4f947ebef25c589d8ed9f0 to your computer and use it in GitHub Desktop.
Save pengj/10d8d4015e4f947ebef25c589d8ed9f0 to your computer and use it in GitHub Desktop.
class TextAnalyzer(private val onTextDetected: (MLText) -> Unit) : ImageAnalysis.Analyzer {
private val setting = MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_TRACKING_MODE)
.setLanguage("en")
.create()
private val analyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer(setting)
@SuppressLint("UnsafeOptInUsageError")
override fun analyze(imageProxy: ImageProxy) {
imageProxy.image?.let { image ->
analyzer.asyncAnalyseFrame(MLFrame.fromMediaImage(image, imageProxy.imageInfo.rotationDegrees))
.addOnSuccessListener { mlText ->
mlText?.let {
onTextDetected.invoke(it)
}
imageProxy.close()
}.addOnFailureListener {
imageProxy.close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment