I am trying to implement Google ML OCR Tesseract in my android project and i am getting some random text from the picture I have clicked. Following is my code:
类 MainActivity : ComponentActivity() { 私有 Lateinit var tessBaseAPI:TessBaseAPI private Lateinit var imageView: ImageView private Lateinit var textView: TextView 私有val requestImageCapture = 1 私人 val takePicture = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { 结果 ->
if (result.resultCode == RESULT_OK) {
val resultInfo = result.data?.extras?.get("data")
val imageBitmap = result.data?.extras?.get("data") as Bitmap?
imageBitmap?.let {
imageView.setImageBitmap(it)
recognizeText(it)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageView = findViewById(R.id.imageView)
textView = findViewById(R.id.textView)
val captureButton: Button = findViewById(R.id.captureButton)
copyLanguageDataToInternalStorage()
initTesseract()
captureButton.setOnClickListener {
dispatchTakePictureIntent()
}
}
private fun initTesseract() {
val datapath = "${filesDir}/tesseract/"
val dir = File(datapath)
if (!dir.exists()) {
dir.mkdirs()
}
val datafilepath = "${datapath}/tessdata/"
val filesDir = File(datafilepath)
if (!filesDir.exists()) {
filesDir.mkdirs()
}
Log.d("dataPath", "data: $datapath")
tessBaseAPI = TessBaseAPI()
tessBaseAPI.init(datapath, "eng")
}
private fun copyLanguageDataToInternalStorage() {
Log.d("test", "testing")
try {
val assetManager = assets
val inputStream = assetManager.open("tesseract/tessdata/eng.traineddata")
val outputStream =
FileOutputStream(File(filesDir, "tesseract/tessdata/eng.traineddata"))
inputStream.use { input ->
outputStream.use { output ->
input.copyTo(output)
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(packageManager) != null) {
takePicture.launch(takePictureIntent)
}
}
private fun recognizeText(bitmap: Bitmap) {
tessBaseAPI.setImage(bitmap)
val recognizedText = tessBaseAPI.getUTF8Text()
textView.text = recognizedText
}
override fun onDestroy() {
super.onDestroy()
tessBaseAPI.end()
}
}
and i am getting this as text
`m”;-.v__m-~_;;;;;;§'F"WWW If?`
Can Someone tell me where i am doing wrong in getting text from capturing a picture from the phone
您没有描述如何将 Tesseract 包含到您的项目中(通过包装器还是直接?),但 tesseract
setImage
需要 Leptonica PIX 对象进行识别,而不是位图。请检查这是否不是您问题的原因(例如通过 tesseract 变量tessedit_write_images
)。
我不是 Android 开发人员,但是
initTesseract
函数的逻辑很可疑 - 它只是创建一个目录结构而不调用 copyLanguageDataToInternalStorage
- 这可能会导致你尝试在没有任何语言文件的情况下初始化 tesseract API,因此 API 会不被启动...