Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yektasarioglu/093b8981643e60b062e9ec019d9d631f to your computer and use it in GitHub Desktop.
Save yektasarioglu/093b8981643e60b062e9ec019d9d631f to your computer and use it in GitHub Desktop.
// TextRecognitionViewModel
fun analyzeBitmap(bitmap: Bitmap) {
textRecognizer.analyzeBitmap(bitmap) {
Timber.d("stringValue is ${it.stringValue}")
if (it.stringValue.isEmpty()) {
outputLiveData.value = "No text recognized !!"
Timber.e("No text recognized !!")
}
it.blocks.forEach {
Timber.d("analyzeBitmap: ${it.stringValue}")
outputLiveData.value = it.stringValue
}
}
}
// TextRecognizer
fun analyzeBitmap(bitmap: Bitmap, onSuccess: (MLText) -> Unit) {
Timber.d("analyzeBitmap()")
val task: Task<MLText> = analyzer?.asyncAnalyseFrame(MLFrame.fromBitmap(bitmap))!!
task.addOnSuccessListener {
// Recognition success.
Timber.d("Success - Result is ${it.stringValue}")
onSuccess(it)
}.addOnFailureListener { e ->
// If the recognition fails, obtain related exception information.
try {
val mlException = e as MLException
// Obtain the result code. You can process the result code and customize respective messages displayed to users.
val errorCode = mlException.errCode
// Obtain the error information. You can quickly locate the fault based on the result code.
val errorMessage = mlException.message
Timber.d("Failure - errorCode: $errorCode errorMessage is $errorMessage")
} catch (error: Exception) {
Timber.e("Failure - Exception is $error")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment