android.media.image passing in FirebaseVisionImage.fromMediaImage gives java.nio.BufferUnderflowException
See original GitHub issueit.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:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try to call rewind() for each media.Image.getPlanes[i].getBuffer() in your code:
private fun getCardDetails(imageProxy: ImageProxy, rotation: Int) {
}
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).