Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在macOS上镜像AVCaptureVideoPreviewLayer_Macos_Avfoundation - Fatal编程技术网

无法在macOS上镜像AVCaptureVideoPreviewLayer

无法在macOS上镜像AVCaptureVideoPreviewLayer,macos,avfoundation,Macos,Avfoundation,我正在使用AVCaptureVideoPreviewLayer(macOS,而不是iOS)从连接的摄像头捕获视频输入。我已经设置了捕获会话并创建了UIView以显示图像: self.captureLayer=AVCaptureVideoPreviewLayer(会话:self.captureSession); self.playerView.layer=self.captureLayer; 一切正常,我从相机上看到了图像,但现在我想镜像图像(垂直)。我使用的是CATTransferM3DMak

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

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

一切正常,我从相机上看到了图像,但现在我想镜像图像(垂直)。我使用的是CATTransferM3DMakeScale:

self.captureLayer.transform=CATTransferM3DMakeScale(-1.0,1.0,1.0)

但是,该层不是镜像图像,而是变为空白(父视图的背景)

我尝试过其他变换(例如,调整大小),效果很好。我还尝试了镜像superlayer:

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

这是可行的(尽管它镜像了整个窗口,包括标题栏!)


谢谢你的帮助。我相信转换是正确的,但由于某些原因,它不适用于AVCaptureVideoPreviewLayer。

您可以使用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
    }
你可以在


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


为您编写了一个示例代码。请核对一下。下面的片段

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()
   }

你能试试我提供的解决方案吗?谢谢@manishganvir-非常感谢你的答案和代码示例。我将在本周末尝试,但是如果这确实有效,我假设它将只提供横向访问的镜像,而使用CatTransferM3dMakeScale,我也可以垂直镜像(这也在我的应用程序中使用)。@manishganvir刚刚运行了您的示例代码-效果很好。还找到了
self.cameraLayer!。connection.videoOrientation=AVCaptureVideoOrientation.GraphitalUpsideDown将为我提供垂直镜像。谢谢你的帮助!非常感谢你给我这个很棒的答案!