设置 AVCaptureSession 时 availableMetadataObjectTypes 中的空数组

Empty array in availableMetadataObjectTypes when setting up an AVCaptureSession

我想配置一个扫描二维码的摄像头。但是当尝试添加支持的元数据类型时,我发现 availableMetadataObjectTypes 的数组是空的。

我得到了这段代码:

private var captureDevice: AVCaptureDevice? {
    return AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
}

private var input: AVCaptureDeviceInput? {
    guard AVCaptureDevice.authorizationStatus(for: .video) == .authorized else { return nil }
    guard let device = captureDevice else { return nil }
    return try? AVCaptureDeviceInput(device: device)
}

private var output: AVCaptureMetadataOutput? {
    let output = AVCaptureMetadataOutput()
    print(output.availableMetadataObjectTypes)
    output.setMetadataObjectsDelegate(delegate, queue: .global(qos: .userInitiated))
    return output
}

这部分代码用输入和输出配置会话:

let session = AVCaptureSession()

session.beginConfiguration()
session.sessionPreset = .high
if let input = input, session.canAddInput(input) { session.addInput(input) }
if let output = output, session.canAddOutput(output) { session.addOutput(output) }
session.commitConfiguration()

output 计算变量中的打印为我提供了一个空数组 []。我错过了什么?

在访问 属性 之前,您必须将输出添加到会话中。否则,它不知道会话可以捕获哪些类型的元数据。

来自文档:

The available types are dependent on the capabilities of the AVCaptureInput.Port to which the receiver’s connection is attached.