Convert Android camera2 api YUV_420_888 to RGB

后端 未结 7 881
暗喜
暗喜 2020-12-14 02:56

I am writing an app that takes the camera feed, converts it to rgb, in order to do some processing.

It works fine on the old camera implementation which uses NV21 Y

相关标签:
7条回答
  • 2020-12-14 03:32

    So I ran into the exact same problem, where I had a code that took the old YUV_420_SP format byte[] data from OnPreviewFrame() and converted it to RGB.

    They key here is that the 'old' data in the byte[] is as YYYYYY...CrCbCrCbCrCb, and the 'new' data from the Camera2 API is divided into 3 planes: 0=Y, 1=Cb, 2=Cr., from where you can obtain each's byte[]s. So, all you need to do is reorder the new data as a single array that matches the 'old' format, which you can pass to your existing toRGB() functions:

    Image.Plane[] planes = image.getPlanes(); // in YUV220_888 format
    int acc = 0, i;
    ByteBuffer[] buff = new ByteBuffer[planes.length];
    
    for (i = 0; i < planes.length; i++) {
       buff[i] = planes[i].getBuffer();
       acc += buff[i].capacity();
    }
    byte[] data = new byte[acc],
       tmpCb = new byte[buff[1].capacity()] , tmpCr = new byte[buff[2].capacity()];
    
    buff[0].get(data, 0, buff[0].capacity()); // Y
    acc = buff[0].capacity();
    
    buff[2].get(tmpCr, 0, buff[2].capacity()); // Cr
    buff[1].get(tmpCb, 0, buff[1].capacity()); // Cb
    
    for (i=0; i<tmpCb.length; i++) {
        data[acc] = tmpCr[i];
        data[acc + 1] = tmpCb[i];
        acc++;
    }
    

    ..and now data[] is formatted just as the old YUV_420_SP.

    (hope that it helps someone, despite the years passed..)

    0 讨论(0)
提交回复
热议问题