如何从图像的特定块中提取文本?[ML-Kit]

问题描述 投票:-1回答:1

我很绝望。我有16个TextViews。目标是拍摄一张处方的图片。我想要的是为每个TextView提取正确的文本。这是我最后的问题。

下面是拍照,创建文件,识别文本并将其放入TextView的代码。

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Ensure that there's a camera activity to handle the intent
        takePictureIntent.resolveActivity(packageManager)?.also {
            // Create the File where the photo should go

            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                // Error occurred while creating the File
                null
            }
            // Continue only if the File was successfully created
            photoFile?.also {
                photoURI = FileProvider.getUriForFile(
                    this,
                    "com.example.android.fileprovider2",
                    it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
                /*if (REQUEST_TAKE_PHOTO == 1)
                {
                    return imageView.setImageURI(photoURI)
                }*/

            }
        }
    }
}



@Throws(IOException::class)
private fun createImageFile(): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    return File.createTempFile(
        "JPEG_${timeStamp}_", /* prefix */
        ".jpg", /* suffix */
        storageDir /* directory */
    ).apply {
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = absolutePath

    }
}











fun startRecognizing(v: View) {
    if (imageView.drawable != null) {
        editText.setText("")
        v.isEnabled = false
        val bitmap = (imageView.drawable as BitmapDrawable).bitmap
        val image = FirebaseVisionImage.fromBitmap(bitmap)
        val detector = FirebaseVision.getInstance().onDeviceTextRecognizer


        detector.processImage(image)
            .addOnSuccessListener { firebaseVisionText ->
                v.isEnabled = true
                processResultText(firebaseVisionText)
            }
            .addOnFailureListener {
                v.isEnabled = true
                editText.setText("Failed")
            }
    } else {
        Toast.makeText(this, "Select an Image First", Toast.LENGTH_LONG).show()
    }

}

private fun String.toEditable(): Editable =  Editable.Factory.getInstance().newEditable(this)


private fun processResultText(resultText: FirebaseVisionText) {
    if (resultText.textBlocks.size == 0) {
        editText.setText("No Text Found")
        return
    }
    for (blocks in resultText.textBlocks) {
        val blockText = blocks.text
        val stringText  = resultText.text
        val stringTextSplit = stringText.lines()
        krankenkasse.text = stringTextSplit[1].toEditable()
        et2.text = stringTextSplit[4].toEditable()
        et3.text = stringTextSplit[5].toEditable()
        et4.text = stringTextSplit[6].toEditable()
        et5.text = stringTextSplit[7].toEditable()
        et6.text = stringTextSplit[8].toEditable()
        et7.text = stringTextSplit[9].toEditable()
        et8.text = stringTextSplit[11].toEditable()
        editText.append(blockText + "\n")
    }
}

如果你有问题。请问。

android kotlin android-camera firebase-mlkit
1个回答
2
投票

如果你想检测图像中的一些特定区域,你可以根据这些区域裁剪图像,并将裁剪后的图像发送到ML Kit中。

另一个选择是检测整个图像,但过滤掉不在区域内的文本。每个检测到的文本都有自己的边界框坐标。

https:/firebase.google.comdocsreferenceandroidcomgooglefirebasemlvisiontextFirebaseVisionText.TextBlock#getBoundingBox()

© www.soinside.com 2019 - 2024. All rights reserved.