camera-samples icon indicating copy to clipboard operation
camera-samples copied to clipboard

java.lang.IndexOutOfBoundsException, because of YuvToRgbConverter.yuvToRgb

Open viktor-idenfy opened this issue 4 years ago • 3 comments

Hello, we use cameraX for the production and started to notice increasing crashes, because of the ** yuvToRgb** (specifically imageToByteArray(image: Image, outputBuffer: ByteArray)) YuvToRgbConverter starting camera x core beta version ~20.

We use this class for converting Image from cameraX to Bitmap and it is an official recommendation from Google themselves.

Perpahs you should update the code because it is not stable anymore? Or please provide another solution for converting Image to the Bitmap.

Here is the list of devices with the crash: CUBOT X19 Samsung Galaxy S9+ HUAWEI Y7 2019 BQru-5514G

The full crash log:

Fatal Exception: java.lang.IndexOutOfBoundsException off=1382400, len=1920 out of bounds (size=1382407) java.nio.Buffer.checkBounds (Buffer.java:559) java.nio.DirectByteBuffer.get (DirectByteBuffer.java:234) com.idenfy.idenfySdk.documentscamerasession.ui.utils.CustomBitmapConverter.imageToByteArray (CustomBitmapConverter.java:117) com.idenfy.idenfySdk.documentscamerasession.ui.utils.CustomBitmapConverter.convertYuvToBitmap (CustomBitmapConverter.java:41) com.idenfy.idenfySdk.documentscamerasession.ui.utils.CustomDocumentImageProcessor.analyze (CustomDocumentImageProcessor.java:76) androidx.camera.core.ImageAnalysis.lambda$setAnalyzer$1 (ImageAnalysis.java:367) java.lang.Thread.run (Thread.java:784)

viktor-idenfy avatar Apr 18 '21 08:04 viktor-idenfy

I believe there is a bug in YuvToRgbConverter.needCreateAllocations function. The part bytes.size == yuvBuffer.buffer.capacity() should actually be bytes.size != yuvBuffer.buffer.capacity()

Just to note, there are two YuvToRgbConverter.kt files in this repo, both of them have the same issue.

DanPetras avatar Apr 19 '24 09:04 DanPetras

If you are using CameraX ImageAnalysis, you could use this API, it's built on top of libyuv instead of renderscript. https://developer.android.com/reference/kotlin/androidx/camera/core/ImageAnalysis.Builder#setOutputImageFormat(int) https://medium.com/androiddevelopers/convert-yuv-to-rgb-for-camerax-imageanalysis-6c627f3a0292

kailianc avatar Apr 19 '24 15:04 kailianc

I'm forced to use camera2 api because of AR core.

DanPetras avatar Apr 19 '24 17:04 DanPetras