无法在 macOS 上镜像 AVCaptureVideoPreviewLayer

Sim*_*est 4 macos avfoundation

我正在使用 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。

man*_*shg 7

您可以利用 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
    }
Run Code Online (Sandbox Code Playgroud)

您可以在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()
   }
Run Code Online (Sandbox Code Playgroud)