【问题标题】:Unable to mirror AVCaptureVideoPreviewLayer on macOS无法在 macOS 上镜像 AVCaptureVideoPreviewLayer
【发布时间】:2017-01-27 01:44:04
【问题描述】:

我正在使用 AVCaptureVideoPreviewLayer(macOS,而不是 iOS)从连接的摄像头捕获视频输入。我已经设置了一个捕获会话并创建了一个 UIView 来显示图像:

self.captureLayer = AVCaptureVideoPreviewLayer(session: self.captureSession); self.playerView.layer = self.captureLayer;

一切正常,我看到了来自相机的图像,但现在我想(垂直)镜像图像。我正在使用 CATransform3DMakeScale:

self.captureLayer.transform = CATransform3DMakeScale(-1.0, 1.0, 1.0);

然而,图层并没有镜像图像,而是变成空白(父视图的背景)。

我尝试了其他转换(例如调整大小),它们工作正常。我也尝试过镜像超级层:

self.captureLayer.superLayer?.transform = CATransform3DMakeScale(-1.0, 1.0, 1.0);

这很有效(尽管它反映了包括标题栏在内的整个窗口!)。

任何帮助表示赞赏。我相信转换是正确的,但由于某种原因它不适用于 AVCaptureVideoPreviewLayer。

【问题讨论】:

  • 你可以试试我提供的解决方案吗?
  • 感谢@manishganvir - 非常感谢答案和代码示例。本周末将尝试,但是如果这确实有效,我认为它只会提供跨水平访问的镜像,而使用 CATransform3DMakeScale 我也可以垂直镜像(这也在我的应用程序中使用)。
  • @manishganvir 刚刚运行了您的示例代码 - 效果很好。还发现self.cameraLayer!.connection.videoOrientation = AVCaptureVideoOrientation.portraitUpsideDown 将为我提供垂直镜像。感谢您的帮助!

标签: macos avfoundation


【解决方案1】:

您可以使用 avcaptureconnection 属性来执行此操作。

self.cameraLayer = AVCaptureVideoPreviewLayer(session: frontVideoSession)
self.cameraLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill

    if (self.cameraLayer!.connection.isVideoMirroringSupported)
    {
        self.cameraLayer!.connection.automaticallyAdjustsVideoMirroring = false
        self.cameraLayer!.connection.isVideoMirrored = true
    }

您可以在https://developer.apple.com/reference/avfoundation/avcaptureconnection/1389172-isvideomirrored阅读更多相关信息


isVideoMirrored 标志允许您选择是否要镜像视频。如果设置为true,则镜像视频,如果设置为false,则不镜像。为了使用此标志,您必须将 automaticAdjustsVideoMirroring 标志设置为 false。


为您编写了示例代码https://github.com/manishganvir/mac-camera-mirror-example。请检查相同。下面的片段

do {
         let input = try AVCaptureDeviceInput(device: camDevice)


          VideoSession.addInput(input)

          self.cameraLayer = AVCaptureVideoPreviewLayer(session: VideoSession)
          print(" connection " , self.cameraLayer!.connection.isVideoMirroringSupported , self.cameraLayer!.connection.automaticallyAdjustsVideoMirroring);

          if (self.cameraLayer!.connection.isVideoMirroringSupported)
          {
                 self.cameraLayer!.connection.automaticallyAdjustsVideoMirroring = false
                 self.cameraLayer!.connection.isVideoMirrored = true
          }
          self.cameraLayer!.frame = self.view.bounds

          self.view.layer = self.cameraLayer!
          self.view.wantsLayer = true

          VideoSession.startRunning()
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2017-09-15
    相关资源
    最近更新 更多