diff --git a/WooCommerce/build.gradle b/WooCommerce/build.gradle index b022c27933f..d4d2b2627f0 100644 --- a/WooCommerce/build.gradle +++ b/WooCommerce/build.gradle @@ -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" diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/ai/TextRecognitionEngine.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/ai/TextRecognitionEngine.kt index 31acebd22fe..3da346e322d 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/ai/TextRecognitionEngine.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/products/ai/TextRecognitionEngine.kt @@ -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> { 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}")