Home  >  Q&A  >  body text

java - Android surfaceview获取的YUV数据如何进行YUV转RGB转Bitmap转BGRA?

用surfaceview获取到YUV类型的byte[]数据如何进行YUV转RGB转Bitmap转BGRA的过程?
希望有大神贴点代码,或者给一些相关资料借鉴下。

怪我咯怪我咯2764 days ago833

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-18 10:51:03

    I don’t know if there is a better way in Android.
    If C++ is used for processing, it is recommended to find a library to do it, such as ffmpeg.

    1. YUV to RGB

    The formula for converting the two to each other is as follows
    RGB to YUV

        Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
        Cr = V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
        Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128

    YUV to RGB

        B = 1.164(Y - 16)                   + 2.018(U - 128)
        G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
        R = 1.164(Y - 16) + 1.596(V - 128)

    You can write your own code to implement it here, it is not difficult.
    But YUV has a variety of sampling formats and storage methods, so what you get here byte[] is not enough, you also need to know what YUV looks like.
    So it is recommended that you directly find a way to obtain RGB pixel data.

    2. RGB to Bitmap

    This RGB also has the same problem, it may be RGB565/RGB555/RGB24/RGB32 etc. You still have to know what it looks like.
    General image libraries have implementations related to Bitmap operations, which can directly set pixel data. I don’t know about it here. For example, QBitmap::fromData

    in Qt

    If you want to do it yourself, you have to write the file header yourself. For relevant information, please refer to http://blog.csdn.net/o_sun_o/...

    3. Bitmap to BGRA

    If you do this yourself, just take the pixels and save them yourself. All the added Alpha channels are 0.

    reply
    0
  • Cancelreply