Skip to content

Commit

Permalink
Continue scanning after detecting other object type
Browse files Browse the repository at this point in the history
  • Loading branch information
bgonzales-adsk committed Oct 28, 2021
1 parent d7bbea0 commit 7cf369b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions Sources/Controllers/BarcodeScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ public protocol BarcodeScannerCodeDelegate: class {

/// Delegate to report errors.
public protocol BarcodeScannerErrorDelegate: class {
func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error)
func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: BarcodeScannerError)
}

/// Delegate to dismiss barcode scanner when the close button has been pressed.
public protocol BarcodeScannerDismissalDelegate: class {
func scannerDidDismiss(_ controller: BarcodeScannerViewController)
}

// MARK: - Error types

public enum BarcodeScannerError: Error {
/// Error when something besides a MachineReadableCodeObject was detected. (Check AVMetadataObject.ObjectType documentation)
case nonMachineReadableCodeDetected
/// Error describing an unexpected/general error
case unexpected(Error)
/// Error when a MachineReadableCodeObject was detected but its metadata is unsupported
case unsupported
}

// MARK: - Controller

/**
Expand All @@ -45,6 +56,9 @@ open class BarcodeScannerViewController: UIViewController {
/// Delegate to dismiss barcode scanner when the close button has been pressed.
public weak var dismissalDelegate: BarcodeScannerDismissalDelegate?

/// Stop scanning when other object besides a MachineReadableCodeObject is detected
public var stopCaptureWhenDetectingOtherObject = true

/// `AVCaptureMetadataOutput` metadata object types.
public var metadata = AVMetadataObject.ObjectType.barcodeScannerMetadata {
didSet {
Expand Down Expand Up @@ -236,7 +250,7 @@ extension BarcodeScannerViewController: CameraViewControllerDelegate {
}

func cameraViewController(_ controller: CameraViewController, didReceiveError error: Error) {
errorDelegate?.scanner(self, didReceiveError: error)
errorDelegate?.scanner(self, didReceiveError: .unexpected(error))
}

func cameraViewControllerDidTapSettingsButton(_ controller: CameraViewController) {
Expand All @@ -251,15 +265,17 @@ extension BarcodeScannerViewController: CameraViewControllerDelegate {
didOutput metadataObjects: [AVMetadataObject]) {
guard !locked && isVisible else { return }
guard !metadataObjects.isEmpty else { return }
guard let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject else {
if self.stopCaptureWhenDetectingOtherObject { controller.stopCapturing() }
errorDelegate?.scanner(self, didReceiveError: .nonMachineReadableCodeDetected)
return
}

controller.stopCapturing()

guard
let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject,
var code = metadataObj.stringValue,
metadata.contains(metadataObj.type)
else {
errorDelegate?.scanner(self, didReceiveError: NSError())
return

guard var code = metadataObj.stringValue, metadata.contains(metadataObj.type) else {
errorDelegate?.scanner(self, didReceiveError: .unsupported)
return
}

var rawType = metadataObj.type.rawValue
Expand Down

0 comments on commit 7cf369b

Please sign in to comment.