Android ML Kit Camera OCR with Kotlin & Jetpack Compose: Build a Live Text Scanner & Translator

Imagine pointing your Android phone at a printed document, restaurant menu, product label, or signboard and instantly seeing the text appear on your screen.

Now imagine selecting that text, copying it, identifying its language, and translating it into English, Hindi, Spanish, or another supported language.

This experience is possible using Google’s on-device machine learning technologies.

In this tutorial, we’ll build a Live Text Scanner & Translator using CameraX, Google ML Kit Text Recognition, Language Identification, On-Device Translation, Kotlin, and Jetpack Compose.

Unlike a traditional document scanner, which captures a document and processes it afterward, our application continuously analyzes frames from the camera.

We’ll learn how to manage the camera lifecycle, process images efficiently, extract text, draw OCR overlays, and integrate translation.

We’ll also discuss offline functionality, permissions, performance, privacy, and production-ready architecture.


1. What Is a Live Camera OCR Scanner?

OCR stands for Optical Character Recognition.

A live camera OCR scanner analyzes images captured by a camera and converts visible text into machine-readable characters.

For example, the camera might capture:

Welcome to Hyderabad

Metro Station

Platform 2

ML Kit can recognize the text and return it to the application.

The app can then offer actions such as copying, searching, or translating the recognized content.

Common applications

  • Live text scanning.
  • Document reading.
  • Product label recognition.
  • Menu translation.
  • Travel sign translation.
  • Business card scanning.
  • Educational reading tools.
  • Accessibility utilities.

2. Technologies Used

Our application combines several Android technologies.

TechnologyPurpose
CameraXCamera preview and frame analysis
ML Kit Text RecognitionRecognize text in images
ML Kit Language IdentificationDetect the text’s language
ML Kit TranslationTranslate recognized text
KotlinApplication logic
Jetpack ComposeModern Android UI
Material 3UI components
CoroutinesAsynchronous operations
StateFlowReactive state
MVVMSeparation of concerns

Each technology handles a different part of the workflow.

3. Application Features

Our application will provide:

  • Real-time camera preview.
  • Live text recognition.
  • OCR text bounding boxes.
  • Pause and resume scanning.
  • Copy recognized text.
  • Detect source language.
  • Select translation language.
  • Download translation models.
  • Translate recognized text.
  • Display translated results.
  • Offline OCR.
  • Flashlight control.
  • Camera permission handling.
  • Error and loading states.

For the first version, we’ll use Latin-script OCR and English-to-Spanish translation as a working example.

4. Application Architecture

Android Camera
      |
      v
CameraX Preview + ImageAnalysis
      |
      v
Camera Frame
      |
      v
ML Kit Text Recognition
      |
      +----> Recognized Text
      |           |
      |           v
      |      Language Detection
      |           |
      |           v
      |      ML Kit Translation
      |           |
      |           v
      |      Translated Text
      |
      +----> OCR Bounding Boxes
                    |
                    v
              Compose Overlay
                    |
                    v
                User Actions

The camera pipeline should remain separate from the UI and translation layers.

This makes the application easier to maintain and test.

5. Project Requirements

Recommended configuration:

Project: LiveTextScanner

Package:
com.moiks.livetextscanner

Language: Kotlin

UI: Jetpack Compose

Architecture: MVVM

Minimum SDK: 23

CameraX and ML Kit support Android devices with different capabilities. Always test the combined integration on physical devices.

6. Add Dependencies

Open app/build.gradle.kts.

dependencies {

    // CameraX
    implementation(
        "androidx.camera:camera-core:<camerax-version>"
    )

    implementation(
        "androidx.camera:camera-camera2:<camerax-version>"
    )

    implementation(
        "androidx.camera:camera-lifecycle:<camerax-version>"
    )

    implementation(
        "androidx.camera:camera-view:<camerax-version>"
    )

    // ML Kit OCR - Latin script
    implementation(
        "com.google.mlkit:text-recognition:16.0.1"
    )

    // Language identification
    implementation(
        "com.google.mlkit:language-id:17.0.6"
    )

    // On-device translation
    implementation(
        "com.google.mlkit:translate:<current-version>"
    )

    // Google Tasks coroutine integration
    implementation(
        "org.jetbrains.kotlinx:" +
        "kotlinx-coroutines-play-services:<version>"
    )

    // Compose and Lifecycle
    implementation(
        "androidx.activity:activity-compose:<version>"
    )

    implementation(
        "androidx.lifecycle:" +
        "lifecycle-viewmodel-compose:<version>"
    )

    implementation(
        "androidx.lifecycle:" +
        "lifecycle-runtime-compose:<version>"
    )
}

Replace placeholders with compatible published versions from the official Android and ML Kit documentation.

The CameraX artifacts should use the same compatible release.

7. Configure Camera Permission

Add camera permission to AndroidManifest.xml.

<uses-permission
    android:name="android.permission.CAMERA" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />

Set required="true" if the application cannot function without a camera.

For a utility that also supports importing images, declaring the camera optional may be more appropriate.

Important distinction

Unlike the Google-provided Document Scanner from Article 90, a custom CameraX camera experience requires your application to request camera permission.

8. Request Runtime Camera Permission

Jetpack Compose supports runtime permissions through Activity Result APIs.

@Composable
fun CameraPermissionScreen(
    content: @Composable () -> Unit
) {
    val context = LocalContext.current

    var hasPermission by remember {
        mutableStateOf(
            ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.CAMERA
            ) == PackageManager.PERMISSION_GRANTED
        )
    }

    val launcher =
        rememberLauncherForActivityResult(
            ActivityResultContracts
                .RequestPermission()
        ) { granted ->

            hasPermission = granted
        }

    if (hasPermission) {

        content()

    } else {

        Button(
            onClick = {
                launcher.launch(
                    Manifest.permission.CAMERA
                )
            }
        ) {
            Text("Allow Camera Access")
        }
    }
}

For production, also explain why the permission is needed and handle permanently denied permission with a suitable settings option.

9. Understand CameraX Use Cases

CameraX provides several camera use cases.

Use casePurpose
PreviewDisplay camera output
ImageCaptureCapture photographs
ImageAnalysisProcess camera frames
VideoCaptureRecord video

For real-time OCR, we primarily need:

Preview + ImageAnalysis

ImageCapture can be added later for saving snapshots.

10. Create the Camera Preview

CameraX provides PreviewView, which can be embedded in Compose using AndroidView.

@Composable
fun CameraPreview(
    previewView: PreviewView
) {
    AndroidView(
        factory = {
            previewView
        },
        modifier = Modifier.fillMaxSize()
    )
}

Keep the PreviewView instance stable across recompositions.

11. Configure CameraX

Create the camera preview and image-analysis use cases.

val preview = Preview.Builder()
    .build()

val imageAnalysis =
    ImageAnalysis.Builder()
        .setBackpressureStrategy(
            ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
        )
        .build()

Why use KEEP_ONLY_LATEST?

OCR processing can take longer than the time between camera frames.

This strategy prevents an unbounded queue of outdated frames.

The analyzer receives the most recent available frame when it becomes ready.

12. Initialize ML Kit Text Recognition

Create the text recognizer.

val recognizer =
    TextRecognition.getClient(
        TextRecognizerOptions.DEFAULT_OPTIONS
    )

Required imports include:

import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.latin.TextRecognizerOptions

Reuse the recognizer rather than creating a new instance for every frame.

Close it when the camera processing component is permanently released.

13. Convert Camera Frames into ML Kit Images

CameraX provides an ImageProxy.

ML Kit accepts an InputImage.

val mediaImage = imageProxy.image

if (mediaImage != null) {

    val inputImage =
        InputImage.fromMediaImage(
            mediaImage,
            imageProxy.imageInfo.rotationDegrees
        )
}

The rotation value is essential for correct text recognition and overlay alignment.

14. Create the OCR Image Analyzer

Implement ImageAnalysis.Analyzer.

class TextAnalyzer(
    private val recognizer: TextRecognizer,
    private val onResult: (Text) -> Unit,
    private val onError: (Exception) -> Unit
) : ImageAnalysis.Analyzer {

    override fun analyze(
        imageProxy: ImageProxy
    ) {
        val mediaImage =
            imageProxy.image

        if (mediaImage == null) {
            imageProxy.close()
            return
        }

        val inputImage =
            InputImage.fromMediaImage(
                mediaImage,
                imageProxy.imageInfo.rotationDegrees
            )

        recognizer.process(inputImage)
            .addOnSuccessListener { result ->
                onResult(result)
            }
            .addOnFailureListener { error ->
                onError(error)
            }
            .addOnCompleteListener {
                imageProxy.close()
            }
    }
}

Critical rule

Every ImageProxy must be closed.

If an image is not closed, CameraX may stop delivering new frames.

In a production implementation, also guard against synchronous failures before a Task is returned, and ensure the proxy is closed on every error path.

15. Bind CameraX to the Lifecycle

A camera use case must be bound to a lifecycle owner.

val cameraProvider =
    ProcessCameraProvider
        .getInstance(context)
        .get()

val cameraSelector =
    CameraSelector.DEFAULT_BACK_CAMERA

cameraProvider.unbindAll()

cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    preview,
    imageAnalysis
)

Production recommendation

The .get() call shown above is a simplified illustration. Do not block the main thread waiting for the camera provider.

Use the provider’s asynchronous listener or coroutine integration, then bind use cases on the main thread.

Configure the preview surface:

preview.surfaceProvider =
    previewView.surfaceProvider

Unbind or release camera use cases when appropriate for the screen lifecycle.

16. Use a Dedicated Analysis Executor

Create an executor for image analysis.

val cameraExecutor =
    Executors.newSingleThreadExecutor()

Attach the analyzer:

imageAnalysis.setAnalyzer(
    cameraExecutor,
    textAnalyzer
)

Why a single-thread executor?

It provides a predictable analysis pipeline.

Combined with STRATEGY_KEEP_ONLY_LATEST, it helps control frame processing.

Shut down the executor when the camera component is disposed.

17. Extract Recognized Text

ML Kit returns a Text object.

val recognizedText =
    result.text

Example:

Welcome to Hyderabad

Metro Station

Platform 2

The result also contains structured information about recognized text blocks, lines, and elements.

18. Understand OCR Result Structure

Text Result
    |
    +-- TextBlock
           |
           +-- Line
                  |
                  +-- Element

Inspect text lines:

result.textBlocks.forEach { block ->

    block.lines.forEach { line ->

        val text = line.text

        val bounds = line.boundingBox
    }
}

Bounding boxes are useful for drawing text detection overlays.

19. Create an OCR Result Model

Keep ML Kit objects separate from presentation models where practical.

data class OcrLine(
    val text: String,
    val boundingBox: Rect?
)

data class OcrResult(
    val text: String,
    val lines: List<OcrLine>
)

Map the ML Kit result:

fun mapOcrResult(
    result: Text
): OcrResult {

    val lines =
        result.textBlocks.flatMap { block ->

            block.lines.map { line ->

                OcrLine(
                    text = line.text,
                    boundingBox = line.boundingBox
                )
            }
        }

    return OcrResult(
        text = result.text,
        lines = lines
    )
}

For overlay rendering, preserve image dimensions and orientation metadata alongside these boxes.

20. Display OCR Bounding Boxes

A modern text scanner can highlight recognized text directly over the camera preview.

Jetpack Compose provides Canvas.

@Composable
fun OcrOverlay(
    lines: List<OcrLine>,
    modifier: Modifier = Modifier
) {
    Canvas(modifier = modifier) {

        lines.forEach { line ->

            val bounds =
                line.boundingBox
                    ?: return@forEach

            drawRect(
                color = Color.Cyan,
                topLeft = Offset(
                    bounds.left.toFloat(),
                    bounds.top.toFloat()
                ),
                size = Size(
                    bounds.width().toFloat(),
                    bounds.height().toFloat()
                ),
                style = Stroke(
                    width = 3.dp.toPx()
                )
            )
        }
    }
}

Important

This is the basic drawing operation, not a complete camera overlay.

The OCR bounding boxes use image coordinates.

A production overlay must transform those coordinates into preview coordinates, accounting for image rotation, scaling, cropping, and possible mirroring.

Otherwise, rectangles may not align with the visible text.

21. Create the OCR ViewModel

Define a UI state.

data class TextScannerUiState(
    val recognizedText: String = "",
    val detectedLanguage: String = "",
    val translatedText: String = "",
    val isScanning: Boolean = true,
    val isTranslating: Boolean = false,
    val errorMessage: String? = null
)

Create the ViewModel:

class TextScannerViewModel : ViewModel() {

    private val _uiState =
        MutableStateFlow(
            TextScannerUiState()
        )

    val uiState =
        _uiState.asStateFlow()

    fun updateRecognizedText(
        text: String
    ) {
        _uiState.update {
            it.copy(
                recognizedText = text
            )
        }
    }

    fun pauseScanning() {
        _uiState.update {
            it.copy(isScanning = false)
        }
    }

    fun resumeScanning() {
        _uiState.update {
            it.copy(isScanning = true)
        }
    }
}

For production, connect this state to the analyzer through a dedicated controller and avoid updating UI state with every redundant frame result.

22. Implement Pause and Resume

Continuous recognition is useful while positioning the camera.

However, users need a stable result before copying or translating text.

Recommended workflow

Live Camera Preview
        |
        v
Recognize Text
        |
        v
User Taps Freeze
        |
        v
Stable OCR Result
        |
        +-- Copy
        |
        +-- Translate
        |
        +-- Share

Pause can be implemented by disabling or clearing the image analyzer while retaining the last recognized result.

A true frozen camera preview additionally requires preserving a captured frame or switching to a still-image preview.

Simply stopping analysis does not freeze the live Preview use case.

23. Copy Recognized Text to Clipboard

Use Android’s clipboard APIs.

fun copyText(
    context: Context,
    text: String
) {
    val clipboard =
        context.getSystemService(
            Context.CLIPBOARD_SERVICE
        ) as ClipboardManager

    val clip =
        ClipData.newPlainText(
            "Recognized Text",
            text
        )

    clipboard.setPrimaryClip(clip)
}

UX recommendation

Disable the Copy button when no text is available.

Be mindful that copied content may be visible through the system clipboard.

24. Add Language Identification

ML Kit Language Identification can determine the likely language of recognized text.

Initialize the client:

val languageIdentifier =
    LanguageIdentification.getClient()

Analyze text:

languageIdentifier
    .identifyLanguage(recognizedText)
    .addOnSuccessListener { languageCode ->

        // Example: "en", "fr", "es"
    }

Important

The API may return und when the language cannot be determined reliably.

Language identification and OCR script recognition are separate capabilities.

A language can be identifiable from text even when your installed OCR model cannot recognize that language’s script from an image.

25. Use Language Confidence Scores

For more detailed results:

languageIdentifier
    .identifyPossibleLanguages(
        recognizedText
    )
    .addOnSuccessListener { languages ->

        languages.forEach { language ->

            Log.d(
                "Language",
                "${language.languageTag}: " +
                    language.confidence
            )
        }
    }

This helps when text contains multiple languages or the detection result is uncertain.

Avoid automatically selecting a translation model from a low-confidence language result.

26. Understand On-Device Translation

ML Kit Translation supports translation between a defined set of languages.

Translation models are downloaded to the device.

Once the required models are available, translation can run without sending the text to a translation server.

Important limitations

  • Translation language support differs from language identification support.
  • A model download may require internet connectivity.
  • Translation quality varies by language pair.
  • Short phrases may be ambiguous.
  • Technical terminology may not translate perfectly.
  • Some translations may use English as an intermediate language.

27. Initialize the Translator

Create a translator for English-to-Spanish translation.

val options =
    TranslatorOptions.Builder()
        .setSourceLanguage(
            TranslateLanguage.ENGLISH
        )
        .setTargetLanguage(
            TranslateLanguage.SPANISH
        )
        .build()

val translator =
    Translation.getClient(options)

Required imports include:

import com.google.mlkit.nl.translate.TranslateLanguage
import com.google.mlkit.nl.translate.Translation
import com.google.mlkit.nl.translate.TranslatorOptions

28. Download Translation Models

Before translating, download the required model.

val conditions =
    DownloadConditions.Builder()
        .requireWifi()
        .build()

translator.downloadModelIfNeeded(
    conditions
)
    .addOnSuccessListener {

        // Translation model is ready.

    }
    .addOnFailureListener {

        // Show download error.
    }

Requiring Wi-Fi is optional.

For a production app, consider giving users control over downloading models through mobile data.

29. Translate Recognized Text

After model preparation:

translator.translate(
    recognizedText
)
    .addOnSuccessListener { translated ->

        // Display translated text.

    }
    .addOnFailureListener { error ->

        // Handle translation failure.
    }

Example:

Recognized text

Welcome to Hyderabad.

Translated text

Bienvenido a Hyderabad.

Translation results may vary depending on the model and context.

30. Create a Translation Repository

Use Coroutines to simplify asynchronous ML Kit calls.

class TranslationRepository {

    suspend fun translateEnglishToSpanish(
        text: String
    ): String {

        val options =
            TranslatorOptions.Builder()
                .setSourceLanguage(
                    TranslateLanguage.ENGLISH
                )
                .setTargetLanguage(
                    TranslateLanguage.SPANISH
                )
                .build()

        val translator =
            Translation.getClient(options)

        return try {

            translator
                .downloadModelIfNeeded()
                .await()

            translator
                .translate(text)
                .await()

        } finally {

            translator.close()
        }
    }
}

This is a simple example.

A production app should reuse translators for frequently used language pairs instead of recreating and closing them for every request.

31. Automatically Select the Source Language

Combine Language Identification and Translation.

Workflow

OCR Recognized Text
        |
        v
Language Identification
        |
        v
Detected Language Code
        |
        v
Validate Translation Support
        |
        v
Prepare Translator
        |
        v
Download Model if Needed
        |
        v
Translate Text

Important

Do not assume every BCP-47 language tag returned by Language Identification maps directly to a supported ML Kit translation language.

Normalize and validate language codes against the supported translation-language list.

If detection returns und or an unsupported language, let the user choose the source language manually.

32. Avoid Translating Every Camera Frame

One of the biggest mistakes in live OCR applications is triggering translation continuously.

A camera can generate many frames per second.

Translating every recognized result would create unnecessary processing and UI instability.

Recommended approach

  • Run OCR continuously at a controlled rate.
  • Display the latest stable recognized text.
  • Let users freeze or select the text.
  • Perform language identification once.
  • Translate only when requested.
  • Cache results for unchanged text.

This produces a more predictable user experience.

33. Create the Jetpack Compose Scanner Screen

Suggested interface:

+----------------------------------+
|          Live Text Scanner       |
|                                  |
|  +----------------------------+  |
|  |                            |  |
|  |       CAMERA PREVIEW       |  |
|  |                            |  |
|  |   [Highlighted OCR Text]   |  |
|  |                            |  |
|  +----------------------------+  |
|                                  |
|       [ Freeze Scan ]            |
|                                  |
|  Recognized Text                 |
|  ------------------------------  |
|  Welcome to Hyderabad           |
|                                  |
|  [ Copy ]       [ Translate ]    |
|                                  |
|  Language: English               |
|                                  |
|  Translated Text                 |
|  ------------------------------  |
|  Bienvenido a Hyderabad          |
|                                  |
+----------------------------------+

Use Material 3 cards and clear typography.

Keep the camera preview as the primary visual element.

34. Build a Recognized Text Card

@Composable
fun RecognizedTextCard(
    text: String,
    onCopy: () -> Unit,
    onTranslate: () -> Unit
) {
    ElevatedCard(
        modifier = Modifier.fillMaxWidth()
    ) {

        Column(
            modifier = Modifier.padding(16.dp),
            verticalArrangement =
                Arrangement.spacedBy(12.dp)
        ) {

            Text(
                text = "Recognized Text",
                style =
                    MaterialTheme.typography.titleMedium
            )

            Text(
                text = text.ifBlank {
                    "Point your camera at text"
                }
            )

            Row(
                horizontalArrangement =
                    Arrangement.spacedBy(12.dp)
            ) {

                OutlinedButton(
                    onClick = onCopy,
                    enabled = text.isNotBlank()
                ) {
                    Text("Copy")
                }

                Button(
                    onClick = onTranslate,
                    enabled = text.isNotBlank()
                ) {
                    Text("Translate")
                }
            }
        }
    }
}

35. Add a Translation Result Card

@Composable
fun TranslationResultCard(
    translatedText: String,
    isLoading: Boolean
) {
    ElevatedCard(
        modifier = Modifier.fillMaxWidth()
    ) {

        Column(
            modifier = Modifier.padding(16.dp)
        ) {

            Text(
                text = "Translation",
                style =
                    MaterialTheme.typography.titleMedium
            )

            if (isLoading) {

                CircularProgressIndicator()

            } else {

                Text(
                    text = translatedText
                )
            }
        }
    }
}

Provide a retry action when model download or translation fails.

36. Add Flashlight Control

CameraX provides camera control APIs.

camera.cameraControl
    .enableTorch(true)

Turn it off:

camera.cameraControl
    .enableTorch(false)

Before displaying the flashlight control, check whether the device has a flash unit.

val hasFlash =
    camera.cameraInfo.hasFlashUnit()

Recommended UX

Display a flashlight button over the camera preview.

Disable it on devices without a flash.

37. Add Tap-to-Focus

CameraX supports focus and metering actions.

val point =
    previewView.meteringPointFactory
        .createPoint(x, y)

val action =
    FocusMeteringAction.Builder(
        point
    ).build()

camera.cameraControl
    .startFocusAndMetering(action)

This can improve text clarity when users scan documents at different distances.

For production, convert tap coordinates appropriately and handle the focus result.

38. Optimize OCR Frame Processing

Real-time OCR must balance accuracy and performance.

Recommended practices

  • Use STRATEGY_KEEP_ONLY_LATEST.
  • Avoid creating a recognizer for each frame.
  • Keep the image-analysis pipeline lightweight.
  • Close every ImageProxy.
  • Avoid unnecessary Bitmap conversions.
  • Process frames at a controlled rate.
  • Skip duplicate recognized text.
  • Avoid translation during continuous scanning.
  • Release the camera when the screen closes.
  • Test on lower-end devices.

Important

Camera frame resolution affects OCR accuracy.

Very low resolution can make small text unreadable.

Very high resolution increases processing cost.

Test a practical resolution for your target devices.

39. Reduce Duplicate OCR Updates

CameraX may recognize the same text repeatedly.

Avoid updating Compose state unnecessarily.

fun updateRecognizedText(
    newText: String
) {
    val current =
        _uiState.value.recognizedText

    if (current == newText) {
        return
    }

    _uiState.update {
        it.copy(
            recognizedText = newText
        )
    }
}

For advanced implementations, consider text stability across multiple frames before presenting a final result.

40. Support Multiple OCR Scripts

ML Kit Text Recognition v2 supports different script models.

ScriptTypical languages
LatinEnglish, French, Spanish and others
DevanagariHindi, Marathi and others
ChineseChinese
JapaneseJapanese
KoreanKorean

Each supported script uses a corresponding recognition model.

Important limitation

The listed ML Kit Text Recognition v2 script models do not include Telugu.

Do not assume that language identification or translation support implies OCR support for the same script.

41. Add Image Import as an Alternative

Some users may want to recognize text from existing images rather than using the camera.

Android Photo Picker is suitable for selecting images without requesting broad photo-library access.

Workflow

Choose Image
      |
      v
Android Photo Picker
      |
      v
Selected Image URI
      |
      v
ML Kit Text Recognition
      |
      v
Recognized Text
      |
      v
Copy / Translate

This is a useful fallback for devices without a supported camera.

42. Offline Functionality

Different features have different offline requirements.

FeatureOffline support
CameraX previewYes
Bundled Latin OCRYes
Bundled language identificationYes
ML Kit translationAfter required model download
Copy to clipboardYes
Local result displayYes
Cloud synchronizationRequires connectivity

Recommendation

Provide an Offline Language Models screen.

Allow users to prepare translation models before traveling.

Clearly indicate when a model download is required.

43. Recommended MVVM Project Structure

com.moiks.livetextscanner/
|
+-- camera/
|   +-- CameraController.kt
|   +-- TextAnalyzer.kt
|   +-- CameraPreview.kt
|
+-- data/
|   +-- mlkit/
|       +-- OcrRepository.kt
|       +-- LanguageRepository.kt
|       +-- TranslationRepository.kt
|
+-- domain/
|   +-- model/
|   |   +-- OcrResult.kt
|   |   +-- OcrLine.kt
|   |
|   +-- usecase/
|       +-- RecognizeTextUseCase.kt
|       +-- TranslateTextUseCase.kt
|
+-- presentation/
|   +-- TextScannerViewModel.kt
|   +-- TextScannerUiState.kt
|
+-- ui/
|   +-- TextScannerScreen.kt
|   +-- OcrOverlay.kt
|   +-- RecognizedTextCard.kt
|   +-- TranslationResultCard.kt
|
+-- MainActivity.kt

Responsibilities

Camera layer: Manages preview and image analysis.

OCR repository: Converts frames into recognized text.

Language repository: Identifies text language.

Translation repository: Manages translation models.

ViewModel: Coordinates user actions and state.

Compose UI: Displays preview, results, and controls.

44. Privacy and Security

Live OCR may capture sensitive information.

Examples include:

  • Identity documents.
  • Financial statements.
  • Personal messages.
  • Business documents.
  • Contact information.
  • Medical information.

Recommended practices

  • Process camera frames on-device.
  • Avoid uploading images without consent.
  • Do not log recognized text.
  • Do not automatically store camera frames.
  • Explain camera permission clearly.
  • Avoid retaining sensitive data unnecessarily.
  • Secure copied or shared results.
  • Provide a clear way to delete saved scans.

Important

On-device processing does not automatically protect text that the application subsequently copies, stores, or shares.

45. Testing Strategy

Test scenarioExpected behavior
Camera permission grantedPreview starts
Permission deniedHelpful explanation
Clear printed textOCR recognizes text
Blurry textGraceful result handling
Low-light environmentFlashlight available when supported
Rotated deviceCorrect image orientation
Portrait cameraOverlay aligned
Landscape cameraOverlay aligned
Rapid camera movementNo analysis backlog
Freeze scanningStable result retained
Copy textClipboard updated
English detectionCorrect language identification
Unknown languageManual selection available
Translation model missingDownload requested
Offline with modelTranslation works
Offline without modelHelpful message
Unsupported OCR scriptLimitation explained
App backgroundedCamera resources managed
Screen rotationLifecycle handled
Lower-end deviceAcceptable responsiveness
Dark modeReadable controls
TabletAdaptive layout

46. Common Development Mistakes

Mistake 1: Forgetting to Close ImageProxy

This can stall image analysis.

Mistake 2: Creating ML Kit Clients for Every Frame

Reuse clients for better performance.

Mistake 3: Blocking the Main Thread During Camera Initialization

Initialize CameraX asynchronously.

Mistake 4: Drawing Bounding Boxes Without Coordinate Transformation

Image and preview coordinates are not necessarily identical.

Mistake 5: Translating Every Frame

Translate stable text on demand.

Mistake 6: Assuming Every Language Is Supported

OCR, language identification, and translation have different language support.

Mistake 7: Ignoring Model Downloads

Prepare translation models before attempting offline translation.

Mistake 8: Updating Compose State Excessively

Filter duplicate OCR results.

Mistake 9: Ignoring Camera Lifecycle

Release camera resources when appropriate.

Mistake 10: Assuming OCR Is Perfect

Text recognition depends on lighting, resolution, focus, and script support.

47. Production Checklist

Before publishing:

  • Camera permission configured.
  • Runtime permission flow implemented.
  • CameraX Preview configured.
  • ImageAnalysis configured.
  • Backpressure strategy selected.
  • Analysis executor managed.
  • ML Kit OCR initialized.
  • Every ImageProxy closed.
  • OCR results mapped into UI models.
  • Bounding-box coordinates transformed.
  • Duplicate results filtered.
  • Freeze and resume implemented.
  • Clipboard integration works.
  • Language Identification integrated.
  • Unsupported language handling implemented.
  • Translation model downloads implemented.
  • Translation clients reused appropriately.
  • Offline behavior tested.
  • Flashlight control implemented.
  • Camera lifecycle tested.
  • Privacy requirements reviewed.
  • Accessibility tested.
  • Physical Android devices tested.

48. Frequently Asked Questions

What is Android ML Kit Camera OCR?

It is a camera-based text recognition workflow built using Android camera APIs and Google’s ML Kit Text Recognition.

Does ML Kit provide the camera preview?

No. Use CameraX or another Android camera API for live preview and frame capture.

Can ML Kit recognize text in real time?

Yes. ML Kit can process frames from CameraX to recognize text continuously.

Does CameraX require camera permission?

Yes. A custom camera experience requires runtime camera permission.

Can I translate recognized text?

Yes. Combine Text Recognition with ML Kit Language Identification and Translation.

Does OCR work offline?

The bundled OCR model works offline.

Does translation work offline?

Yes, after the required translation model is downloaded.

Can I detect Hindi text?

ML Kit provides a Devanagari OCR model that can recognize supported Devanagari-script text.

Can I detect Telugu text?

The listed ML Kit Text Recognition v2 script models do not include Telugu.

Can I scan text from images in the gallery?

Yes. Use Android Photo Picker and ML Kit Text Recognition.

Can I copy recognized text?

Yes. Use Android’s clipboard APIs.

Can I highlight recognized words?

Yes. Use ML Kit bounding boxes and draw overlays in Compose.

Does OCR require a backend?

No. ML Kit OCR can run on-device.

Can this app work without internet?

Camera preview, bundled OCR, and local UI features can work offline. Translation requires its models to be available locally.

Can I publish a live text scanner app on Google Play?

Yes, provided it meets Google Play policies, permission requirements, privacy obligations, and technical quality expectations.

Conclusion

Building a live camera text scanner is a practical way to combine modern Android development with on-device machine learning.

Using CameraX, ML Kit Text Recognition, Language Identification, Translation, Kotlin, and Jetpack Compose, developers can create an application that recognizes and translates text directly from the camera.

The most important practices are:

  • Use CameraX for camera preview and frame analysis.
  • Use ML Kit for text recognition.
  • Close every ImageProxy.
  • Keep the analysis pipeline efficient.
  • Transform OCR coordinates correctly for overlays.
  • Avoid redundant UI updates.
  • Freeze recognized text before translation.
  • Validate supported languages.
  • Download translation models when required.
  • Manage camera and ML Kit lifecycles.
  • Protect sensitive user information.
  • Test extensively on physical devices.

A production-quality live text scanner should deliver accurate recognition, responsive camera performance, intuitive translation controls, and strong privacy protections.

This architecture provides a foundation for translation utilities, travel assistants, educational tools, accessibility applications, and intelligent camera-based productivity apps.

Official References


Published by Moiks Developer Blog — Practical tutorials covering Android, Kotlin, Jetpack Compose, Flutter, React Native, SwiftUI, and modern software engineering.

Explore Apps by Moiks Studio

Discover our Android apps and games on Google Play.

View All Android Apps →

Leave a Comment