Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for scanning QR codes #199

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions camera/CameraManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,54 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}

/**
The signature for a handler.
The success value is the string representation of a scanned QR code, if any.
*/
public typealias QRCodeDetectionHandler = (Result<String, Error>) -> Void

/**
Start detecting QR codes.
*/
open func startQRCodeDetection(_ handler: @escaping QRCodeDetectionHandler) {
guard let captureSession = self.captureSession
else { return }

let output = AVCaptureMetadataOutput()

guard captureSession.canAddOutput(output)
else { return }

self.qrCodeDetectionHandler = handler
captureSession.addOutput(output)

// Note: The object types must be set after the output was added to the capture session.
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
output.metadataObjectTypes = [.qr, .ean8, .ean13, .pdf417]
}

/**
Stop detecting QR codes.
*/
open func stopQRCodeDetection() {
self.qrCodeDetectionHandler = nil

if let output = self.qrOutput {
self.captureSession?.removeOutput(output)
}
self.qrOutput = nil
}

/**
The stored handler for QR codes.
*/
private var qrCodeDetectionHandler: QRCodeDetectionHandler?

/**
The stored meta data output; used to detect QR codes.
*/
private var qrOutput: AVCaptureOutput? = nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phlippieb it seems that qrOutput is always nil. Could it be that in line 815 it should be qrOutput instead of output?

Right now there is an issue when you call stopQRCodeDetection() directly in the handler and then startQRCodeDetection(). By starting QR code detection the following times, it will not work again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was so long ago, I have no idea anymore 😁 it's totally possible. Is your issue fixed if you change line 815 to qrOutput?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phlippieb I fixed it in PR #237. Please check it out :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't my repo 😺


/**
Check if the device rotation is locked
*/
Expand Down Expand Up @@ -2097,3 +2145,23 @@ extension PHPhotoLibrary {
}
}

extension CameraManager: AVCaptureMetadataOutputObjectsDelegate {
/**
Called when a QR code is detected.
*/
public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
// Check if there is a registered handler.
guard let handler = self.qrCodeDetectionHandler
else { return }

// Get the detection result.
let stringValues = metadataObjects
.compactMap { $0 as? AVMetadataMachineReadableCodeObject }
.compactMap { $0.stringValue }

guard let stringValue = stringValues.first
else { return }

handler(.success(stringValue))
}
}