Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Build generated
build/
DerivedData
.build

## Various settings
*.pbxuser
Expand Down
29 changes: 21 additions & 8 deletions Sources/CameraManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,15 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
Stops running capture session but all setup devices, inputs and outputs stay for further reuse.
*/
open func stopCaptureSession() {
captureSession?.stopRunning()
_stopFollowingDeviceOrientation()
if let validCaptureSession = captureSession {
if validCaptureSession.isRunning, cameraIsSetup {
sessionQueue.async { [weak self] in
guard let self = self else { return }
self.captureSession?.stopRunning()
self._stopFollowingDeviceOrientation()
}
}
}
}

/**
Expand Down Expand Up @@ -795,7 +802,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
/**
Start detecting QR codes.
*/
open func startQRCodeDetection(_ handler: @escaping QRCodeDetectionHandler) {
open func startQRCodeDetection(withTypes types: [AVMetadataObject.ObjectType] = [.qr, .ean8, .ean13, .pdf417], _ handler: @escaping QRCodeDetectionHandler) {
guard let captureSession = self.captureSession
else { return }

Expand All @@ -805,11 +812,17 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
else { return }

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].filter { output.availableMetadataObjectTypes.contains($0) }
sessionQueue.async {
captureSession.beginConfiguration()
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)
assert(captureSession.inputs.count ?? 0 > 0, "Error: output.availableMetadataObjectTypes is available only after adding input.")
output.metadataObjectTypes = types.filter { output.availableMetadataObjectTypes.contains($0) }
captureSession.commitConfiguration()
}
qrOutput = output //add this line, for removing when stopped
}

/**
Expand All @@ -832,7 +845,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
/**
The stored meta data output; used to detect QR codes.
*/
private var qrOutput: AVCaptureOutput?
open private(set) var qrOutput: AVCaptureMetadataOutput?

/**
Check if the device rotation is locked
Expand Down