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

时间:2020-04-16 14:47:54

标签: android kotlin android-camera firebase-mlkit

我很绝望。我有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")
    }
}

如果您有任何疑问。请问。

1 个答案:

答案 0 :(得分:2)

如果要检测图像中的某些特定区域,可以根据区域裁剪图像,然后将裁剪后的图像发送到ML Kit中。

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

here