java如何将YUV422转换为YUV420
我想使用at H.264 form camera preview获取数据,常用摄像头为YV12,因此我可以使用:
public byte[] YV12toYUV420PackedSemiPlanar(final byte[] input, final byte[] output, final int width, final int height)
{
this.halfWidth = width / 2;
this.halfHeight = height / 2;
this.frameRate = framerate;
this.yStride = (int) Math.ceil(width/16.0f) * 16;
this.cStride = (int) Math.ceil(width/32.0f) * 16;
this.ySize = yStride * height;
this.cSize = cStride * height / 2;
for (int i=0; i<height; i++)
System.arraycopy(input, yStride*i, output, yStride*i, yStride); // Y
for (int i=0; i<halfHeight; i++)
{
for (int j=0; j<halfWidth; j++)
{
output[ySize + (i*halfWidth + j)*2] = input[ySize + cSize + i*cStride + j]; // Cb (U)
output[ySize + (i*halfWidth + j)*2 + 1] = input[ySize + i*cStride + j]; // Cr (V)
}
}
return output;
}
但是现在我的相机预览是yuv422,所以如何使用java将yuv422转换为YUV420,我搜索了一些c方法和理论,但我无法成功地将其转换
我用过这个:我说得对吗
static void yuyv_to_yuv420PackedSemiPlanar( byte[] yuyv, byte[] yuv, int w, int h ) {
int expected_length;
int actual_length;
actual_length = yuyv.length;
expected_length = PixelFormat.V4L2_PIX_FMT_YUYV.frameSize( w, h );
if ( actual_length < expected_length ) {
throw new IllegalArgumentException( "invalid input buffer length " + actual_length + " for yuyv image of size ( " + w + ", " + h + " ). Should be " + expected_length );
}
actual_length = yuv.length;
expected_length = PixelFormat.V4L2_PIX_FMT_RGB24.frameSize( w, h );
if ( actual_length < expected_length ) {
throw new IllegalArgumentException( "invalid output buffer length " + actual_length + " for yuv image of size ( " + w + ", " + h + " ). Should be " + expected_length );
}
for( int i = 0, j = 0; i < w * h * 3; i += 6, j += 4 ) {
yuv[ i + 0 ] = yuyv[ j + 0 ];
yuv[ i + 1 ] = yuyv[ j + 1 ];
yuv[ i + 2 ] = yuyv[ j + 3 ];
yuv[ i + 3 ] = yuyv[ j + 2 ];
yuv[ i + 4 ] = yuyv[ j + 1 ];
yuv[ i + 5 ] = yuyv[ j + 3 ];
}
}
# 1 楼答案
你的代码错了
YUV format
yuv_rgb
yuv-player
# 2 楼答案
下面是一个从YUV422(yuyv,V4L2_PIX_FMT_yuyv,交错格式)转换为YUV420SP(NV21,V4L2_PIX_FMT_NV21,半平面格式)的代码示例