微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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 举报,一经查实,本站将立刻删除。