1

I wanted to configure a camera with scanning QR codes. But when trying to add the supported metadata types I found that the array of availableMetadataObjectTypes is empty.

I ended up with this code:

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
}

This portion of the code which configures the session with the input and 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()

The print in the output computed variable gives me an empty array []. What am I missing?

ph1psG
  • 568
  • 5
  • 24

1 Answers1

6

You have to add the output to the session before you access the property. Otherwise, it doesn't know which types of metadata can be captured by the session.

From the documentation:

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

Frank Rupprecht
  • 9,191
  • 31
  • 56