Skip to content

Commit

Permalink
Merge pull request #12244 from woocommerce/issue/12242-fix-text-recog…
Browse files Browse the repository at this point in the history
…nition

Adds text recognition for Japanese, Korean, and Chinese
  • Loading branch information
irfano authored Aug 7, 2024
2 parents 325005d + 4dbcb6e commit 0531462
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions WooCommerce/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ dependencies {
implementation 'com.google.android.gms:play-services-code-scanner:16.1.0'

implementation "com.google.mlkit:text-recognition:$mlkitTextRecognitionVersion"
implementation "com.google.android.gms:play-services-mlkit-text-recognition-japanese:$mlkitTextRecognitionVersion"
implementation "com.google.android.gms:play-services-mlkit-text-recognition-chinese:$mlkitTextRecognitionVersion"
implementation "com.google.android.gms:play-services-mlkit-text-recognition-korean:$mlkitTextRecognitionVersion"

implementation "com.google.mlkit:barcode-scanning:$mlkitBarcodeScanningVersion"

implementation "com.google.zxing:core:3.5.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,37 @@ import coil.request.ImageRequest.Builder
import coil.request.SuccessResult
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions
import com.google.mlkit.vision.text.japanese.JapaneseTextRecognizerOptions
import com.google.mlkit.vision.text.korean.KoreanTextRecognizerOptions
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
import com.woocommerce.android.util.WooLog
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.tasks.await
import java.util.Locale
import javax.inject.Inject

class TextRecognitionEngine @Inject constructor(
@ApplicationContext private val appContext: Context
) {
private val textRecognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
private companion object {
const val JAPANESE_LANGUAGE_CODE = "ja"
const val CHINESE_LANGUAGE_CODE = "zh"
const val KOREAN_LANGUAGE_CODE = "ko"
}

@Suppress("TooGenericExceptionCaught")
suspend fun processImage(imageUrl: String): Result<List<String>> {
return try {
val bitmap = loadBitmap(imageUrl)
val image = InputImage.fromBitmap(requireNotNull(bitmap), 0)

val result = textRecognizer.process(image).await()
val deviceLocale = Locale.getDefault().language
val result = when (deviceLocale) {
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) {
WooLog.d(WooLog.T.AI, "Failed to scan text from image: ${e.message}")
Expand Down

0 comments on commit 0531462

Please sign in to comment.