Skip to content

Commit

Permalink
Merge pull request #129 from qalandarov/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Fallet authored Feb 23, 2024
2 parents e1943d7 + 4c28fd3 commit 0af227c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Sources/CodeScanner/CodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
public let manualSelect: Bool
public let scanInterval: Double
public let showViewfinder: Bool
public let requirePhotoOutput: Bool
public var simulatedData = ""
public var shouldVibrateOnSuccess: Bool
public var isTorchOn: Bool
Expand All @@ -82,6 +83,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
manualSelect: Bool = false,
scanInterval: Double = 2.0,
showViewfinder: Bool = false,
requirePhotoOutput: Bool = true,
simulatedData: String = "",
shouldVibrateOnSuccess: Bool = true,
isTorchOn: Bool = false,
Expand All @@ -93,6 +95,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
self.scanMode = scanMode
self.manualSelect = manualSelect
self.showViewfinder = showViewfinder
self.requirePhotoOutput = requirePhotoOutput
self.scanInterval = scanInterval
self.simulatedData = simulatedData
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
Expand Down
17 changes: 9 additions & 8 deletions Sources/CodeScanner/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension CodeScannerView {
public class ScannerViewController: UIViewController, UINavigationControllerDelegate {
private let photoOutput = AVCapturePhotoOutput()
private var isCapturing = false
private var handler: ((UIImage) -> Void)?
private var handler: ((UIImage?) -> Void)?
var parentView: CodeScannerView!
var codesFound = Set<String>()
var didFinishScanning = false
Expand Down Expand Up @@ -438,12 +438,7 @@ extension CodeScannerView.ScannerViewController: AVCaptureMetadataOutputObjectsD
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }

guard didFinishScanning == false else { return }

let photoSettings = AVCapturePhotoSettings()
guard !isCapturing else { return }
isCapturing = true
guard !didFinishScanning && !isCapturing else { return }

handler = { [self] image in
let result = ScanResult(string: stringValue, type: readableObject.type, image: image, corners: readableObject.corners)
Expand Down Expand Up @@ -472,7 +467,13 @@ extension CodeScannerView.ScannerViewController: AVCaptureMetadataOutputObjectsD
}
}
}
photoOutput.capturePhoto(with: photoSettings, delegate: self)

if parentView.requirePhotoOutput {
isCapturing = true
photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
} else {
handler?(nil)
}
}
}
}
Expand Down

0 comments on commit 0af227c

Please sign in to comment.