Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Recognition not working in camera rotate: #60

Closed
FrancisRR opened this issue Jul 1, 2020 · 1 comment
Closed

Text Recognition not working in camera rotate: #60

FrancisRR opened this issue Jul 1, 2020 · 1 comment

Comments

@FrancisRR
Copy link

Photo captured in landscape mode that time recognizer not provided any output(actually text recognizer only working in camera zero angle remaining three angle not working 90,180,270) but portrait mode it's working fine how to solve this issue. please provide the solution in as soon as possible.

I mention my complete code on below

private fun callGallery() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(intent, "Select gallery"),
AppConstant.GALLERY_REQUEST_CODE
)
}

private fun callCamera() {
    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (intent.resolveActivity(packageManager) != null) {
        val content = ContentValues()
        content.put(MediaStore.Images.Media.TITLE, "New pictures")
        content.put(MediaStore.Images.Media.DESCRIPTION, "From camera")
        uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, content)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
        startActivityForResult(intent, AppConstant.CAMERA_REQUEST_CODE)
    } else {
        UiUtils.showErrorLog(TAG, "Camera not available")
    }

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == AppConstant.GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        uri = data!!.data
        imageFromPath(this, uri!!)
        ivActualIcon.setImageURI(uri)
    } else if (requestCode == AppConstant.CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        imageFromPath(this, uri!!)
        ivActualIcon.setImageURI(uri)
    } else {
        UiUtils.showErrorLog(TAG, "result empty")
    }
}

private fun imageFromPath(context: Context, uri: Uri) {
    // [START image_from_path]
    val image: InputImage
    try {
        image = InputImage.fromFilePath(context, uri)
        recognizeText(image)
    } catch (e: IOException) {
        e.printStackTrace()
    }
}



private fun recognizeText(image: InputImage) {
    val recognizer = TextRecognition.getClient()
    val result = recognizer.process(image)
        .addOnSuccessListener { visionText: Text ->
            processTextBlock(visionText)
            UiUtils.showErrorLog(TAG, "Success")
        }
        .addOnFailureListener { e ->
            UiUtils.showErrorLog(TAG, "${e.message}")
        }
}

private fun processTextBlock(result: Text) {
    val resultText = result.text
    var lineText: String = ""
    for (block in result.textBlocks) {
        val blockText = block.text
        UiUtils.showErrorLog("Block text", "$blockText")
        for (line in block.lines) {
            lineText = lineText + "\n" + line.text
            UiUtils.showErrorLog("Line text", "$lineText")
        }
    }

}
@FrancisRR FrancisRR changed the title Text Recognition not working in default camera rotate: Text Recognition not working in camera rotate: Jul 1, 2020
@swiyu
Copy link
Collaborator

swiyu commented Jul 18, 2020

From the code snippet, you are using InputImage.fromFilePath(context, uri) to read the image. For this API, ML Kit uses the file exif tag to read the image orientation. See here: https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html

If the exif information is missed in the file, the API assume the image is unrotated. If InputImage.fromFilePath(context, uri) does not work for you, you could use ``InputImage.fromBitmap()` here.

Basically, read the Uri file as a bitmap before sending to InputImage. With this API, you could provide the orientation information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants