使用libyuv将UYVY格式转换成NV21和RGBA

1.UYVY转换成NV21

void uyvyToNv21AndScale(uint8_t *src, int srcWidth, int srcHeight,
        uint8_t *dstY, uint8_t *dstVU,int dstWidth, int dstHeight)
{
  uint8_t* mCamera3BufferTemp = new unsigned char[srcWidth* srcHeight* 4];
  uint8_t* mCamera3ScaledBuffer = new unsigned char[srcWidth* srcHeight* 4];
  uint8_t *y = mCamera3BufferTemp;
  uint8_t *u = mCamera3BufferTemp + srcWidth * srcHeight;
  uint8_t *v = mCamera3BufferTemp + srcWidth * srcHeight * 5 / 4;

  libyuv::UYVYToI420(src, srcWidth << 1, y, srcWidth, u, srcWidth >> 1, v, srcWidth >> 1, 
      srcWidth, srcHeight);

  libyuv::I420Scale(y, srcWidth, u, srcWidth >> 1, v, srcWidth >> 1, srcWidth, srcHeight,
      mCamera3ScaledBuffer, dstWidth, mCamera3ScaledBuffer + dstWidth * dstHeight, 
      dstWidth >> 1,mCamera3ScaledBuffer + (dstWidth * dstHeight) * 5 / 4, dstWidth >> 1, 
      dstWidth, dstHeight, libyuv::FilterMode::kFilterNone);
  
  //生成的dstVU数据是VU分量交叉的,V分量在前,U分量在后
  libyuv::I420ToNV21(mCamera3ScaledBuffer, dstWidth,
        mCamera3ScaledBuffer + dstWidth * dstHeight,
        dstWidth >> 1, mCamera3ScaledBuffer + (dstWidth * dstHeight) * 5 / 4,
        dstWidth >> 1, dstY, dstWidth, dstVU, dstWidth, dstWidth, dstHeight);
}

2.UYVY转换成RGBA

void uyvyToRgbaAndScale(uint8_t *src, int srcWidth, int srcHeight,
        uint8_t *dst, int dstWidth, int dstHeight)
{
  uint8_t * mCamera3BufferTemp = new unsigned char[srcWidth* srcHeight* 4];
  uint8_t * mCamera3ScaledBuffer = new unsigned char[srcWidth* srcHeight* 4];
  uint8_t *y = mCamera3BufferTemp;
  uint8_t *u = mCamera3BufferTemp + srcWidth * srcHeight;
  uint8_t *v = mCamera3BufferTemp + (srcWidth * srcHeight) * 5 / 4;

  libyuv::UYVYToI420(src, srcWidth << 1, y, srcWidth, u, srcWidth >> 1, v, srcWidth >> 1, 
      srcWidth, srcHeight);

  libyuv::I420Scale(y, srcWidth, u, srcWidth >> 1, v, srcWidth >> 1, srcWidth, srcHeight,
      mCamera3ScaledBuffer, dstWidth, mCamera3ScaledBuffer + dstWidth * dstHeight, 
      dstWidth >> 1, + (dstWidth * dstHeight) * 5 / 4, dstWidth >> 1, dstWidth, dstHeight,
      libyuv::FilterMode::kFilterNone);

  //需要使用I420ToABGR接口而非I420ToRGBA接口,否则颜色显示有问题
  libyuv::I420ToABGR(mCamera3ScaledBuffer, dstWidth, mCamera3ScaledBuffer + dstWidth *       
      dstHeight, dstWidth >> 1, mCamera3ScaledBuffer + dstWidth * dstHeight * 5 / 4, 
      dstWidth >> 1, dst, dstWidth << 2, dstWidth, dstHeight);
}

你可能感兴趣的:(Camera,camera,算法)