ios - 使用 TrueDepth 摄像头拍摄照片时出现“没有事件且已启用的视频连接”错误

标签 ios truedepth-camera

我正在尝试将 TrueDepth 相机的深度数据与照片一起记录。但是打电话的时候 AVCapturePhotoOutput capturePhoto(withSettings,delegate)

我收到一个异常说明:

没有事件且已启用的视频连接

我像这样配置相机和输出(基本上遵循Apple关于 photo capturingcapturing depth 的指南):

func configurePhotoOutput() throws {
            self.captureSession = AVCaptureSession()

            guard self.captureSession != nil else {
                return
            }

            // Select a depth-capable capture device.
            guard let videoDevice = AVCaptureDevice.default(.builtInTrueDepthCamera,
                                                            for: .video, position: .unspecified)
                else { fatalError("No dual camera.") }

            // Select a depth (not disparity) format that works with the active color format.
            let availableFormats = videoDevice.activeFormat.supportedDepthDataFormats
            let depthFormat = availableFormats.first(where: { format in
                let pixelFormatType = CMFormatDescriptionGetMediaSubType(format.formatDescription)
                return (pixelFormatType == kCVPixelFormatType_DepthFloat16 ||
                    pixelFormatType == kCVPixelFormatType_DepthFloat32)
            })

            do {
                try videoDevice.lockForConfiguration()
                videoDevice.activeDepthDataFormat = depthFormat
                videoDevice.unlockForConfiguration()
            } catch {
                print("Could not lock device for configuration: \(error)")
                return
            }

            self.captureSession!.beginConfiguration()

            // add video input
            guard let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice),
                self.captureSession!.canAddInput(videoDeviceInput)
                else { fatalError("Can't add video input.") }
            self.captureSession!.addInput(videoDeviceInput)

            // add video output
            if self.captureSession!.canAddOutput(videoOutput) {
                self.captureSession!.addOutput(videoOutput)
                videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
            } else { fatalError("Can't add video output.") }


            // Set up photo output for depth data capture.
            let photoOutput = AVCapturePhotoOutput()
            photoOutput.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliverySupported
            guard self.captureSession!.canAddOutput(photoOutput)
                else { fatalError("Can't add photo output.") }
            self.captureSession!.addOutput(photoOutput)
            self.captureSession!.sessionPreset = .photo

            self.captureSession!.commitConfiguration()
            self.captureSession!.startRunning()
        }

负责拍摄照片的代码:

func captureImage(delegate: AVCapturePhotoCaptureDelegate,completion: @escaping (UIImage?, Error?) -> Void) {
    let photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
    photoSettings.isDepthDataDeliveryEnabled =
        self.photoOutput.isDepthDataDeliverySupported
    photoSettings.isDepthDataFiltered = false

    self.photoOutput.capturePhoto(with: photoSettings, delegate: delegate) // <---- error is being thrown on this call
    self.photoCaptureCompletionBlock = completion
}

我在这个配置中做错了什么?

最佳答案

通过以下实现解决了这个问题:

非常感谢任何评论/评论!

import AVFoundation
import UIKit

class CameraController: NSObject {

    var captureSession: AVCaptureSession?
    var videoDevice: AVCaptureDevice?
    var previewLayer: AVCaptureVideoPreviewLayer?

    var videoOutput = AVCaptureVideoDataOutput()
    var photoOutput = AVCapturePhotoOutput()

    func prepare(completionHandler: @escaping (Error?) -> Void) {
        func createCaptureSession() {
            captureSession = AVCaptureSession()
        }
        func configureCaptureDevices() throws {
            // Select a depth-capable capture device.
            guard let vd = AVCaptureDevice.default(.builtInTrueDepthCamera,
                                                            for: .video, position: .unspecified)
                else { fatalError("No dual camera.") }
            videoDevice = vd

            // Select a depth (not disparity) format that works with the active color format.
            let availableFormats = videoDevice!.activeFormat.supportedDepthDataFormats
            let depthFormat = availableFormats.first(where: { format in
                let pixelFormatType = CMFormatDescriptionGetMediaSubType(format.formatDescription)
                return (pixelFormatType == kCVPixelFormatType_DepthFloat16 ||
                    pixelFormatType == kCVPixelFormatType_DepthFloat32)
            })

            do {
                try videoDevice!.lockForConfiguration()
                videoDevice!.activeDepthDataFormat = depthFormat
                videoDevice!.unlockForConfiguration()
            } catch {
                print("Could not lock device for configuration: \(error)")
                return
            }
        }
        func configureDeviceInputs() throws {
            if( captureSession == nil) {
                throw CameraControllerError.captureSessionIsMissing
            }
            captureSession?.beginConfiguration()

            // add video input
            guard let videoDeviceInput = try? AVCaptureDeviceInput(device: self.videoDevice!),
                captureSession!.canAddInput(videoDeviceInput)
                else { fatalError("Can't add video input.") }
            captureSession!.addInput(videoDeviceInput)
            captureSession?.commitConfiguration()
        }
        func configurePhotoOutput() throws {
            guard let captureSession = self.captureSession else { throw CameraControllerError.captureSessionIsMissing }
            captureSession.beginConfiguration()

            // Set up photo output for depth data capture.
            photoOutput = AVCapturePhotoOutput()

            photoOutput.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])], completionHandler: nil)


            guard captureSession.canAddOutput(photoOutput)
                else { fatalError("Can't add photo output.") }
            captureSession.addOutput(photoOutput)
            // must be set after photoOutput is added to captureSession. Why???
            photoOutput.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliverySupported
            captureSession.sessionPreset = .photo
            captureSession.commitConfiguration()

            captureSession.startRunning()
        }

        DispatchQueue(label: "prepare").async {
            do {
                createCaptureSession()
                try configureCaptureDevices()
                try configureDeviceInputs()
                try configurePhotoOutput()
            }

            catch {
                DispatchQueue.main.async {
                    completionHandler(error)
                }

                return
            }

            DispatchQueue.main.async {
                completionHandler(nil)
            }
        }
    }

    func displayPreview(on view: UIView) throws {
        guard let captureSession = self.captureSession, captureSession.isRunning else { throw CameraControllerError.captureSessionIsMissing }

        self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        self.previewLayer?.connection?.videoOrientation = .portrait

        view.layer.insertSublayer(self.previewLayer!, at: 0)
        self.previewLayer?.frame = view.frame
    }


    func captureImage(delegate: AVCapturePhotoCaptureDelegate,completion: @escaping (UIImage?, Error?) -> Void) {
        let photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
        photoSettings.isDepthDataDeliveryEnabled = true
        photoSettings.isDepthDataFiltered = false
        self.photoOutput.capturePhoto(with: photoSettings, delegate: delegate)
        self.photoCaptureCompletionBlock = completion
    }

    var photoCaptureCompletionBlock: ((UIImage?, Error?) -> Void)?
}

extension CameraController {
    public enum CameraPosition {
        case front
        case rear
    }

    enum CameraControllerError: Swift.Error {
        case captureSessionAlreadyRunning
        case captureSessionIsMissing
        case inputsAreInvalid
        case invalidOperation
        case noCamerasAvailable
        case unknown
    }
}

关于ios - 使用 TrueDepth 摄像头拍摄照片时出现“没有事件且已启用的视频连接”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380629/

相关文章:

ios 快速应用程序开发 : UIScrollview not scrolling contents and working properly

ios - 应用程序没有网络连接

ios - 如何使用 iPhone X 的 faceID 数据

ios - AVDepthData 可以返回超过 8 位的数据吗?

ios - 如何在 iOS 上将深度数据捕获为 kCVPixelFormatType_DepthFloat16?

ios - 在 iOS 中使用音频电平反馈进行录音

iphone - 在 iOS 中朗读文本

javascript - 在移动 Safari 中检测 iOS5(首选 javascript)