HarmonyOS 鸿蒙Next中前后置摄像头切换导致录制视频上下颠倒
HarmonyOS 鸿蒙Next中前后置摄像头切换导致录制视频上下颠倒
【问题现象】
使用摄像头录制时,前后切换摄像头时,录制的视频出现上下颠倒问题,问题场景的操作步骤如下:
- 开启前置摄像头预览;
- 开始录制;
- 录制过程中,切换到后置摄像头,后置摄像头在UI上显示是正常的;
- 结束录制;
- 查看录制文件,录制的文件播放时后置摄像头是上下颠倒的。
【背景知识】
【定位思路】
- 前置摄像头录像,预览和录像成镜像属于正常现象,因为预览是镜像预览。
- 录像过程中,切换前后置摄像头,前置和后置成颠倒是因为AVRecorderConfig中的rotation设置的有角度,因此需重新配置config。
【解决方案】
- 摄像头获取预览的YUV数据并写到文件中保存,可以使用Camera_PhotoCaptureSetting中的rotation设置拍照的旋转角度,默认0度。
- 当前规格,预览流旋转角度固定值90度,目前双路预览流角度固定前置摄像头得到的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;
}
对于前置摄像头的数据还需要进行镜像翻转的操作:
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;
}
更多关于HarmonyOS 鸿蒙Next中前后置摄像头切换导致录制视频上下颠倒的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于HarmonyOS 鸿蒙Next中前后置摄像头切换导致录制视频上下颠倒的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,前后置摄像头切换导致录制视频上下颠倒的问题
在HarmonyOS鸿蒙Next中,前后置摄像头切换导致录制视频上下颠倒的问题,通常与摄像头传感器的方向信息处理有关。鸿蒙系统在切换摄像头时,可能会错误地处理或未正确应用摄像头的方向信息,导致视频帧的旋转或翻转不正确。这可能是由于系统在切换摄像头时未能正确读取或应用摄像头的元数据(如EXIF方向标签),或者摄像头驱动在切换时未正确传递方向信息。解决此问题通常需要检查摄像头驱动和系统层对摄像头方向信息的处理逻辑,确保在切换摄像头时正确应用方向信息。