question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

android.media.image passing in FirebaseVisionImage.fromMediaImage gives java.nio.BufferUnderflowException

See original GitHub issue
it.setAnalyzer(cameraExecutor, LuminosityAnalyzer { luma, image ->
getCardDetails(image,rotation)
})

private fun getCardDetails(imageProxy: ImageProxy, rotation: Int) {

    val mediaImage = imageProxy.image

    val planes = mediaImage!!.planes
    if (planes.size >= 3) {

        val image = mediaImage?.let { FirebaseVisionImage.fromMediaImage(it, getRotationConstant(rotation)) }
        //val image = mediaImage?.let { FirebaseVisionImage.fromByteArray(dataYUV, metadata) }
        val firebaseVisionTextDetector = FirebaseVision.getInstance().onDeviceTextRecognizer

        if (image != null) {
            firebaseVisionTextDetector.processImage(image)
                    .addOnSuccessListener {
                        val words = it.text.split("\n")
                        Toast.makeText(context, words.get(0), Toast.LENGTH_LONG).show()

                    }
                    .addOnFailureListener {
                        Toast.makeText(context, "Sorry, something went wrong!", Toast.LENGTH_SHORT).show()
                    }

        }
    }

}

private fun getRotationConstant(rotation: Int): Int {
return when (rotation) {
90 -> FirebaseVisionImageMetadata.ROTATION_90
180 -> FirebaseVisionImageMetadata.ROTATION_180
270 -> FirebaseVisionImageMetadata.ROTATION_270
else -> FirebaseVisionImageMetadata.ROTATION_0
}
}

// Error

Process: com.demo, PID: 24383
java.nio.BufferUnderflowException
at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:240)
at com.google.android.gms.internal.firebase_ml.zzsc.zza(com.google.firebase:firebase-ml-vision@@24.0.3:42)
at com.google.firebase.ml.vision.common.FirebaseVisionImage.fromMediaImage(com.google.firebase:firebase-ml-vision@@24.0.3:20)
at com.demo.ui.gift_shop.fragment.ScanPayFragment.getCardDetails(ScanPayFragment.kt:529)
at com.demo.ui.gift_shop.fragment.ScanPayFragment.access$getCardDetails(ScanPayFragment.kt:54)
at com.demo.ui.gift_shop.fragment.ScanPayFragment$bindCameraUseCases$1$$special$$inlined$also$lambda$1.invoke(ScanPayFragment.kt:271)
at com.demo.ui.gift_shop.fragment.ScanPayFragment$bindCameraUseCases$1$$special$$inlined$also$lambda$1.invoke(ScanPayFragment.kt:54)
at com.demo.ui.gift_shop.fragment.ScanPayFragment$LuminosityAnalyzer.analyze(ScanPayFragment.kt:383)
at androidx.camera.core.ImageAnalysisAbstractAnalyzer.lambda$analyzeImage$0$ImageAnalysisAbstractAnalyzer(ImageAnalysisAbstractAnalyzer.java:87)
at androidx.camera.core.-$$Lambda$ImageAnalysisAbstractAnalyzer$V6Gtux2-h8spda8vf4wFnIZntYI.run(Unknown Source:8)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cs-googlercommented, May 19, 2020

Try to call rewind() for each media.Image.getPlanes[i].getBuffer() in your code:

private fun getCardDetails(imageProxy: ImageProxy, rotation: Int) {

val mediaImage = imageProxy.image

val planes = mediaImage!!.planes
if (planes.size >= 3) {
   // Reset buffer position for each plane's buffer. 
   for (plane in planes) {
       plane.getBuffer().rewind()
    }
    val image = mediaImage?.let { FirebaseVisionImage.fromMediaImage(it, getRotationConstant(rotation)) }
    val firebaseVisionTextDetector = FirebaseVision.getInstance().onDeviceTextRecognizer

    if (image != null) {
        firebaseVisionTextDetector.processImage(image)
                .addOnSuccessListener {
                    val words = it.text.split("\n")
                    Toast.makeText(context, words.get(0), Toast.LENGTH_LONG).show()

                }
                .addOnFailureListener {
                    Toast.makeText(context, "Sorry, something went wrong!", Toast.LENGTH_SHORT).show()
                }
    }
}

}

0reactions
maxwellnewagecommented, Jul 3, 2020

Try to call rewind() for each media.Image.getPlanes[i].getBuffer() in your code:

private fun getCardDetails(imageProxy: ImageProxy, rotation: Int) {

val mediaImage = imageProxy.image

val planes = mediaImage!!.planes
if (planes.size >= 3) {
   // Reset buffer position for each plane's buffer. 
   for (plane in planes) {
       plane.getBuffer().rewind()
    }
    val image = mediaImage?.let { FirebaseVisionImage.fromMediaImage(it, getRotationConstant(rotation)) }
    val firebaseVisionTextDetector = FirebaseVision.getInstance().onDeviceTextRecognizer

    if (image != null) {
        firebaseVisionTextDetector.processImage(image)
                .addOnSuccessListener {
                    val words = it.text.split("\n")
                    Toast.makeText(context, words.get(0), Toast.LENGTH_LONG).show()

                }
                .addOnFailureListener {
                    Toast.makeText(context, "Sorry, something went wrong!", Toast.LENGTH_SHORT).show()
                }
    }
}

}

This works for me, but I don’t understand why we should do this, and if is this apply also to the rest of detectors (barcode, ocr).

Read more comments on GitHub >

github_iconTop Results From Across the Web

android.media.image passing in FirebaseVisionImage ...
android.media.image passing in FirebaseVisionImage.fromMediaImage gives java.nio.BufferUnderflowException #1527.
Read more >
Why FirebaseVisionImage.fromMediaImage() produces ...
I was able to resolve the issue by switching to mlkit. First update the app/build.gradle file to use mlkit instead of firebase:
Read more >
Label Images with Firebase ML on Android
To label objects in an image, pass the FirebaseVisionImage object to the FirebaseVisionImageLabeler 's processImage method. First, get an instance of ...
Read more >
Card Scanner on Android Using Firebase's ML Kit and CameraX
Image object, such as when capturing an image from a device's camera, pass the media.Image object and the image's rotation to FirebaseVisionImage.fromMediaImage ......
Read more >
Exploring Firebase MLKit on Android: Landmark Detection ...
With this comes the landmark recognition feature, giving us the ... feature we can pass an instance of an image to the Firebase...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found