Kotlin Android Program (QCR) Application Codes That Read Text in Photos
Kotlin Android Program (QCR) Application Codes That Read Text in Photos
To write a program to convert text from a photo to text (Optical Character Recognition) with Kotlin, libraries such as Google's **ML Kit** or **Tesseract OCR** are typically used. In this example, we will develop a simple OCR application using Google ML Kit. This application allows the user to select a photo and translate the text in the photo.
---
1. Project Setup
- Create a new project in Android Studio.
- Select the `Empty Activity` template.
2. Adding Necessary Dependencies
- Add ML Kit dependencies to `build.gradle` file:
```gradle
dependencies {
implementation ( "com.google.android.gms:play-services-mlkit-text-recognition:19.0.0" )
implementation ( "androidx.activity:activity-ktx:1.7.0" )
implementation ( "androidx.fragment:fragment-ktx:1.6.0" )
}
```
---
3. Creating XML Layout File
- Open the `res/layout/activity_main.xml` file and design a UI as follows:
```xml
<LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android"
xmlns: app ="http://schemas.android.com/apk/res-auto"
xmlns: tools ="http://schemas.android.com/tools"
android :id ="@+id/main" android :orientation ="vertical"
android :layout_width ="match_parent" android :layout_height ="match_parent" tools :context =".MainActivity" > <Button android :id ="@+id/btnSelectImage" android :layout_width ="match_parent" android :layout_height ="wrap_content" android :text ="Select Photo" /> <ImageView android :id ="@+id/ivSelectedImage" android :layout_width ="match_parent" android :layout_height ="200dp" android :scaleType ="centerCrop" android :contentDescription ="Selected Photo" /> <TextView android :id ="@+id/tvRecognizedText" android :layout_width ="match_parent" android :layout_height ="wrap_content" android :text ="Recognized Text" android :padding ="8dp" android :textSize ="16sp" /> </LinearLayout>
---
### 4. OCR Process with Kotlin Codes
- Open the file `MainActivity.kt` and code it as follows:
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var btnSelectImage : Button
private lateinit var ivSelectedImage : ImageView
private lateinit var tvRecognizedText : TextView
private val selectImageLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result. resultCode == RESULT_OK )
{ val data: Intent? =result. data val imageUri: Uri? =data?. data if (imageUri != null ) { ivSelectedImage .setImageURI(imageUri) processImageFromUri( imageUri ) } } } override fun onCreate (savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout. activity_main ) btnSelectImage = findViewById(R.id. btnSelectImage ) ivSelectedImage = findViewById(R.id. ivSelectedImage ) tvRecognizedText = findViewById(R.id. tvRecognizedText ) btnSelectImage .setOnClickListener { openImagePicker() } } private fun openImagePicker () { val intent = Intent(Intent. ACTION_PICK , MediaStore.Images.Media. EXTERNAL_CONTENT_URI )
selectImageLauncher .launch(intent)
}
private fun processImageFromUri (imageUri: Uri) {
try {
val bitmap: Bitmap = MediaStore.Images.Media.getBitmap( contentResolver , imageUri)
recognizeTextFromImage(bitmap)
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText( this , " Error loading photo ! " , Toast .
.addOnSuccessListener { visionText -> tvRecognizedText . text = visionText. text } .addOnFailureListener { e -> e.printStackTrace() Toast.makeText( this , "Text recognition failed!" , Toast. LENGTH_SHORT ).show() } } }
```
---
### 5. Permissions
- To be able to select photos from the gallery, connect to the internet when needed, and use memory, add the following permissions to the `AndroidManifest.xml` file:
```xml
<uses-permission android :name ="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android :name ="android.permission.CAMERA" tools
:ignore ="PermissionImpliesUnsupportedChromeOsHardware" /> <uses-permission android :name ="android.permission.MANAGE_EXTERNAL_STORAGE" /> <uses-permission android :name ="android.permission.INTERNET" /> <uses-permission android :name ="android.permission.READ_MEDIA_IMAGES" />
### 6. Run the Application
- Run the application and select a photo. The text in the photo will be displayed in the `TextView`.
---
### Explanations:
1. **Selecting Photo**: Photo is selected from the gallery using the `ActivityResultContracts` API.
2. **Text Recognition**: Text in the photo is recognized using Google ML Kit's `TextRecognition` API.
3. **Error Management**: `Try-catch` blocks and `Toast` messages are used for errors that may occur during photo upload or text recognition.
This example is a basic OCR app. You can explore other features of ML Kit to add more advanced features (for example, live text recognition with camera).
Comments
Post a Comment