我配置了我的代码,以便使用一个对象和其他众所周知的APIYUV_420_888
从我的设备的相机中获取帧流。现在我需要将这些帧转换为像素格式并调用一个本机函数,该函数期望这种格式的帧执行某些计算。这是我在 imagereader 回调中使用的代码,用于重新排列帧的字节:imageReader
camera2
NV21
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;
}
但我不知道为什么,结果帧在水平方向“翻转”。换句话说,当我将相机向右移动时,我所描述的打包过程后的帧正在向左移动,就像传感器被放置在一个反自然的位置一样。
我希望你能明白我的意思
谢谢,
杰米