DEV Community

vmodal_ai
vmodal_ai

Posted on

Extract Text from Images Using Google ML Kit Text Recognition in Android

Extract Text from Images Using Google ML Kit Text Recognition in Android

Google ML Kit provides powerful machine learning APIs that run directly on the device. One of its most popular features is Text Recognition, which allows Android applications to extract text from images with minimal setup.

In this tutorial, you'll learn how to integrate Google ML Kit Text Recognition into your Android application using Kotlin.

What is ML Kit Text Recognition?

ML Kit Text Recognition uses on-device machine learning models to identify and extract text from images.

Common use cases include:

  • Scanning documents
  • Reading receipts and invoices
  • Extracting text from business cards
  • Digitizing printed content
  • Building OCR-based applications

Step 1: Add ML Kit Dependencies

Add the following dependency to your app-level build.gradle file:

dependencies {
    implementation("com.google.mlkit:text-recognition:16.0.1")
}
Enter fullscreen mode Exit fullscreen mode

Sync the project after adding the dependency.

Step 2: Select an Image

You can obtain an image from:

  • Camera capture
  • Gallery selection
  • Downloaded image files

Create an InputImage instance:

val image = InputImage.fromBitmap(bitmap, 0)
Enter fullscreen mode Exit fullscreen mode

The second parameter represents the image rotation.

Step 3: Initialize the Text Recognizer

Create a recognizer instance:

val recognizer = TextRecognition.getClient(
    TextRecognizerOptions.DEFAULT_OPTIONS
)
Enter fullscreen mode Exit fullscreen mode

This recognizer will process the image and return detected text.

Step 4: Process the Image

Call the process() method:

recognizer.process(image)
    .addOnSuccessListener { visionText ->

        val extractedText = visionText.text

        Log.d("MLKit", extractedText)
    }
    .addOnFailureListener { exception ->

        Log.e("MLKit", exception.message ?: "Error")
    }
Enter fullscreen mode Exit fullscreen mode

When processing succeeds, the recognized text becomes available through visionText.text.

Step 5: Display the Extracted Text

You can show the result inside a TextView:

recognizer.process(image)
    .addOnSuccessListener { visionText ->
        binding.tvResult.text = visionText.text
    }
Enter fullscreen mode Exit fullscreen mode

The detected text will immediately appear in the UI.

Understanding the Result Structure

ML Kit organizes recognized text into several levels:

  • Text
  • Text Blocks
  • Lines
  • Elements (individual words)

Example:

for (block in visionText.textBlocks) {
    for (line in block.lines) {
        Log.d("MLKit", line.text)
    }
}
Enter fullscreen mode Exit fullscreen mode

This allows you to process specific sections instead of using the entire text output.

Why Use ML Kit Text Recognition?

ML Kit offers several advantages:

  • Fast on-device processing
  • Works offline
  • Easy integration
  • High accuracy for printed text
  • No custom machine learning expertise required

Best Practices

  • Use high-quality images for better accuracy.
  • Resize extremely large images before processing.
  • Handle image rotation correctly.
  • Process images off the main UI thread when needed.
  • Release resources when recognition is complete.

Conclusion

Google ML Kit Text Recognition makes it easy to add OCR functionality to Android applications with just a few lines of code. Whether you're building a document scanner, receipt reader, or text extraction tool, ML Kit provides a fast and reliable solution that works directly on the device.

Thanks for reading! If you found this tutorial helpful, consider following me for more Android, Kotlin, and Mobile Development articles.

Top comments (0)