如何将android位图转换为NV12颜色格式?
我正在写一些将android位图转换为NV12格式的代码 我从android位图中找到了提供NV21的代码,似乎代码可以正常工作。 () 我发现的唯一区别是根据参考在NV12和NV21之间切换U和V字节。 () 所以我改变了原始代码中U和V的位置,结果如下如何将android位图转换为NV12颜色格式?,android,colors,bitmap,Android,Colors,Bitmap,我正在写一些将android位图转换为NV12格式的代码 我从android位图中找到了提供NV21的代码,似乎代码可以正常工作。 () 我发现的唯一区别是根据参考在NV12和NV21之间切换U和V字节。 () 所以我改变了原始代码中U和V的位置,结果如下 byte [] getNV12(int inputWidth, int inputHeight, Bitmap scaled) { // Reference (Variation) : https://gist.github.com/
byte [] getNV12(int inputWidth, int inputHeight, Bitmap scaled) {
// Reference (Variation) : https://gist.github.com/wobbals/5725412
int [] argb = new int[inputWidth * inputHeight];
//Log.i(TAG, "scaled : " + scaled);
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte [] yuv = new byte[inputWidth*inputHeight*3/2];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
V = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128; // Previously U
U = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128; // Previously V
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
}
index ++;
}
}
}
byte[]getNV12(int-inputWidth,int-inputhweight,位图缩放){
//参考(变更):https://gist.github.com/wobbals/5725412
int[]argb=新的int[inputWidth*inputHeight];
//Log.i(标签,“缩放:”+缩放);
scaled.getPixels(argb,0,inputWidth,0,0,inputWidth,inputHeight);
字节[]yuv=新字节[inputWidth*inputWidth*3/2];
编码YUV420SP(yuv,argb,输入宽度,输入宽度);
缩放。回收();
返回yuv;
}
void encodeYUV420SP(字节[]yuv420sp,整数[]argb,整数宽度,整数高度){
最终整数帧大小=宽度*高度;
int-yIndex=0;
int uvIndex=帧大小;
int a,R,G,B,Y,U,V;
int指数=0;
对于(int j=0;j>24;//显然没有使用a
R=(argb[索引]&0xff0000)>>16;
G=(argb[索引]&0xff00)>>8;
B=(argb[索引]&0xff)>>0;
//著名的RGB-to-YUV算法
Y=((66*R+129*G+25*B+128)>>8)+16;
V=(-38*R-74*G+112*B+128)>>8)+128;//以前的U
U=((112*R-94*G-18*B+128)>>8)+128;//以前的V
yuv420sp[yIndex++]=(字节)((Y<0)?0:((Y>255)?255:Y));
如果(j%2==0&&index%2==0){
yuv420sp[uvIndex++]=(字节)((v255)?255:V));
yuv420sp[uvIndex++]=(字节)((U 255)?255:U));
}
索引++;
}
}
}
我在转换图像时出错了吗?
(我很确定编码器没有问题。)
损坏的图像截图:更换
a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
有,
你可以用
R = Color.red(argb[index]);
G = Color.green(argb[index]);
B = Color.blue(argb[index]);
您的其余代码工作得很好。我使用相同的代码使用mediaEncoder从摄像头图像创建视频。生成的视频中存在颜色(for)问题。似乎mediaEncoder支持几种格式(-Q5) 因此,我必须将其转换为N12(或I420格式),我根据
int uIndex=帧大小;
int-vIndex=frameSize+frameSize/4;
....
yuv420sp[uIndex++]=(字节)((u255)?255:U));
yuv420sp[vIndex++]=(字节)((v255)?255:V));
...
现在生成的视频似乎工作正常
R = Color.red(argb[index]);
G = Color.green(argb[index]);
B = Color.blue(argb[index]);
int uIndex = frameSize;
int vIndex = frameSize + frameSize/4;
....
yuv420sp[uIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
yuv420sp[vIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
...