{"id":358,"date":"2026-10-30T11:45:00","date_gmt":"2026-10-30T11:45:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=358"},"modified":"2026-11-06T08:47:57","modified_gmt":"2026-11-06T08:47:57","slug":"android-ml-kit-camera-ocr-with-kotlin-jetpack-compose-build-a-live-text-scanner-translator","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/android-ml-kit-camera-ocr-with-kotlin-jetpack-compose-build-a-live-text-scanner-translator\/","title":{"rendered":"Android ML Kit Camera OCR with Kotlin &#038; Jetpack Compose: Build a Live Text Scanner &#038; Translator"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Imagine pointing your Android phone at a printed document, restaurant menu, product label, or signboard and instantly seeing the text appear on your screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now imagine selecting that text, copying it, identifying its language, and translating it into English, Hindi, Spanish, or another supported language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This experience is possible using Google&#8217;s on-device machine learning technologies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll build a <strong>Live Text Scanner &amp; Translator<\/strong> using CameraX, Google ML Kit Text Recognition, Language Identification, On-Device Translation, Kotlin, and Jetpack Compose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike a traditional document scanner, which captures a document and processes it afterward, our application continuously analyzes frames from the camera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll learn how to manage the camera lifecycle, process images efficiently, extract text, draw OCR overlays, and integrate translation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also discuss offline functionality, permissions, performance, privacy, and production-ready architecture.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is a Live Camera OCR Scanner?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">OCR stands for Optical Character Recognition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A live camera OCR scanner analyzes images captured by a camera and converts visible text into machine-readable characters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the camera might capture:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Hyderabad\n\nMetro Station\n\nPlatform 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit can recognize the text and return it to the application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The app can then offer actions such as copying, searching, or translating the recognized content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common applications<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Live text scanning.<\/li>\n\n\n\n<li>Document reading.<\/li>\n\n\n\n<li>Product label recognition.<\/li>\n\n\n\n<li>Menu translation.<\/li>\n\n\n\n<li>Travel sign translation.<\/li>\n\n\n\n<li>Business card scanning.<\/li>\n\n\n\n<li>Educational reading tools.<\/li>\n\n\n\n<li>Accessibility utilities.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Technologies Used<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our application combines several Android technologies.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Technology<\/th><th>Purpose<\/th><\/tr><tr><td>CameraX<\/td><td>Camera preview and frame analysis<\/td><\/tr><tr><td>ML Kit Text Recognition<\/td><td>Recognize text in images<\/td><\/tr><tr><td>ML Kit Language Identification<\/td><td>Detect the text&#8217;s language<\/td><\/tr><tr><td>ML Kit Translation<\/td><td>Translate recognized text<\/td><\/tr><tr><td>Kotlin<\/td><td>Application logic<\/td><\/tr><tr><td>Jetpack Compose<\/td><td>Modern Android UI<\/td><\/tr><tr><td>Material 3<\/td><td>UI components<\/td><\/tr><tr><td>Coroutines<\/td><td>Asynchronous operations<\/td><\/tr><tr><td>StateFlow<\/td><td>Reactive state<\/td><\/tr><tr><td>MVVM<\/td><td>Separation of concerns<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Each technology handles a different part of the workflow.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Application Features<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our application will provide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Real-time camera preview.<\/li>\n\n\n\n<li>Live text recognition.<\/li>\n\n\n\n<li>OCR text bounding boxes.<\/li>\n\n\n\n<li>Pause and resume scanning.<\/li>\n\n\n\n<li>Copy recognized text.<\/li>\n\n\n\n<li>Detect source language.<\/li>\n\n\n\n<li>Select translation language.<\/li>\n\n\n\n<li>Download translation models.<\/li>\n\n\n\n<li>Translate recognized text.<\/li>\n\n\n\n<li>Display translated results.<\/li>\n\n\n\n<li>Offline OCR.<\/li>\n\n\n\n<li>Flashlight control.<\/li>\n\n\n\n<li>Camera permission handling.<\/li>\n\n\n\n<li>Error and loading states.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For the first version, we&#8217;ll use Latin-script OCR and English-to-Spanish translation as a working example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Application Architecture<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Android Camera\n      |\n      v\nCameraX Preview + ImageAnalysis\n      |\n      v\nCamera Frame\n      |\n      v\nML Kit Text Recognition\n      |\n      +----&gt; Recognized Text\n      |           |\n      |           v\n      |      Language Detection\n      |           |\n      |           v\n      |      ML Kit Translation\n      |           |\n      |           v\n      |      Translated Text\n      |\n      +----&gt; OCR Bounding Boxes\n                    |\n                    v\n              Compose Overlay\n                    |\n                    v\n                User Actions<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The camera pipeline should remain separate from the UI and translation layers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This makes the application easier to maintain and test.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Project Requirements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recommended configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Project: LiveTextScanner\n\nPackage:\ncom.moiks.livetextscanner\n\nLanguage: Kotlin\n\nUI: Jetpack Compose\n\nArchitecture: MVVM\n\nMinimum SDK: 23<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX and ML Kit support Android devices with different capabilities. Always test the combined integration on physical devices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Add Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Open <code>app\/build.gradle.kts<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    \/\/ CameraX\n    implementation(\n        \"androidx.camera:camera-core:&lt;camerax-version&gt;\"\n    )\n\n    implementation(\n        \"androidx.camera:camera-camera2:&lt;camerax-version&gt;\"\n    )\n\n    implementation(\n        \"androidx.camera:camera-lifecycle:&lt;camerax-version&gt;\"\n    )\n\n    implementation(\n        \"androidx.camera:camera-view:&lt;camerax-version&gt;\"\n    )\n\n    \/\/ ML Kit OCR - Latin script\n    implementation(\n        \"com.google.mlkit:text-recognition:16.0.1\"\n    )\n\n    \/\/ Language identification\n    implementation(\n        \"com.google.mlkit:language-id:17.0.6\"\n    )\n\n    \/\/ On-device translation\n    implementation(\n        \"com.google.mlkit:translate:&lt;current-version&gt;\"\n    )\n\n    \/\/ Google Tasks coroutine integration\n    implementation(\n        \"org.jetbrains.kotlinx:\" +\n        \"kotlinx-coroutines-play-services:&lt;version&gt;\"\n    )\n\n    \/\/ Compose and Lifecycle\n    implementation(\n        \"androidx.activity:activity-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:\" +\n        \"lifecycle-viewmodel-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:\" +\n        \"lifecycle-runtime-compose:&lt;version&gt;\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace placeholders with compatible published versions from the official Android and ML Kit documentation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The CameraX artifacts should use the same compatible release.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Configure Camera Permission<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add camera permission to <code>AndroidManifest.xml<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;uses-permission\n    android:name=\"android.permission.CAMERA\" \/&gt;\n\n&lt;uses-feature\n    android:name=\"android.hardware.camera\"\n    android:required=\"false\" \/&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Set <code>required=\"true\"<\/code> if the application cannot function without a camera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a utility that also supports importing images, declaring the camera optional may be more appropriate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important distinction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike the Google-provided Document Scanner from Article 90, a custom CameraX camera experience requires your application to request camera permission.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Request Runtime Camera Permission<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose supports runtime permissions through Activity Result APIs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun CameraPermissionScreen(\n    content: @Composable () -&gt; Unit\n) {\n    val context = LocalContext.current\n\n    var hasPermission by remember {\n        mutableStateOf(\n            ContextCompat.checkSelfPermission(\n                context,\n                Manifest.permission.CAMERA\n            ) == PackageManager.PERMISSION_GRANTED\n        )\n    }\n\n    val launcher =\n        rememberLauncherForActivityResult(\n            ActivityResultContracts\n                .RequestPermission()\n        ) { granted -&gt;\n\n            hasPermission = granted\n        }\n\n    if (hasPermission) {\n\n        content()\n\n    } else {\n\n        Button(\n            onClick = {\n                launcher.launch(\n                    Manifest.permission.CAMERA\n                )\n            }\n        ) {\n            Text(\"Allow Camera Access\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For production, also explain why the permission is needed and handle permanently denied permission with a suitable settings option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Understand CameraX Use Cases<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX provides several camera use cases.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Use case<\/th><th>Purpose<\/th><\/tr><tr><td>Preview<\/td><td>Display camera output<\/td><\/tr><tr><td>ImageCapture<\/td><td>Capture photographs<\/td><\/tr><tr><td>ImageAnalysis<\/td><td>Process camera frames<\/td><\/tr><tr><td>VideoCapture<\/td><td>Record video<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For real-time OCR, we primarily need:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Preview + ImageAnalysis<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ImageCapture can be added later for saving snapshots.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Create the Camera Preview<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX provides <code>PreviewView<\/code>, which can be embedded in Compose using <code>AndroidView<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun CameraPreview(\n    previewView: PreviewView\n) {\n    AndroidView(\n        factory = {\n            previewView\n        },\n        modifier = Modifier.fillMaxSize()\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the <code>PreviewView<\/code> instance stable across recompositions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11. Configure CameraX<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create the camera preview and image-analysis use cases.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val preview = Preview.Builder()\n    .build()\n\nval imageAnalysis =\n    ImageAnalysis.Builder()\n        .setBackpressureStrategy(\n            ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST\n        )\n        .build()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why use KEEP_ONLY_LATEST?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">OCR processing can take longer than the time between camera frames.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This strategy prevents an unbounded queue of outdated frames.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The analyzer receives the most recent available frame when it becomes ready.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">12. Initialize ML Kit Text Recognition<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create the text recognizer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val recognizer =\n    TextRecognition.getClient(\n        TextRecognizerOptions.DEFAULT_OPTIONS\n    )<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Required imports include:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.google.mlkit.vision.text.TextRecognition\nimport com.google.mlkit.vision.text.latin.TextRecognizerOptions<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reuse the recognizer rather than creating a new instance for every frame.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Close it when the camera processing component is permanently released.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. Convert Camera Frames into ML Kit Images<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX provides an <code>ImageProxy<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit accepts an <code>InputImage<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val mediaImage = imageProxy.image\n\nif (mediaImage != null) {\n\n    val inputImage =\n        InputImage.fromMediaImage(\n            mediaImage,\n            imageProxy.imageInfo.rotationDegrees\n        )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The rotation value is essential for correct text recognition and overlay alignment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">14. Create the OCR Image Analyzer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Implement <code>ImageAnalysis.Analyzer<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TextAnalyzer(\n    private val recognizer: TextRecognizer,\n    private val onResult: (Text) -&gt; Unit,\n    private val onError: (Exception) -&gt; Unit\n) : ImageAnalysis.Analyzer {\n\n    override fun analyze(\n        imageProxy: ImageProxy\n    ) {\n        val mediaImage =\n            imageProxy.image\n\n        if (mediaImage == null) {\n            imageProxy.close()\n            return\n        }\n\n        val inputImage =\n            InputImage.fromMediaImage(\n                mediaImage,\n                imageProxy.imageInfo.rotationDegrees\n            )\n\n        recognizer.process(inputImage)\n            .addOnSuccessListener { result -&gt;\n                onResult(result)\n            }\n            .addOnFailureListener { error -&gt;\n                onError(error)\n            }\n            .addOnCompleteListener {\n                imageProxy.close()\n            }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Critical rule<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every <code>ImageProxy<\/code> must be closed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If an image is not closed, CameraX may stop delivering new frames.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a production implementation, also guard against synchronous failures before a Task is returned, and ensure the proxy is closed on every error path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">15. Bind CameraX to the Lifecycle<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A camera use case must be bound to a lifecycle owner.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val cameraProvider =\n    ProcessCameraProvider\n        .getInstance(context)\n        .get()\n\nval cameraSelector =\n    CameraSelector.DEFAULT_BACK_CAMERA\n\ncameraProvider.unbindAll()\n\ncameraProvider.bindToLifecycle(\n    lifecycleOwner,\n    cameraSelector,\n    preview,\n    imageAnalysis\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Production recommendation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>.get()<\/code> call shown above is a simplified illustration. Do not block the main thread waiting for the camera provider.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the provider&#8217;s asynchronous listener or coroutine integration, then bind use cases on the main thread.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Configure the preview surface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>preview.surfaceProvider =\n    previewView.surfaceProvider<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unbind or release camera use cases when appropriate for the screen lifecycle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">16. Use a Dedicated Analysis Executor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create an executor for image analysis.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val cameraExecutor =\n    Executors.newSingleThreadExecutor()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Attach the analyzer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>imageAnalysis.setAnalyzer(\n    cameraExecutor,\n    textAnalyzer\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why a single-thread executor?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It provides a predictable analysis pipeline.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Combined with <code>STRATEGY_KEEP_ONLY_LATEST<\/code>, it helps control frame processing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Shut down the executor when the camera component is disposed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">17. Extract Recognized Text<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit returns a <code>Text<\/code> object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val recognizedText =\n    result.text<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Hyderabad\n\nMetro Station\n\nPlatform 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result also contains structured information about recognized text blocks, lines, and elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">18. Understand OCR Result Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Text Result\n    |\n    +-- TextBlock\n           |\n           +-- Line\n                  |\n                  +-- Element<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Inspect text lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result.textBlocks.forEach { block -&gt;\n\n    block.lines.forEach { line -&gt;\n\n        val text = line.text\n\n        val bounds = line.boundingBox\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Bounding boxes are useful for drawing text detection overlays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">19. Create an OCR Result Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Keep ML Kit objects separate from presentation models where practical.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data class OcrLine(\n    val text: String,\n    val boundingBox: Rect?\n)\n\ndata class OcrResult(\n    val text: String,\n    val lines: List&lt;OcrLine&gt;\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Map the ML Kit result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun mapOcrResult(\n    result: Text\n): OcrResult {\n\n    val lines =\n        result.textBlocks.flatMap { block -&gt;\n\n            block.lines.map { line -&gt;\n\n                OcrLine(\n                    text = line.text,\n                    boundingBox = line.boundingBox\n                )\n            }\n        }\n\n    return OcrResult(\n        text = result.text,\n        lines = lines\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For overlay rendering, preserve image dimensions and orientation metadata alongside these boxes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">20. Display OCR Bounding Boxes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A modern text scanner can highlight recognized text directly over the camera preview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose provides <code>Canvas<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun OcrOverlay(\n    lines: List&lt;OcrLine&gt;,\n    modifier: Modifier = Modifier\n) {\n    Canvas(modifier = modifier) {\n\n        lines.forEach { line -&gt;\n\n            val bounds =\n                line.boundingBox\n                    ?: return@forEach\n\n            drawRect(\n                color = Color.Cyan,\n                topLeft = Offset(\n                    bounds.left.toFloat(),\n                    bounds.top.toFloat()\n                ),\n                size = Size(\n                    bounds.width().toFloat(),\n                    bounds.height().toFloat()\n                ),\n                style = Stroke(\n                    width = 3.dp.toPx()\n                )\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the basic drawing operation, not a complete camera overlay.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The OCR bounding boxes use image coordinates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A production overlay must transform those coordinates into preview coordinates, accounting for image rotation, scaling, cropping, and possible mirroring.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Otherwise, rectangles may not align with the visible text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">21. Create the OCR ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Define a UI state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data class TextScannerUiState(\n    val recognizedText: String = \"\",\n    val detectedLanguage: String = \"\",\n    val translatedText: String = \"\",\n    val isScanning: Boolean = true,\n    val isTranslating: Boolean = false,\n    val errorMessage: String? = null\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the ViewModel:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TextScannerViewModel : ViewModel() {\n\n    private val _uiState =\n        MutableStateFlow(\n            TextScannerUiState()\n        )\n\n    val uiState =\n        _uiState.asStateFlow()\n\n    fun updateRecognizedText(\n        text: String\n    ) {\n        _uiState.update {\n            it.copy(\n                recognizedText = text\n            )\n        }\n    }\n\n    fun pauseScanning() {\n        _uiState.update {\n            it.copy(isScanning = false)\n        }\n    }\n\n    fun resumeScanning() {\n        _uiState.update {\n            it.copy(isScanning = true)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For production, connect this state to the analyzer through a dedicated controller and avoid updating UI state with every redundant frame result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">22. Implement Pause and Resume<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Continuous recognition is useful while positioning the camera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, users need a stable result before copying or translating text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended workflow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Live Camera Preview\n        |\n        v\nRecognize Text\n        |\n        v\nUser Taps Freeze\n        |\n        v\nStable OCR Result\n        |\n        +-- Copy\n        |\n        +-- Translate\n        |\n        +-- Share<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Pause can be implemented by disabling or clearing the image analyzer while retaining the last recognized result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A true frozen camera preview additionally requires preserving a captured frame or switching to a still-image preview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simply stopping analysis does not freeze the live Preview use case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">23. Copy Recognized Text to Clipboard<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use Android&#8217;s clipboard APIs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun copyText(\n    context: Context,\n    text: String\n) {\n    val clipboard =\n        context.getSystemService(\n            Context.CLIPBOARD_SERVICE\n        ) as ClipboardManager\n\n    val clip =\n        ClipData.newPlainText(\n            \"Recognized Text\",\n            text\n        )\n\n    clipboard.setPrimaryClip(clip)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">UX recommendation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Disable the Copy button when no text is available.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Be mindful that copied content may be visible through the system clipboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">24. Add Language Identification<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit Language Identification can determine the likely language of recognized text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize the client:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val languageIdentifier =\n    LanguageIdentification.getClient()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Analyze text:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>languageIdentifier\n    .identifyLanguage(recognizedText)\n    .addOnSuccessListener { languageCode -&gt;\n\n        \/\/ Example: \"en\", \"fr\", \"es\"\n    }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The API may return <code>und<\/code> when the language cannot be determined reliably.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Language identification and OCR script recognition are separate capabilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A language can be identifiable from text even when your installed OCR model cannot recognize that language&#8217;s script from an image.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">25. Use Language Confidence Scores<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For more detailed results:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>languageIdentifier\n    .identifyPossibleLanguages(\n        recognizedText\n    )\n    .addOnSuccessListener { languages -&gt;\n\n        languages.forEach { language -&gt;\n\n            Log.d(\n                \"Language\",\n                \"${language.languageTag}: \" +\n                    language.confidence\n            )\n        }\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This helps when text contains multiple languages or the detection result is uncertain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid automatically selecting a translation model from a low-confidence language result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">26. Understand On-Device Translation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit Translation supports translation between a defined set of languages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Translation models are downloaded to the device.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the required models are available, translation can run without sending the text to a translation server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important limitations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Translation language support differs from language identification support.<\/li>\n\n\n\n<li>A model download may require internet connectivity.<\/li>\n\n\n\n<li>Translation quality varies by language pair.<\/li>\n\n\n\n<li>Short phrases may be ambiguous.<\/li>\n\n\n\n<li>Technical terminology may not translate perfectly.<\/li>\n\n\n\n<li>Some translations may use English as an intermediate language.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">27. Initialize the Translator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a translator for English-to-Spanish translation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val options =\n    TranslatorOptions.Builder()\n        .setSourceLanguage(\n            TranslateLanguage.ENGLISH\n        )\n        .setTargetLanguage(\n            TranslateLanguage.SPANISH\n        )\n        .build()\n\nval translator =\n    Translation.getClient(options)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Required imports include:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.google.mlkit.nl.translate.TranslateLanguage\nimport com.google.mlkit.nl.translate.Translation\nimport com.google.mlkit.nl.translate.TranslatorOptions<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">28. Download Translation Models<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before translating, download the required model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val conditions =\n    DownloadConditions.Builder()\n        .requireWifi()\n        .build()\n\ntranslator.downloadModelIfNeeded(\n    conditions\n)\n    .addOnSuccessListener {\n\n        \/\/ Translation model is ready.\n\n    }\n    .addOnFailureListener {\n\n        \/\/ Show download error.\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Requiring Wi-Fi is optional.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a production app, consider giving users control over downloading models through mobile data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">29. Translate Recognized Text<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After model preparation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>translator.translate(\n    recognizedText\n)\n    .addOnSuccessListener { translated -&gt;\n\n        \/\/ Display translated text.\n\n    }\n    .addOnFailureListener { error -&gt;\n\n        \/\/ Handle translation failure.\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Recognized text<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Hyderabad.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Translated text<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Bienvenido a Hyderabad.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Translation results may vary depending on the model and context.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">30. Create a Translation Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use Coroutines to simplify asynchronous ML Kit calls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TranslationRepository {\n\n    suspend fun translateEnglishToSpanish(\n        text: String\n    ): String {\n\n        val options =\n            TranslatorOptions.Builder()\n                .setSourceLanguage(\n                    TranslateLanguage.ENGLISH\n                )\n                .setTargetLanguage(\n                    TranslateLanguage.SPANISH\n                )\n                .build()\n\n        val translator =\n            Translation.getClient(options)\n\n        return try {\n\n            translator\n                .downloadModelIfNeeded()\n                .await()\n\n            translator\n                .translate(text)\n                .await()\n\n        } finally {\n\n            translator.close()\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a simple example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A production app should reuse translators for frequently used language pairs instead of recreating and closing them for every request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">31. Automatically Select the Source Language<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Combine Language Identification and Translation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Workflow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>OCR Recognized Text\n        |\n        v\nLanguage Identification\n        |\n        v\nDetected Language Code\n        |\n        v\nValidate Translation Support\n        |\n        v\nPrepare Translator\n        |\n        v\nDownload Model if Needed\n        |\n        v\nTranslate Text<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not assume every BCP-47 language tag returned by Language Identification maps directly to a supported ML Kit translation language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Normalize and validate language codes against the supported translation-language list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If detection returns <code>und<\/code> or an unsupported language, let the user choose the source language manually.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">32. Avoid Translating Every Camera Frame<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the biggest mistakes in live OCR applications is triggering translation continuously.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A camera can generate many frames per second.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Translating every recognized result would create unnecessary processing and UI instability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Run OCR continuously at a controlled rate.<\/li>\n\n\n\n<li>Display the latest stable recognized text.<\/li>\n\n\n\n<li>Let users freeze or select the text.<\/li>\n\n\n\n<li>Perform language identification once.<\/li>\n\n\n\n<li>Translate only when requested.<\/li>\n\n\n\n<li>Cache results for unchanged text.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This produces a more predictable user experience.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">33. Create the Jetpack Compose Scanner Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suggested interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+----------------------------------+\n|          Live Text Scanner       |\n|                                  |\n|  +----------------------------+  |\n|  |                            |  |\n|  |       CAMERA PREVIEW       |  |\n|  |                            |  |\n|  |   &#91;Highlighted OCR Text]   |  |\n|  |                            |  |\n|  +----------------------------+  |\n|                                  |\n|       &#91; Freeze Scan ]            |\n|                                  |\n|  Recognized Text                 |\n|  ------------------------------  |\n|  Welcome to Hyderabad           |\n|                                  |\n|  &#91; Copy ]       &#91; Translate ]    |\n|                                  |\n|  Language: English               |\n|                                  |\n|  Translated Text                 |\n|  ------------------------------  |\n|  Bienvenido a Hyderabad          |\n|                                  |\n+----------------------------------+<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use Material 3 cards and clear typography.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the camera preview as the primary visual element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">34. Build a Recognized Text Card<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun RecognizedTextCard(\n    text: String,\n    onCopy: () -&gt; Unit,\n    onTranslate: () -&gt; Unit\n) {\n    ElevatedCard(\n        modifier = Modifier.fillMaxWidth()\n    ) {\n\n        Column(\n            modifier = Modifier.padding(16.dp),\n            verticalArrangement =\n                Arrangement.spacedBy(12.dp)\n        ) {\n\n            Text(\n                text = \"Recognized Text\",\n                style =\n                    MaterialTheme.typography.titleMedium\n            )\n\n            Text(\n                text = text.ifBlank {\n                    \"Point your camera at text\"\n                }\n            )\n\n            Row(\n                horizontalArrangement =\n                    Arrangement.spacedBy(12.dp)\n            ) {\n\n                OutlinedButton(\n                    onClick = onCopy,\n                    enabled = text.isNotBlank()\n                ) {\n                    Text(\"Copy\")\n                }\n\n                Button(\n                    onClick = onTranslate,\n                    enabled = text.isNotBlank()\n                ) {\n                    Text(\"Translate\")\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">35. Add a Translation Result Card<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun TranslationResultCard(\n    translatedText: String,\n    isLoading: Boolean\n) {\n    ElevatedCard(\n        modifier = Modifier.fillMaxWidth()\n    ) {\n\n        Column(\n            modifier = Modifier.padding(16.dp)\n        ) {\n\n            Text(\n                text = \"Translation\",\n                style =\n                    MaterialTheme.typography.titleMedium\n            )\n\n            if (isLoading) {\n\n                CircularProgressIndicator()\n\n            } else {\n\n                Text(\n                    text = translatedText\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Provide a retry action when model download or translation fails.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">36. Add Flashlight Control<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX provides camera control APIs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>camera.cameraControl\n    .enableTorch(true)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Turn it off:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>camera.cameraControl\n    .enableTorch(false)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before displaying the flashlight control, check whether the device has a flash unit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val hasFlash =\n    camera.cameraInfo.hasFlashUnit()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended UX<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Display a flashlight button over the camera preview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Disable it on devices without a flash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">37. Add Tap-to-Focus<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX supports focus and metering actions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val point =\n    previewView.meteringPointFactory\n        .createPoint(x, y)\n\nval action =\n    FocusMeteringAction.Builder(\n        point\n    ).build()\n\ncamera.cameraControl\n    .startFocusAndMetering(action)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This can improve text clarity when users scan documents at different distances.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production, convert tap coordinates appropriately and handle the focus result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">38. Optimize OCR Frame Processing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Real-time OCR must balance accuracy and performance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>STRATEGY_KEEP_ONLY_LATEST<\/code>.<\/li>\n\n\n\n<li>Avoid creating a recognizer for each frame.<\/li>\n\n\n\n<li>Keep the image-analysis pipeline lightweight.<\/li>\n\n\n\n<li>Close every ImageProxy.<\/li>\n\n\n\n<li>Avoid unnecessary Bitmap conversions.<\/li>\n\n\n\n<li>Process frames at a controlled rate.<\/li>\n\n\n\n<li>Skip duplicate recognized text.<\/li>\n\n\n\n<li>Avoid translation during continuous scanning.<\/li>\n\n\n\n<li>Release the camera when the screen closes.<\/li>\n\n\n\n<li>Test on lower-end devices.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Camera frame resolution affects OCR accuracy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Very low resolution can make small text unreadable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Very high resolution increases processing cost.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Test a practical resolution for your target devices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">39. Reduce Duplicate OCR Updates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CameraX may recognize the same text repeatedly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid updating Compose state unnecessarily.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun updateRecognizedText(\n    newText: String\n) {\n    val current =\n        _uiState.value.recognizedText\n\n    if (current == newText) {\n        return\n    }\n\n    _uiState.update {\n        it.copy(\n            recognizedText = newText\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For advanced implementations, consider text stability across multiple frames before presenting a final result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">40. Support Multiple OCR Scripts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit Text Recognition v2 supports different script models.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Script<\/th><th>Typical languages<\/th><\/tr><tr><td>Latin<\/td><td>English, French, Spanish and others<\/td><\/tr><tr><td>Devanagari<\/td><td>Hindi, Marathi and others<\/td><\/tr><tr><td>Chinese<\/td><td>Chinese<\/td><\/tr><tr><td>Japanese<\/td><td>Japanese<\/td><\/tr><tr><td>Korean<\/td><td>Korean<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Each supported script uses a corresponding recognition model.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important limitation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The listed ML Kit Text Recognition v2 script models do not include Telugu.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not assume that language identification or translation support implies OCR support for the same script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">41. Add Image Import as an Alternative<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some users may want to recognize text from existing images rather than using the camera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Android Photo Picker is suitable for selecting images without requesting broad photo-library access.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Workflow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Choose Image\n      |\n      v\nAndroid Photo Picker\n      |\n      v\nSelected Image URI\n      |\n      v\nML Kit Text Recognition\n      |\n      v\nRecognized Text\n      |\n      v\nCopy \/ Translate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a useful fallback for devices without a supported camera.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">42. Offline Functionality<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Different features have different offline requirements.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>Offline support<\/th><\/tr><tr><td>CameraX preview<\/td><td>Yes<\/td><\/tr><tr><td>Bundled Latin OCR<\/td><td>Yes<\/td><\/tr><tr><td>Bundled language identification<\/td><td>Yes<\/td><\/tr><tr><td>ML Kit translation<\/td><td>After required model download<\/td><\/tr><tr><td>Copy to clipboard<\/td><td>Yes<\/td><\/tr><tr><td>Local result display<\/td><td>Yes<\/td><\/tr><tr><td>Cloud synchronization<\/td><td>Requires connectivity<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Recommendation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Provide an Offline Language Models screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Allow users to prepare translation models before traveling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Clearly indicate when a model download is required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">43. Recommended MVVM Project Structure<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>com.moiks.livetextscanner\/\n|\n+-- camera\/\n|   +-- CameraController.kt\n|   +-- TextAnalyzer.kt\n|   +-- CameraPreview.kt\n|\n+-- data\/\n|   +-- mlkit\/\n|       +-- OcrRepository.kt\n|       +-- LanguageRepository.kt\n|       +-- TranslationRepository.kt\n|\n+-- domain\/\n|   +-- model\/\n|   |   +-- OcrResult.kt\n|   |   +-- OcrLine.kt\n|   |\n|   +-- usecase\/\n|       +-- RecognizeTextUseCase.kt\n|       +-- TranslateTextUseCase.kt\n|\n+-- presentation\/\n|   +-- TextScannerViewModel.kt\n|   +-- TextScannerUiState.kt\n|\n+-- ui\/\n|   +-- TextScannerScreen.kt\n|   +-- OcrOverlay.kt\n|   +-- RecognizedTextCard.kt\n|   +-- TranslationResultCard.kt\n|\n+-- MainActivity.kt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Responsibilities<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Camera layer:<\/strong> Manages preview and image analysis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>OCR repository:<\/strong> Converts frames into recognized text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Language repository:<\/strong> Identifies text language.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Translation repository:<\/strong> Manages translation models.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ViewModel:<\/strong> Coordinates user actions and state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Compose UI:<\/strong> Displays preview, results, and controls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">44. Privacy and Security<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Live OCR may capture sensitive information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Examples include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identity documents.<\/li>\n\n\n\n<li>Financial statements.<\/li>\n\n\n\n<li>Personal messages.<\/li>\n\n\n\n<li>Business documents.<\/li>\n\n\n\n<li>Contact information.<\/li>\n\n\n\n<li>Medical information.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Process camera frames on-device.<\/li>\n\n\n\n<li>Avoid uploading images without consent.<\/li>\n\n\n\n<li>Do not log recognized text.<\/li>\n\n\n\n<li>Do not automatically store camera frames.<\/li>\n\n\n\n<li>Explain camera permission clearly.<\/li>\n\n\n\n<li>Avoid retaining sensitive data unnecessarily.<\/li>\n\n\n\n<li>Secure copied or shared results.<\/li>\n\n\n\n<li>Provide a clear way to delete saved scans.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On-device processing does not automatically protect text that the application subsequently copies, stores, or shares.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">45. Testing Strategy<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Test scenario<\/th><th>Expected behavior<\/th><\/tr><tr><td>Camera permission granted<\/td><td>Preview starts<\/td><\/tr><tr><td>Permission denied<\/td><td>Helpful explanation<\/td><\/tr><tr><td>Clear printed text<\/td><td>OCR recognizes text<\/td><\/tr><tr><td>Blurry text<\/td><td>Graceful result handling<\/td><\/tr><tr><td>Low-light environment<\/td><td>Flashlight available when supported<\/td><\/tr><tr><td>Rotated device<\/td><td>Correct image orientation<\/td><\/tr><tr><td>Portrait camera<\/td><td>Overlay aligned<\/td><\/tr><tr><td>Landscape camera<\/td><td>Overlay aligned<\/td><\/tr><tr><td>Rapid camera movement<\/td><td>No analysis backlog<\/td><\/tr><tr><td>Freeze scanning<\/td><td>Stable result retained<\/td><\/tr><tr><td>Copy text<\/td><td>Clipboard updated<\/td><\/tr><tr><td>English detection<\/td><td>Correct language identification<\/td><\/tr><tr><td>Unknown language<\/td><td>Manual selection available<\/td><\/tr><tr><td>Translation model missing<\/td><td>Download requested<\/td><\/tr><tr><td>Offline with model<\/td><td>Translation works<\/td><\/tr><tr><td>Offline without model<\/td><td>Helpful message<\/td><\/tr><tr><td>Unsupported OCR script<\/td><td>Limitation explained<\/td><\/tr><tr><td>App backgrounded<\/td><td>Camera resources managed<\/td><\/tr><tr><td>Screen rotation<\/td><td>Lifecycle handled<\/td><\/tr><tr><td>Lower-end device<\/td><td>Acceptable responsiveness<\/td><\/tr><tr><td>Dark mode<\/td><td>Readable controls<\/td><\/tr><tr><td>Tablet<\/td><td>Adaptive layout<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">46. Common Development Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Forgetting to Close ImageProxy<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This can stall image analysis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Creating ML Kit Clients for Every Frame<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Reuse clients for better performance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Blocking the Main Thread During Camera Initialization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize CameraX asynchronously.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Drawing Bounding Boxes Without Coordinate Transformation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Image and preview coordinates are not necessarily identical.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Translating Every Frame<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Translate stable text on demand.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Assuming Every Language Is Supported<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">OCR, language identification, and translation have different language support.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Ignoring Model Downloads<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prepare translation models before attempting offline translation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Updating Compose State Excessively<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Filter duplicate OCR results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Ignoring Camera Lifecycle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Release camera resources when appropriate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Assuming OCR Is Perfect<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Text recognition depends on lighting, resolution, focus, and script support.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">47. Production Checklist<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before publishing:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Camera permission configured.<\/li>\n\n\n\n<li>Runtime permission flow implemented.<\/li>\n\n\n\n<li>CameraX Preview configured.<\/li>\n\n\n\n<li>ImageAnalysis configured.<\/li>\n\n\n\n<li>Backpressure strategy selected.<\/li>\n\n\n\n<li>Analysis executor managed.<\/li>\n\n\n\n<li>ML Kit OCR initialized.<\/li>\n\n\n\n<li>Every ImageProxy closed.<\/li>\n\n\n\n<li>OCR results mapped into UI models.<\/li>\n\n\n\n<li>Bounding-box coordinates transformed.<\/li>\n\n\n\n<li>Duplicate results filtered.<\/li>\n\n\n\n<li>Freeze and resume implemented.<\/li>\n\n\n\n<li>Clipboard integration works.<\/li>\n\n\n\n<li>Language Identification integrated.<\/li>\n\n\n\n<li>Unsupported language handling implemented.<\/li>\n\n\n\n<li>Translation model downloads implemented.<\/li>\n\n\n\n<li>Translation clients reused appropriately.<\/li>\n\n\n\n<li>Offline behavior tested.<\/li>\n\n\n\n<li>Flashlight control implemented.<\/li>\n\n\n\n<li>Camera lifecycle tested.<\/li>\n\n\n\n<li>Privacy requirements reviewed.<\/li>\n\n\n\n<li>Accessibility tested.<\/li>\n\n\n\n<li>Physical Android devices tested.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">48. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Android ML Kit Camera OCR?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is a camera-based text recognition workflow built using Android camera APIs and Google&#8217;s ML Kit Text Recognition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does ML Kit provide the camera preview?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Use CameraX or another Android camera API for live preview and frame capture.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can ML Kit recognize text in real time?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. ML Kit can process frames from CameraX to recognize text continuously.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does CameraX require camera permission?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A custom camera experience requires runtime camera permission.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I translate recognized text?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Combine Text Recognition with ML Kit Language Identification and Translation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does OCR work offline?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The bundled OCR model works offline.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does translation work offline?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, after the required translation model is downloaded.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I detect Hindi text?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ML Kit provides a Devanagari OCR model that can recognize supported Devanagari-script text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I detect Telugu text?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The listed ML Kit Text Recognition v2 script models do not include Telugu.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I scan text from images in the gallery?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Use Android Photo Picker and ML Kit Text Recognition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I copy recognized text?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Use Android&#8217;s clipboard APIs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I highlight recognized words?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Use ML Kit bounding boxes and draw overlays in Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does OCR require a backend?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. ML Kit OCR can run on-device.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can this app work without internet?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Camera preview, bundled OCR, and local UI features can work offline. Translation requires its models to be available locally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I publish a live text scanner app on Google Play?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, provided it meets Google Play policies, permission requirements, privacy obligations, and technical quality expectations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Building a live camera text scanner is a practical way to combine modern Android development with on-device machine learning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using <strong>CameraX, ML Kit Text Recognition, Language Identification, Translation, Kotlin, and Jetpack Compose<\/strong>, developers can create an application that recognizes and translates text directly from the camera.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most important practices are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use CameraX for camera preview and frame analysis.<\/li>\n\n\n\n<li>Use ML Kit for text recognition.<\/li>\n\n\n\n<li>Close every ImageProxy.<\/li>\n\n\n\n<li>Keep the analysis pipeline efficient.<\/li>\n\n\n\n<li>Transform OCR coordinates correctly for overlays.<\/li>\n\n\n\n<li>Avoid redundant UI updates.<\/li>\n\n\n\n<li>Freeze recognized text before translation.<\/li>\n\n\n\n<li>Validate supported languages.<\/li>\n\n\n\n<li>Download translation models when required.<\/li>\n\n\n\n<li>Manage camera and ML Kit lifecycles.<\/li>\n\n\n\n<li>Protect sensitive user information.<\/li>\n\n\n\n<li>Test extensively on physical devices.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A production-quality live text scanner should deliver accurate recognition, responsive camera performance, intuitive translation controls, and strong privacy protections.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This architecture provides a foundation for translation utilities, travel assistants, educational tools, accessibility applications, and intelligent camera-based productivity apps.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Official References<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developers.google.com\/ml-kit\/vision\/text-recognition\/v2\/android\" target=\"_blank\" rel=\"noopener\">ML Kit Text Recognition v2 for Android<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developers.google.com\/ml-kit\/language\/identification\/android\" target=\"_blank\" rel=\"noopener\">ML Kit Language Identification<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developers.google.com\/ml-kit\/language\/translation\/android\" target=\"_blank\" rel=\"noopener\">ML Kit On-Device Translation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/media\/camera\/camerax\" target=\"_blank\" rel=\"noopener\">CameraX Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/media\/camera\/camerax\/analyze\" target=\"_blank\" rel=\"noopener\">CameraX Image Analysis<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/media\/camera\/camerax\/preview\" target=\"_blank\" rel=\"noopener\">CameraX Preview<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/develop\/ui\/compose\" target=\"_blank\" rel=\"noopener\">Jetpack Compose<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/training\/permissions\/requesting\" target=\"_blank\" rel=\"noopener\">Android Runtime Permissions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.android.com\/topic\/architecture\" target=\"_blank\" rel=\"noopener\">Android Architecture Guide<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Published by Moiks Developer Blog \u2014 Practical tutorials covering Android, Kotlin, Jetpack Compose, Flutter, React Native, SwiftUI, and modern software engineering.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn to build a real-time Android camera OCR and translation app using CameraX, Google ML Kit Text Recognition, Kotlin, and Jetpack Compose. Recognize printed text from a live camera preview, display detection overlays, copy text to the clipboard, identify languages, and translate text on-device. Includes MVVM architecture, StateFlow, camera permissions, image-analysis optimization, model downloads, error handling, and production best practices.<\/p>\n","protected":false},"author":1,"featured_media":359,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[16,3,333,6,5],"tags":[255,256,170,28,26,229,257,184,234],"class_list":["post-358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced-android","category-android-development","category-android-tutorials","category-jetpack-compose","category-kotlin","tag-android-ml-kit-ocr","tag-camera-ocr","tag-camerax","tag-jetpack-compose","tag-kotlin","tag-language-identification","tag-live-text-scanner","tag-ml-kit-text-recognition","tag-on-device-translation"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"predecessor-version":[{"id":360,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/358\/revisions\/360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/359"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}