Android Machine Learning Image (Bitmap) Text Recognize

Nicos Nicolaou
2 min readAug 14, 2024

--

For this article we will see and explain an easy implementation for the text recognition (“read a text”) from an image. There are four types of Input Image and are.

  • media.Image.
  • URI.
  • ByteBuffer or ByteArray.
  • Bitmap.

You can find an example for all the types of Input Image from the following link. For our example we will use Bitmap without using Google Play Services. To achieve this we need only three steps.

First of all, we need to add the following dependency into the application Gradle file.

Groovy:

implementation 'com.google.mlkit:text-recognition:16.0.1'

Kotlin:

implementation("com.google.mlkit:text-recognition:16.0.1")

Second, initializing an Image Picker (we are using the Native way, you can use a library) to get an image from the Android device gallery.

val galleryLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
if (uri != null) {
bitmap.value = convertUriToBitmap(
contentResolver = context.contentResolver,
uri = uri
) ?: Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
}
}

Launch the Image Picker.

galleryLauncher.launch("image/*")

Convert the URI to bitmap.

private fun convertUriToBitmap(
contentResolver: ContentResolver,
uri: Uri?
): Bitmap? {
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
MediaStore.Images.Media.getBitmap(contentResolver, uri)
} else {
val source: ImageDecoder.Source? =
uri?.let { ImageDecoder.createSource(contentResolver, it) }
source?.let { ImageDecoder.decodeBitmap(it) }
}
}

Third and last step, set the bitmap into Input Image and initialize the TextRecognition.getClient(…). Initializing the process method with Input Image and get the text from the success listener, addOnSuccessListener as a String type.

private fun handleTextRecognition(
bitmap: Bitmap,
result: (String) -> Unit
) {
val inputImage = InputImage.fromBitmap(bitmap, 0)
val recognizer =
TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
recognizer.process(inputImage)
.addOnSuccessListener { visionText ->
result(visionText.text)
}
.addOnFailureListener { e ->
result(e.message ?: getString(R.string.galleryImage))
}
}

Check my sample project in my GitHub account to the follow link for more information:

I hope you find the article and sample project useful. Let me know your opinion under the comments, feel free to report any issue. I will appreciate it if you let a clap, so, if I have your support to continue writing.

References:

--

--

Nicos Nicolaou

Senior Software Engineer, Android and Flutter Developer, Google Play Developer, Building libraries, Writing articles. 👇🔗 https://github.com/NicosNicolaou16