0
     package com.hamza.ocr

     import android.content.Intent
     import android.os.Bundle
     import android.util.Log
     import android.view.View
     import android.widget.Button
     import android.widget.ImageView
     import android.widget.TextView
     import androidx.appcompat.app.AppCompatActivity
     import com.google.firebase.ml.vision.common.FirebaseVisionImage
     import java.io.IOException
     import com.google.firebase.ml.vision.FirebaseVision

     import com.google.firebase.ml.vision.text.FirebaseVisionCloudTextRecognizerOptions
     import java.util.*


  class MainActivity : AppCompatActivity() {

       lateinit var Image:ImageView
       lateinit var choose: Button
       lateinit var resultTv:TextView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
Image = findViewById(R.id.imageView)
    choose = findViewById(R.id.button)
    resultTv = findViewById(R.id.textTv)
    choose.setOnClickListener(View.OnClickListener {
        var i =Intent()
        i.setType("image/*")
        i.setAction(Intent.ACTION_GET_CONTENT)
        startActivityForResult(Intent.createChooser(i,"Select Images"),121)

    })
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (resultCode==121){
        Image.setImageURI(data?.data)
    }

    val image: FirebaseVisionImage
    try {
        image = FirebaseVisionImage.fromFilePath(applicationContext, data?.data!!)

        val options = FirebaseVisionCloudTextRecognizerOptions.Builder()
            .setLanguageHints(Arrays.asList("en", "hi"))
            .build()
        val textRecognizer = FirebaseVision.getInstance()
            .onDeviceTextRecognizer

        textRecognizer.processImage(image)
            .addOnSuccessListener {
                // Task completed successfully
                // ...
                resultTv.setText(it.text)
                Image.setImageURI(data.data)
                Log.d("On Success","$it")
            }
            .addOnFailureListener {
                // Task failed with an exception
                // ...
            }
     }  catch (e: IOException) {
         e.printStackTrace()
    }
 }

}

我想进行文本识别我正在尝试使用此代码从图像中获取文本,但它只能识别英语,但我想从拉丁语中获取文本。我尽力而为,但找不到最佳解决方案。因此,如果有人对 OCR 有所了解,请通过查看我的代码来指导我。这个项目只是为了学习而创建的

4

0 回答 0