根据http://developer.android.com/reference/android/graphics/ImageFormat.html#NV21, NV21 是默认使用的格式。
网上有很多关于 YUV NV21 到 RGB 转换的代码。但是,当我查看代码时,我怀疑代码的正确性。
第一个组件 V 应首先出现,然后是第一个组件 U
根据http://wiki.videolan.org/YUV#NV21, NV21 is like NV12, but with U and V order reversed: it starts with V.
然而,当我完成代码实现时
-
http://pastebin.com/T0my7zSc- 假设 U 优先
-
https://stackoverflow.com/a/8394202/72437- 它假设 U 也排在第一位
-
https://stackoverflow.com/a/10125048/72437- 看来你也是第一位的
R应该是最重要的位置根据实施int argb
in 颜色.java, R 应该位于最重要的位置。但是,我经历了以下代码实现
-
http://pastebin.com/T0my7zSc- 假设 R 处于最不重要的位置
-
https://stackoverflow.com/a/8394202/72437- 假设 R 处于最不重要的位置
我想知道,他们是否犯了常见的错误,或者我忽略了一些事情?
目前,我的实现如下。
public static void YUV_NV21_TO_RGB(int[] argb, byte[] yuv, int width, int height) {
final int frameSize = width * height;
final int ii = 0;
final int ij = 0;
final int di = +1;
final int dj = +1;
int a = 0;
for (int i = 0, ci = ii; i < height; ++i, ci += di) {
for (int j = 0, cj = ij; j < width; ++j, cj += dj) {
int y = (0xff & ((int) yuv[ci * width + cj]));
int v = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 0]));
int u = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 1]));
y = y < 16 ? 16 : y;
int r = (int) (1.164f * (y - 16) + 1.596f * (v - 128));
int g = (int) (1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
int b = (int) (1.164f * (y - 16) + 2.018f * (u - 128));
r = r < 0 ? 0 : (r > 255 ? 255 : r);
g = g < 0 ? 0 : (g > 255 ? 255 : g);
b = b < 0 ? 0 : (b > 255 ? 255 : b);
argb[a++] = 0xff000000 | (r << 16) | (g << 8) | b;
}
}
}