当前位置:   article > 正文

图像处理:YUV_420_888介绍及YUV420转RGBA

yuv420

刚开始做camera相关的开发时,对YUV_420_888这个格式很懵,不同平台的机型,从Image中转换出RGB的方法好像也不同,在终于初步了解YUV420格式后,写下本文,权当笔记总结。
YUV420转RGBA系列共三篇:

本文是其中的第一篇。

1. YUV简介

在了解YUV_420_888之前,我们先来了解一下YUV。我们知道,RGB是一种颜色编码方法,一个像素分别以R、G、B三个分量来表示。YUV也是一种颜色编码方法,一个像素分别以Y、U、V三个分量来表示。Y表示明亮度(Luminance、Luma),U和V则是色度、浓度(Chrominance、Chroma)。

YUV发明于彩色电视与黑白电视的过渡时期。黑白电视使用的黑白图像只有Y(Luma,Luminance)分量,也就是灰阶值。到了彩色电视规格的制定,是以YUV的格式来处理彩色电视图像,把UV视作表示彩度的C(Chrominance或Chroma),如果忽略C信号,那么剩下的Y(Luma)信号就跟之前的黑白电视频号相同,这样一来便解决了彩色电视机与黑白电视机的兼容问题。YUV在对照片或影片编码时,考虑到人类的感知能力,允许降低色度的采样。根据UV的采样不同,YUV分为多种格式:

  • YUV444:每4个Y,配上4个U,4个V
  • YUV422:每4个Y,配上2个U,2个V
  • YUV420:每4个Y,配上1个U,1个V

降低UV的采样后,传输时只需占用极少的带宽。(本节内容来自维基百科:https://zh.wikipedia.org/wiki/YUV)

2. YUV的紧缩格式(packed)和平面格式(planar)

YUV根据Y、U、V存储方式的不同,可以分成两个格式:

  • 紧缩格式(packed):每个像素点的Y、U、V连续存储,Y1U1V1...YnUnVn。
  • 平面格式(planar):先存储所有像素点的Y分量,再存储所有像素点的UV分量,Y和UV分别连续存储在不同矩阵当中。

平面格式(planar)又分为:

  • 平面格式(planar):先存储所有像素的Y,再存储所有像素点U或者V,最后存储V或者U。其中U、V分别连续存储:Y1...Yn U1...Un V1...Vn 或者 Y1...Yn V1...Vn U1...Un。
  • 半平面格式(semi-planar):先存储所有像素的Y,再存储所有像素点UV或者VU。其中U、V交替存储:Y1...Yn U1V1...UnVn 或者 Y1...Yn V1U1...VnUn。

采样方式采用YUV420、存储方式采用平面格式(planar)称为YUV420P。YUV420P根据U和V顺序不同又分为:

  • I420: Y1...Y4n U1...Un V1...Vn (例如:YYYYYYYYUUVV)
  • YV12:Y1...Y4n V1...Vn U1...Un (例如:YYYYYYYYVVUU)

采样方式采用YUV420、存储方式采用半平面格式(semi-planar)称为YUV420SP,YUV420SP根据U和V顺序不同又分为:

  • NV12: Y1...Y4n U1V1...UnVn (例如:YYYYYYYYUVUV)
  • NV21:Y1...Y4n V1U1...VnUn (例如:YYYYYYYYVUVU)

3. Android YUV_420_888

3.1 YUV_420_888API描述

我们先来看Android API中对YUV_420_888的介绍(https://developer.android.google.cn/reference/android/graphics/ImageFormat?hl=en#YUV_420_888):

Multi-plane Android YUV 420 format

This format is a generic YCbCr format, capable of describing any 4:2:0 chroma-subsampled planar or semiplanar buffer (but not fully interleaved), with 8 bits per color sample.

Images in this format are always represented by three separate buffers of data, one for each color plane. Additional information always accompanies the buffers, describing the row stride and the pixel stride for each plane.

The order of planes in the array returned by Image#getPlanes() is guaranteed such that plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V (Cr).

The Y-plane is guaranteed not to be interleaved with the U/V planes (in particular, pixel stride is always 1 in yPlane.getPixelStride()).

The U/V planes are guaranteed to have the same row stride and pixel stride (in particular, uPlane.getRowStride() == vPlane.getRowStride() and uPlane.getPixelStride() == vPlane.getPixelStride(); ).

Google翻译下:

多平面Android YUV 420格式

此格式是通用的YCbCr格式,能够描述任何4:2:0色度采样的平面或半平面缓冲区(但不完全交织),每个颜色样本有8位。

这种格式的图像始终由三个单独的数据缓冲区表示,每个颜色缓冲区一个。 缓冲区中始终会附带其他信息,描述每个平面的行步长和像素步长。

确保Image#getPlanes()返回的数组中平面的顺序,使得平面#0始终为Y,平面#1始终为U(Cb),平面#2始终为V(Cr)。

保证Y平面不与U / V平面交错(特别是yPlane.getPixelStride()中的像素步长始终为1)。

确保U / V平面具有相同的行步长和像素步长(尤其是uPlane.getRowStride()== vPlane.getRowStride()和uPlane.getPixelStride()== vPlane.getPixelStride();)。

3.2 YUV_420_888使用实践

根据API中的介绍,我们可以知道,YUV_420_888是可以兼容所有YUV420P和YUV420SP格式的。也就是说上面提到的I420、YV12、NV12、NV21都可以是YUV_420_888的具体实现。虽然同样是Image对象,不同平台的机型可能有不同的实现。

根据本人测试,创建ImageReader时参数format传入YUV_420_888,Image.getPlanes()后,分别获取Plane0、Plane1、Plane2的buffer:

MTK平台:

//MTK

width:3264, height:2448,

yRowStride:3264, yPixelStride:1, yPixelSize:7990272,

uRowStride:1632, uPixelStride:1, uPixelSize:1997568,

vRowStride:1632, vPixelStride:1, vPixelSize:1997568

可以看到 Y:U:V = 4:1:1,说明这是YUV420采样格式。实测:

Plane0 + Plane1 + Plane2 得到的是I420
展锐平台:

展锐平台就比较奇怪了,即使创建ImageReader时传了YUV_420_888,用的似乎还是YUV422,但这明显不符合Andoid API的设计意图。并且用的是YUV422就算了,又还缺了一个UV色度分量。

//SPRD

width:3264, height:2448,

yRowStride:3264, yPixelStride:1, yPixelSize:7990272,

uRowStride:3264, uPixelStride:2, uPixelSize:3995135, //正常是3995136

vRowStride:3264, vPixelStride:2, vPixelSize:3995135 //正常是3995136

可以看到 Y:U:V = 4:2:2,说明这是YUV422采样格式。所以展锐平台的转换有两种方法:

Plane0 + Plane1 得到NV12
Plane0 + Plane2 得到NV21

其它平台暂未测试,有兴趣的童鞋可以自行测试下。采用YUV422虽然也能正常使用,但是个人认为这个明显是不合理的,YUV422不符合YUV_420_888的API描述,并且采用YUV420处理速度、存储速度都会比YUV422更快,传输带宽占用更少。

根据 API 我们可以知道,创建ImageReader时参数format除了YUV_420_888,还可以传,YUV_422_888、YUV_444_888这样就可以得到不同YUV采样模式的图像,但根据展锐平台的异常现象,我们也可以知道这都依赖于平台实现。

4. I420、YV12、NV12、N21转RGBA

I420、YV12、NV12、N21转换时都有一些共性:

  • 每个像素有自己独立的Y分量,Y的数量与像素点数量相等。
  • 4个像素共用一个U分量和V分量。

因此,我们只要找到每个像素Y、U、V分量的对应关系就可以进行转换。

4.1 I420转RGB

i420.png

4.2 YV12转RGB

yv12.png

4.3 NV12转RGB

nv12.png

4.4 NV21转RGB

nv21.png

4.5 代码实现

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/中有相关的YUV转RGB和RGB转YUV的代码。本文也是参考其中的转换代码,分别封装了一下I420转RGBA、YV12转RGBA、NV12转RGBA、NV21转RGBA。

定义常量:
  1. const int K_MAX_CHANNEL_VALUE = 262143;
  2. const int YUV420P_I420 = 1;
  3. const int YUV420P_YV12 = 2;
  4. const int YUV420SP_NV12 = 3;
  5. const int YUV420SP_NV21 = 4;
YUV转RGBA:
  1. /*
  2. * This function come from:
  3. * https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/jni/yuv2rgb.cc
  4. */
  5. static inline int YUV2RGBA(int nY, int nU, int nV) {
  6. nY -= 16;
  7. nU -= 128;
  8. nV -= 128;
  9. if (nY < 0) nY = 0;
  10. // This is the floating point equivalent. We do the conversion in integer
  11. // because some Android devices do not have floating point in hardware.
  12. // nR = (int)(1.164 * nY + 2.018 * nU);
  13. // nG = (int)(1.164 * nY - 0.813 * nV - 0.391 * nU);
  14. // nB = (int)(1.164 * nY + 1.596 * nV);
  15. int nR = 1192 * nY + 1634 * nV;
  16. int nG = 1192 * nY - 833 * nV - 400 * nU;
  17. int nB = 1192 * nY + 2066 * nU;
  18. nR = nR > K_MAX_CHANNEL_VALUE ? K_MAX_CHANNEL_VALUE : (nR < 0 ? 0 : nR);
  19. nG = nG > K_MAX_CHANNEL_VALUE ? K_MAX_CHANNEL_VALUE : (nG < 0 ? 0 : nG);
  20. nB = nB > K_MAX_CHANNEL_VALUE ? K_MAX_CHANNEL_VALUE : (nB < 0 ? 0 : nB);
  21. nR = (nR >> 10) & 0xff;
  22. nG = (nG >> 10) & 0xff;
  23. nB = (nB >> 10) & 0xff;
  24. return 0xff000000 | (nR << 16) | (nG << 8) | nB;
  25. }
  26. /*
  27. * int order: ARGB
  28. * byte order: RGBA
  29. */
  30. static inline void rgbaIntToBytes(int rgba, unsigned char *b) {
  31. b[0] = (unsigned char) ((rgba >> 16) & 0xff); //R
  32. b[1] = (unsigned char) ((rgba >> 8) & 0xff); //G
  33. b[2] = (unsigned char) (rgba & 0xff); //B
  34. b[3] = (unsigned char) ((rgba >> 24) & 0xff); //A
  35. }
YUV420P转RGB:
  1. void YUV420PToRGBAByte(unsigned char *src, unsigned char *dst, int width, int height,
  2. int yRowStride, int uvRowStride, int uvPixelStride, int format) {
  3. if (format == YUV420P_I420 || format == YUV420P_YV12) {
  4. unsigned char *pRGBA = dst;
  5. unsigned char *pY = src;
  6. unsigned char *pU;
  7. unsigned char *pV;
  8. if (format == YUV420P_I420) {
  9. pU = src + width * height;
  10. pV = src + width * height / 4 * 5;
  11. } else {
  12. pU = src + width * height / 4 * 5;
  13. pV = src + width * height;
  14. }
  15. for (int y = 0; y < height; y++) {
  16. //const int yRowStart = yRowStride * y;
  17. const int uvRowStart = uvRowStride * (y >> 1);
  18. for (int x = 0; x < width; x++) {
  19. const int uvRowOffset = (x >> 1) * uvPixelStride;
  20. rgbaIntToBytes(YUV2RGBA(*pY++,//pY[yRowStart + x],
  21. pU[uvRowStart + uvRowOffset],
  22. pV[uvRowStart + uvRowOffset]),
  23. pRGBA);
  24. pRGBA += 4;
  25. }
  26. }
  27. }
  28. }
  29. void YUV420PToRGBAInt(unsigned char *src, int *dst, int width, int height,
  30. int yRowStride, int uvRowStride, int uvPixelStride, int format) {
  31. if (format == YUV420P_I420 || format == YUV420P_YV12) {
  32. unsigned char *pY = src;
  33. unsigned char *pU;
  34. unsigned char *pV;
  35. int rgbaIndex = 0;
  36. if (format == YUV420P_I420) {
  37. pU = src + width * height;
  38. pV = src + width * height / 4 * 5;
  39. } else {
  40. pU = src + width * height / 4 * 5;
  41. pV = src + width * height;
  42. }
  43. for (int y = 0; y < height; y++) {
  44. //const int yRowStart = yRowStride * y;
  45. const int uvRowStart = uvRowStride * (y >> 1);
  46. for (int x = 0; x < width; x++) {
  47. const int uvRowOffset = (x >> 1) * uvPixelStride;
  48. dst[rgbaIndex++] = YUV2RGBA(*pY++,//pY[yRowStart + x],
  49. pU[uvRowStart + uvRowOffset],
  50. pV[uvRowStart + uvRowOffset]);
  51. }
  52. }
  53. }
  54. }
YUV420SP转RGB:
  1. void YUV420SPToRGBAByte(unsigned char *src, unsigned char *dst, int width, int height,
  2. int format) {
  3. if (format == YUV420SP_NV12 || format == YUV420SP_NV21) {
  4. unsigned char *pRGBA = dst;
  5. unsigned char *pY = src;
  6. unsigned char *pUV = src + width * height;
  7. for (int y = 0; y < height; y++) {
  8. for (int x = 0; x < width; x++) {
  9. int uvOffset = (y >> 1) * width + 2 * (x >> 1);
  10. int uOffset;
  11. int vOffset;
  12. if (format == YUV420SP_NV12) {
  13. uOffset = uvOffset;
  14. vOffset = uvOffset + 1;
  15. } else {
  16. uOffset = uvOffset + 1;
  17. vOffset = uvOffset;
  18. }
  19. rgbaIntToBytes(YUV2RGBA(*pY++, pUV[uOffset], pUV[vOffset]), pRGBA);
  20. pRGBA += 4;
  21. }
  22. }
  23. }
  24. }
  25. void YUV420SPToRGBAInt(unsigned char *src, int *dst, int width, int height,
  26. int format) {
  27. if (format == YUV420SP_NV12 || format == YUV420SP_NV21) {
  28. unsigned char *pY = src;
  29. unsigned char *pUV = src + width * height;
  30. int rgbaIndex = 0;
  31. for (int y = 0; y < height; y++) {
  32. for (int x = 0; x < width; x++) {
  33. int uvOffset = (y >> 1) * width + 2 * (x >> 1);
  34. int uOffset;
  35. int vOffset;
  36. if (format == YUV420SP_NV12) {
  37. uOffset = uvOffset;
  38. vOffset = uvOffset + 1;
  39. } else {
  40. uOffset = uvOffset + 1;
  41. vOffset = uvOffset;
  42. }
  43. dst[rgbaIndex++] = YUV2RGBA(*pY++, pUV[uOffset], pUV[vOffset]);
  44. }
  45. }
  46. }
  47. }
JNI接口
  1. extern "C"
  2. JNIEXPORT void JNICALL
  3. Java_com_qxt_yuv420_NativeUtils_I420ToRGBAByte(JNIEnv *env, jclass clazz,
  4. jbyteArray src, jbyteArray dst,
  5. jint width, jint height, jint yRowStride,
  6. jint uvRowStride, jint uvPixelStride) {
  7. jbyte *_src = env->GetByteArrayElements(src, nullptr);
  8. jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
  9. YUV420PToRGBAByte(reinterpret_cast<unsigned char *>(_src),
  10. reinterpret_cast<unsigned char *>(_dst),
  11. width, height, yRowStride, uvRowStride, uvPixelStride, YUV420P_I420);
  12. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  13. env->ReleaseByteArrayElements(dst, _dst, 0);
  14. }
  15. extern "C"
  16. JNIEXPORT void JNICALL
  17. Java_com_qxt_yuv420_NativeUtils_I420ToRGBAInt(JNIEnv *env, jclass clazz,
  18. jbyteArray src, jintArray dst,
  19. jint width, jint height, jint yRowStride,
  20. jint uvRowStride, jint uvPixelStride) {
  21. jbyte *_src = env->GetByteArrayElements(src, NULL);
  22. jint *_dst = env->GetIntArrayElements(dst, NULL);
  23. YUV420PToRGBAInt(reinterpret_cast<unsigned char *>(_src), _dst, width, height, yRowStride,
  24. uvRowStride, uvPixelStride, YUV420P_I420);
  25. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  26. env->ReleaseIntArrayElements(dst, _dst, 0);
  27. }
  28. extern "C"
  29. JNIEXPORT void JNICALL
  30. Java_com_qxt_yuv420_NativeUtils_YV12ToRGBAByte(JNIEnv *env, jclass clazz,
  31. jbyteArray src, jbyteArray dst,
  32. jint width, jint height, jint yRowStride,
  33. jint uvRowStride, jint uvPixelStride) {
  34. jbyte *_src = env->GetByteArrayElements(src, nullptr);
  35. jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
  36. YUV420PToRGBAByte(reinterpret_cast<unsigned char *>(_src),
  37. reinterpret_cast<unsigned char *>(_dst),
  38. width, height, yRowStride, uvRowStride, uvPixelStride, YUV420P_YV12);
  39. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  40. env->ReleaseByteArrayElements(dst, _dst, 0);
  41. }
  42. extern "C"
  43. JNIEXPORT void JNICALL
  44. Java_com_qxt_yuv420_NativeUtils_YV12ToRGBAInt(JNIEnv *env, jclass clazz,
  45. jbyteArray src, jintArray dst,
  46. jint width, jint height, jint yRowStride,
  47. jint uvRowStride, jint uvPixelStride) {
  48. jbyte *_src = env->GetByteArrayElements(src, NULL);
  49. jint *_dst = env->GetIntArrayElements(dst, NULL);
  50. YUV420PToRGBAInt(reinterpret_cast<unsigned char *>(_src),
  51. _dst, width, height, yRowStride, uvRowStride, uvPixelStride, YUV420P_YV12);
  52. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  53. env->ReleaseIntArrayElements(dst, _dst, 0);
  54. }
  55. extern "C"
  56. JNIEXPORT void JNICALL
  57. Java_com_qxt_yuv420_NativeUtils_NV12ToRGBAByte(JNIEnv *env, jclass clazz,
  58. jbyteArray src, jbyteArray dst,
  59. jint width, jint height) {
  60. jbyte *_src = env->GetByteArrayElements(src, NULL);
  61. jbyte *_dst = env->GetByteArrayElements(dst, NULL);
  62. YUV420SPToRGBAByte(reinterpret_cast<unsigned char *>(_src),
  63. reinterpret_cast<unsigned char *>(_dst), width, height, YUV420SP_NV12);
  64. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  65. env->ReleaseByteArrayElements(dst, _dst, 0);
  66. }
  67. extern "C"
  68. JNIEXPORT void JNICALL
  69. Java_com_qxt_yuv420_NativeUtils_NV12ToRGBAInt(JNIEnv *env, jclass clazz,
  70. jbyteArray src, jintArray dst,
  71. jint width, jint height) {
  72. jbyte *_src = env->GetByteArrayElements(src, NULL);
  73. jint *_dst = env->GetIntArrayElements(dst, NULL);
  74. YUV420SPToRGBAInt(reinterpret_cast<unsigned char *>(_src), _dst, width, height, YUV420SP_NV12);
  75. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  76. env->ReleaseIntArrayElements(dst, _dst, 0);
  77. }
  78. extern "C"
  79. JNIEXPORT void JNICALL
  80. Java_com_qxt_yuv420_NativeUtils_NV21ToRGBAByte(JNIEnv *env, jclass clazz,
  81. jbyteArray src, jbyteArray dst,
  82. jint width, jint height) {
  83. jbyte *_src = env->GetByteArrayElements(src, NULL);
  84. jbyte *_dst = env->GetByteArrayElements(dst, NULL);
  85. YUV420SPToRGBAByte(reinterpret_cast<unsigned char *>(_src),
  86. reinterpret_cast<unsigned char *>(_dst), width, height, YUV420SP_NV21);
  87. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  88. env->ReleaseByteArrayElements(dst, _dst, 0);
  89. }
  90. extern "C"
  91. JNIEXPORT void JNICALL
  92. Java_com_qxt_yuv420_NativeUtils_NV21ToRGBAInt(JNIEnv *env, jclass clazz,
  93. jbyteArray src, jintArray dst,
  94. jint width, jint height) {
  95. jbyte *_src = env->GetByteArrayElements(src, NULL);
  96. jint *_dst = env->GetIntArrayElements(dst, NULL);
  97. YUV420SPToRGBAInt(reinterpret_cast<unsigned char *>(_src), _dst, width, height, YUV420SP_NV21);
  98. env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
  99. env->ReleaseIntArrayElements(dst, _dst, 0);
  100. }
java 函数及参数说明
  1. package com.qxt.yuv420;
  2. /**
  3. * @author Tyler Qiu
  4. * @date: 2020/05/09
  5. */
  6. public class NativeUtils {
  7. /*
  8. * YYYYYYYY UU VV =>I420 =>YUV420P
  9. * YYYYYYYY VV UU =>YV12 =>YUV420P
  10. * YYYYYYYY UV UV =>NV12 =>YUV420SP
  11. * YYYYYYYY VU VU =>NV21 =>YUV420SP
  12. */
  13. static {
  14. System.loadLibrary("NativeUtils");
  15. }
  16. /**
  17. * convert I420 to ARGB_8888
  18. *
  19. * @param src src I420 byte array
  20. * @param dst dst RGBA byte array, the length of the dst array must be >= width*height*4
  21. * @param width image width
  22. * @param height image height
  23. * @param yRowStride The row stride of plane y.
  24. * @param uvRowStride The row stride of plane u or v.
  25. * @param uvPixelStride The pixel stride of plane u or v.
  26. */
  27. public static native void I420ToRGBAByte(byte[] src, byte[] dst, int width, int height,
  28. int yRowStride, int uvRowStride, int uvPixelStride);
  29. /**
  30. * convert I420 to ARGB_8888
  31. *
  32. * @param src src I420 byte array
  33. * @param dst dst RGBA int array, the length of the dst array must be >= width*height
  34. * @param width image width
  35. * @param height image height
  36. * @param yRowStride The row stride of plane y.
  37. * @param uvRowStride The row stride of plane u or v.
  38. * @param uvPixelStride The pixel stride of plane u or v.
  39. */
  40. public static native void I420ToRGBAInt(byte[] src, int[] dst, int width, int height,
  41. int yRowStride, int uvRowStride, int uvPixelStride);
  42. /**
  43. * convert YV12 to ARGB_8888
  44. *
  45. * @param src src YV12 byte array
  46. * @param dst dst RGBA byte array, the length of the dst array must be >= width*height*4
  47. * @param width image width
  48. * @param height image height
  49. * @param yRowStride The row stride of plane y.
  50. * @param uvRowStride The row stride of plane u or v.
  51. * @param uvPixelStride The pixel stride of plane u or v.
  52. */
  53. public static native void YV12ToRGBAByte(byte[] src, byte[] dst, int width, int height,
  54. int yRowStride, int uvRowStride, int uvPixelStride);
  55. /**
  56. * convert YV12 to ARGB_8888
  57. *
  58. * @param src src YV12 byte array
  59. * @param dst dst RGBA int array, the length of the dst array must be >= width*height
  60. * @param width image width
  61. * @param height image height
  62. * @param yRowStride The row stride of plane y.
  63. * @param uvRowStride The row stride of plane u or v.
  64. * @param uvPixelStride The pixel stride of plane u or v.
  65. */
  66. public static native void YV12ToRGBAInt(byte[] src, int[] dst, int width, int height,
  67. int yRowStride, int uvRowStride, int uvPixelStride);
  68. /**
  69. * convert NV12 to ARGB_8888
  70. *
  71. * @param src src NV12 byte array
  72. * @param dst dst RGBA byte array, the length of the dst array must be >= width*height*4
  73. * @param width image width
  74. * @param height image height
  75. */
  76. public static native void NV12ToRGBAByte(byte[] src, byte[] dst, int width, int height);
  77. /**
  78. * convert NV12 to ARGB_8888
  79. *
  80. * @param src src NV12 byte array
  81. * @param dst dst RGBA int array, the length of the dst array must be >= width*height
  82. * @param width image width
  83. * @param height image height
  84. */
  85. public static native void NV12ToRGBAInt(byte[] src, int[] dst, int width, int height);
  86. /**
  87. * convert NV21 to ARGB_8888
  88. *
  89. * @param src src NV21 byte array
  90. * @param dst dst RGBA byte array, the length of the dst array must be >= width*height*4
  91. * @param width image width
  92. * @param height image height
  93. */
  94. public static native void NV21ToRGBAByte(byte[] src, byte[] dst, int width, int height);
  95. /**
  96. * convert NV21 to ARGB_8888
  97. *
  98. * @param src src NV21 byte array
  99. * @param dst dst RGBA int array, the length of the dst array must be >= width*height
  100. * @param width image width
  101. * @param height image height
  102. */
  103. public static native void NV21ToRGBAInt(byte[] src, int[] dst, int width, int height);
  104. }

本文中的代码和i420/yv12/nv12/n21文件已经上传到github:https://github.com/qiuxintai/YUV420Converter,如果本文代码对你有帮助,烦请在github上给我一个小小的star。除转换外,YUV420Converter中还有旋转RGB和YUV420的代码,如果还想了解RGB和YUV420的旋转,请戳:
RGB和YUV420旋转90/180/270度

5. 使用7yuv

除了用代码转换,我们还可以用7yuv工具来查看和转换YUV格式的图像。
7yuv下载地址:https://www.onlinedown.net/soft/1225925.htm

5.1 查看

打开7yuv,File -> open 或者直接将yuv图像拖入7yuv界面进行查看:

7yuv使用1.png

5.2 转换

选择 Edit -> Convert Format 转换:

7yuv使用2.png

6. 本文参考:

  1. https://zh.wikipedia.org/wiki/YUV
  2. https://developer.android.google.cn/reference/android/graphics/ImageFormat?hl=en#YUV_420_888
  3. https://blog.csdn.net/lbknxy/article/details/54633008?tdsourcetag=s_pcqq_aiomsg



链接:https://www.jianshu.com/p/944ede616261

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/670271
推荐阅读
相关标签
  

闽ICP备14008679号