HarmonyOS 相机双路预览,通过ImageReceiver 接收预览流后,怎么获取预览流的角度?

HarmonyOS 相机双路预览,通过ImageReceiver 接收预览流渲染后,图像的角度不正确,是横向的,camera预览流中怎么设置图像角度,ImageReceiver中怎么获取接受的预览流的图像角度

阅读 666
1 个回答

目前双路预览流角度固定前置摄像头预览得到的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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进