Skip to content

Commit

Permalink
Instantiate the text recognition clients exactly when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeMucientes committed Aug 7, 2024
1 parent c808dcb commit 4dbcb6e
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ class TextRecognitionEngine @Inject constructor(
const val KOREAN_LANGUAGE_CODE = "ko"
}

private val generalTextRecognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
private val japaneseTextRecognizer = TextRecognition.getClient(JapaneseTextRecognizerOptions.Builder().build())
private val chineseTextRecognizer = TextRecognition.getClient(ChineseTextRecognizerOptions.Builder().build())
private val koreanTextRecognizer = TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())

@Suppress("TooGenericExceptionCaught")
suspend fun processImage(imageUrl: String): Result<List<String>> {
return try {
val bitmap = loadBitmap(imageUrl)
val image = InputImage.fromBitmap(requireNotNull(bitmap), 0)
val deviceLocale = Locale.getDefault().language
val result = when (deviceLocale) {
JAPANESE_LANGUAGE_CODE -> japaneseTextRecognizer
CHINESE_LANGUAGE_CODE -> chineseTextRecognizer
KOREAN_LANGUAGE_CODE -> koreanTextRecognizer
else -> generalTextRecognizer
JAPANESE_LANGUAGE_CODE -> TextRecognition.getClient(JapaneseTextRecognizerOptions.Builder().build())
CHINESE_LANGUAGE_CODE -> TextRecognition.getClient(ChineseTextRecognizerOptions.Builder().build())
KOREAN_LANGUAGE_CODE -> TextRecognition.getClient(KoreanTextRecognizerOptions.Builder().build())
else -> TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
}.process(image).await()
Result.success(result.textBlocks.map { it.text })
} catch (e: Exception) {
Expand Down

0 comments on commit 4dbcb6e

Please sign in to comment.