HarmonyOS 如何对摄像头预览的数据进行镜像和旋转,在什么时机进行呢?

HarmonyOS
2024-12-25 15:47:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

1.摄像头获取预览的YUV数据并写到文件中保存,可以使用Camera_PhotoCaptureSetting中的rotation设置拍照的旋转角度,默认应该是0度,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/_camera___photo_capture_setting-V5#rotation

当前规格,预览流旋转角度固定值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;
}

2.前置摄像头录像 预览和录像成镜像是正常的,因为预览是做了镜像的。

对于前置摄像头的数据进行镜像翻转的操作:

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;
}

3.可以参考以下demo:https://gitee.com/kairen-13/AVCodecSample

分享
微博
QQ
微信
回复
2024-12-25 18:09:08
相关问题
摄像头获取到yuv数据是否有旋转
973浏览 • 1回复 待解决
相机预览及切换摄像头
1345浏览 • 1回复 待解决
如何获取前置摄像头预览图像
2764浏览 • 1回复 待解决
HarmonyOS 摄像头预览画面方向错误
572浏览 • 1回复 待解决
HarmonyOS 获取摄像头能力
383浏览 • 1回复 待解决
HarmonyOS 摄像头切换时卡死
383浏览 • 1回复 待解决
HarmonyOS 摄像头录制问题
457浏览 • 1回复 待解决
HarmonyOS 前置摄像头开启
559浏览 • 1回复 待解决
HarmonyOS 打开摄像头失败
371浏览 • 1回复 待解决
请问3.1如何调用摄像头
2732浏览 • 1回复 待解决
HarmonyOS 摄像头录制时,帧回调数据
406浏览 • 1回复 待解决
HarmonyOS 录制屏幕 录制摄像头咨询
818浏览 • 1回复 待解决