如何使用 resizeAspectFill 计算来自 previewLayer 的请求视觉中的 regionOfInterest

问题描述

我有一个 captureSession,我在这个会话中使用 AVCaptureVideoPreviewLayer,并将 .resizeAspectFill 作为 videoGravity。 对于我的视觉请求,我需要设置一个感兴趣的区域,这个CGRect需要对应previewLayer中已经裁剪过的视频

private func setupCameraLiveView() {
        // Setup Device input
        captureSession.sessionPreset = .hd4K3840x2160
        guard
            let videoDevice = AVCaptureDevice.default(.builtInWideAngleCamera,for: .video,position: .back),let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice),captureSession.canAddInput(videoDeviceInput)
        else {
            showAlert(
                withTitle: "Camera not found",message: "There seems to be a problem with the camera on your device.")
            return
        }
        captureSession.addInput(videoDeviceInput)
        
        // Setup Output
        let captureOutput = AVCaptureVideoDataOutput()
        captureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
        captureOutput.setSampleBufferDelegate(self,queue: dispatchQueue.global(qos: dispatchQoS.QoSClass.default))
        captureSession.addOutput(captureOutput)
        captureOutput.connection(with: .video)?.videoOrientation = .portrait
        // Setup previewLayer
        let cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewLayer.videoGravity = .resizeAspectFill
        cameraPreviewLayer.connection?.videoOrientation = .portrait
        cameraPreviewLayer.frame = liveView.frame
        view.layer.insertSublayer(cameraPreviewLayer,at: 0)
        liveView.isHidden = true
        
        // Launch session
        captureSession.startRunning()
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)