Skip to content

Commit

Permalink
Merge pull request #131 from qalandarov/main
Browse files Browse the repository at this point in the history
Ability to pause scanning (i.e. when presenting)
  • Loading branch information
nathanfallet authored Feb 24, 2024
2 parents 9302aa1 + 24ffca4 commit 303bd5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Sources/CodeScanner/CodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
public var simulatedData = ""
public var shouldVibrateOnSuccess: Bool
public var isTorchOn: Bool
public var isPaused: Bool
public var isGalleryPresented: Binding<Bool>
public var videoCaptureDevice: AVCaptureDevice?
public var completion: (Result<ScanResult, ScanError>) -> Void
Expand All @@ -99,6 +100,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
simulatedData: String = "",
shouldVibrateOnSuccess: Bool = true,
isTorchOn: Bool = false,
isPaused: Bool = false,
isGalleryPresented: Binding<Bool> = .constant(false),
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo,
completion: @escaping (Result<ScanResult, ScanError>) -> Void
Expand All @@ -112,6 +114,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
self.simulatedData = simulatedData
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
self.isTorchOn = isTorchOn
self.isPaused = isPaused
self.isGalleryPresented = isGalleryPresented
self.videoCaptureDevice = videoCaptureDevice
self.completion = completion
Expand Down
13 changes: 8 additions & 5 deletions Sources/CodeScanner/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import UIKit
@available(macCatalyst 14.0, *)
extension CodeScannerView {

public class ScannerViewController: UIViewController, UINavigationControllerDelegate {
public final class ScannerViewController: UIViewController, UINavigationControllerDelegate {
private let photoOutput = AVCapturePhotoOutput()
private var isCapturing = false
private var handler: ((UIImage?) -> Void)?
Expand Down Expand Up @@ -174,7 +174,7 @@ extension CodeScannerView {
}

private func setupSession() {
guard let captureSession = captureSession else {
guard let captureSession else {
return
}

Expand Down Expand Up @@ -436,9 +436,12 @@ extension CodeScannerView {
extension CodeScannerView.ScannerViewController: AVCaptureMetadataOutputObjectsDelegate {
public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
guard !didFinishScanning && !isCapturing else { return }
guard !parentView.isPaused && !didFinishScanning && !isCapturing,
let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
let stringValue = readableObject.stringValue
else {
return
}

handler = { [self] image in
let result = ScanResult(string: stringValue, type: readableObject.type, image: image, corners: readableObject.corners)
Expand Down

0 comments on commit 303bd5a

Please sign in to comment.