YUV to Bitmap

Completed

Comments

13 comments

  • DJI Developer Support
    The BitmapFactory.decodeByteArray cannot directly accept YUV data. You can try using bitmap.copyPixelsFromBuffer to store YUV data into the bitmap. It is important to note that the YUV data format needs to be RGBA_8888, while the YUV data format provided in version 5.5 is YUV_420.
    0
    Comment actions Permalink
  • MHM

    Yes, y also tried decodeByteArray with same result. I guess it is because data format.

    There is any way to convert from YUV_420 to RGBA_8888?

    Thanks in advance.

    0
    Comment actions Permalink
  • DJI Developer Support
    I'm sorry, but I don't have the reference code to convert YUV_420 to RGBA_8888. However, it is a common data format conversion, and you can find methods online.
    0
    Comment actions Permalink
  • MHM

    Thanks! I will check it.

    Best regards!

    0
    Comment actions Permalink
  • DJI Developer Support
    I hope this has been helpful to you.
    0
    Comment actions Permalink
  • MHM

    Yes, of course :)

    Now checking the format conversion and trying to upgrade the SDK to get the image directly in RGBA_8888 format. I think newer versions gives this format directly from the callback. That would be easier to copy to a bitmap.

    Thank you very much!

    0
    Comment actions Permalink
  • DJI Developer Support
    Yes, version 5.8 has added the function to select the YUV format, which supports directly obtaining images in the RGBA_8888 format.
    0
    Comment actions Permalink
  • MHM

    Hello again!

    I finally update to SDK v 5.8.0 :)

    Now I have the problem that frames (frameData) is null. The code I am using is as follows:

    private fun getFrame() {
    MediaDataCenter.getInstance().cameraStreamManager.addFrameListener(
    ComponentIndexType.LEFT_OR_MAIN,
    ICameraStreamManager.FrameFormat.RGBA_8888,
    object : ICameraStreamManager.CameraFrameListener {
    override fun onFrame(
    frameData: ByteArray,
    offset: Int,
    length: Int,
    width: Int,
    height: Int,
    format: ICameraStreamManager.FrameFormat
    ) {
    if (frameData != null) {
    val bitmap: Bitmap = BitmapFactory.decodeByteArray(
    frameData, offset, length
    )
    }
    }

    }
    )
    }

    and code fails when calling decodeByteArray.

    I cannot find where the problem is and I would really appreciate if you can guide me to get the RGBA_8888 and copy to a bitmap.

    I am using Mavic 3M and MSDK 5.8.0.

    Thanks in advance.

    Best regard.

    0
    Comment actions Permalink
  • DJI Developer Support
    “The BitmapFactory.decodeByteArray cannot directly accept YUV data. You can try using bitmap.copyPixelsFromBuffer to store YUV data into the bitmap.” We have previously discussed the method of converting YUV to a bitmap. You can refer to the above content. I have not encountered a situation where addFrameListener pushes null. Do you have a way to consistently trigger this issue? Can you provide us with a method?
    0
    Comment actions Permalink
  • MHM

    Hello,

    sorry, I wasn't initialising the bitmap so it was always null :S Now it's working as expected :)

    Thank you very much!

    Best regards.

    1
    Comment actions Permalink
  • DJI Developer Support
    I'm glad to help you.
    0
    Comment actions Permalink
  • Angel Gonzalez Negrete

    Hi MHM, I'm facing same problem, but I don't know where and how to initialize the bitmap, could you please help me???

    I edit the LiveFragment.kt trying to get a frame from streaming but allways get an empty or blank bitmap

    Best regards from Mexico

    0
    Comment actions Permalink
  • MHM

    Hi!

    Yes, at frame callback, just before bitmap.copyPixelsFromBuffer, initilize bitmap as follows:

    val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)

    Hope this line solves your issue.

    Best regards.

    0
    Comment actions Permalink

Please sign in to leave a comment.