我正在使用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。
答案 0 :(得分: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()
}