我配置了我的代码,以便使用YUV_420_888
对象和其他众所周知的imageReader
API从设备的相机中获取camera2
帧流。现在我需要将这些帧转换为NV21
像素格式并调用本机函数,该函数期望此格式的帧执行某些计算。这是我在imagereader回调中使用的代码,用于重新排列帧的字节:
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader mReader) {
Image image = null;
image = mReader.acquireLatestImage();
if (image == null) {
return;
}
byte[] bytes = convertYUV420ToNV21(image);
nativeVideoFrame(bytes);
image.close();
}
};
private byte[] convertYUV420ToNV21(Image imgYUV420) {
byte[] rez;
ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
ByteBuffer buffer1 = imgYUV420.getPlanes()[1].getBuffer();
ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();
int buffer0_size = buffer0.remaining();
int buffer1_size = buffer1.remaining();
int buffer2_size = buffer2.remaining();
byte[] buffer0_byte = new byte[buffer0_size];
byte[] buffer1_byte = new byte[buffer1_size];
byte[] buffer2_byte = new byte[buffer2_size];
buffer0.get(buffer0_byte, 0, buffer0_size);
buffer1.get(buffer1_byte, 0, buffer1_size);
buffer2.get(buffer2_byte, 0, buffer2_size);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
try {
outputStream.write( buffer0_byte );
outputStream.write( buffer1_byte );
outputStream.write( buffer2_byte );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rez = outputStream.toByteArray( );
return rez;
}
但我不知道为什么,所得到的帧在水平方向上“翻转”。换句话说,当我将相机向右移动时,我所描述的包装程序后的框架向左移动,就像传感器放置在一个反自然的位置一样。
我希望你能明白我的意思
谢谢,
JM
答案 0 :(得分:1)
相机可以生成镜像图像。如果您不希望它被镜像 - 您需要执行水平镜像,交换每行中的像素。