Skip to content

Commit

Permalink
Merge pull request #4 from plangrid/bgonzales/continue_scanning
Browse files Browse the repository at this point in the history
Continue or stop scanning when an object other than a barcode is detected
  • Loading branch information
bgonzales-adsk authored Oct 29, 2021
2 parents d7bbea0 + 1855362 commit f4e64d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion BarcodeScanner.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "BarcodeScanner"
s.summary = "Simple and beautiful barcode scanner."
s.version = "5.0.4"
s.version = "5.0.5"
s.homepage = "https://github.com/hyperoslo/BarcodeScanner"
s.license = 'MIT'
s.author = { "Hyper Interaktiv AS" => "ios@hyper.no" }
Expand Down
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 f4e64d2

Please sign in to comment.