【关键字】
相机 / camera / 双路预览 / ImageReceiver / 预览流 / 角度 / YUV
【问题描述】
HarmonyOS相机双路预览,通过ImageReceiver接收预览流,再通过XComponent nativewindow结合OpenGL进行视频渲染后,图像的角度不正确,是横向的,如何处理?
【解决方案】
目前双路预览流角度固定前置摄像头得到的YUV数据顺时针旋转了90度,后置摄像头得到的YUV数据顺时针旋转了270度。
可以通过YUV数据进行旋转操作,对于前置摄像头的数据还需进行镜像翻转操作,示例代码如下:
private byte[] rotateYUVDegree270(byte[] data, int imageWidth, int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
// Rotate the Y luma
int i = 0;
for (int x = imageWidth - 1; x >= 0; x--) {
for (int y = 0; y < imageHeight; y++) {
yuv[i] = data[y * imageWidth + x];
i++;
}
}// Rotate the U and V color components
i = imageWidth * imageHeight;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
i++;
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
i++;
}
}
return yuv;
}