zumzum
zumzum

Reputation: 20138

Get CMSampleBuffer of corresponding AVMetadataObject?

I am running a session that successfully return

AVMetadataObjects

in the

AVCaptureMetadataOutputObjectsDelegate

My question is how to get the corresponding image the AVMetadataObject came from?

Upvotes: 0

Views: 484

Answers (1)

johnnyclem
johnnyclem

Reputation: 3344

TL:DR; https://developer.apple.com/documentation/avfoundation/avcapturedataoutputsynchronizer

You're going to want to implement an AVCaptureDataOutputSynchronizerDelegate. It's pretty straight-forward, you just add both inputs (video and metadata) to your AVCaptureDataOutputSynchronizer and then you get a AVCaptureSynchronizedDataCollection which contains one or more sampleBuffers from the same timestamp. This way when you're working with your metadata output sampleBuffer, you just grab the corresponding image sampleBuffer from the AVCaptureSynchronizedDataCollection.

There's a little bit of code, but the brunt of it is to take your code that's currently in

open func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { ... } and move it into the new delegate

public func dataOutputSynchronizer(_ synchronizer: AVCaptureDataOutputSynchronizer, didOutput synchronizedDataCollection: AVCaptureSynchronizedDataCollection) {...}

Upvotes: 2

Related Questions