twostraws / CodeScanner

A SwiftUI view that is able to scan barcodes, QR codes, and more, and send back what was found.
MIT License
1.01k stars 286 forks source link

Add a way to restart a scanning session #66

Closed charpple closed 2 years ago

charpple commented 2 years ago

Right now, I couldn't find a way to restart a new scanning session, other than using the continuous option or making a new instance of the class.

j-cheung commented 2 years ago

ive made some local changes to the package to kind of enable that, if it helps, there should definitely be better options.

ScannerViewController.swift

        func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, toReset: Bool) { // new toReset binding
            if let backCamera = AVCaptureDevice.default(for: AVMediaType.video),
               backCamera.hasTorch
            {
                try? backCamera.lockForConfiguration()
                backCamera.torchMode = isTorchOn ? .on : .off
                backCamera.unlockForConfiguration()
            }

            if isGalleryPresented && !isGalleryShowing {
                openGallery()
            }
            if toReset { // if toReset, use reset function in ScannerCoordinator.swift
                delegate?.reset()
                delegate?.parent.toReset.wrappedValue = false
            }
        }

CodeScanner.swift

    public var isGalleryPresented: Binding<Bool>
    public var toReset: Binding<Bool> // new binding variable
    public var videoCaptureDevice: AVCaptureDevice?

...

        isGalleryPresented: Binding<Bool> = .constant(false),
        toReset: Binding<Bool> = .constant(false), // in init function
        videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.default(for: .video),

...

        self.isGalleryPresented = isGalleryPresented
        self.toReset = toReset
        self.videoCaptureDevice = videoCaptureDevice

...

  public func updateUIViewController(_ uiViewController: ScannerViewController, context: Context) {
        uiViewController.updateViewController(
            isTorchOn: isTorchOn,
            isGalleryPresented: isGalleryPresented.wrappedValue,
            toReset: toReset.wrappedValue // added this
        )
    }

Followed this to temporarily overwrite with local changes https://developer.apple.com/documentation/swift_packages/editing_a_package_dependency_as_a_local_package

Sat1l commented 2 years ago

@charpple @j-cheung Tip: there are many cool functions in this package, unfortunately Paul is shy to write about them :( If you look CodeScanner.swift, there are many params like shouldVibrateOnSuccess ,isTorchOn, and even completion though I haven't figured it out how the last one works. But the one you need is scanMode, and when using you can choose from .continuous,.once and .oncePerCode. Seems like for your case .continuous will be the right one. This is how my code with specified scan mode looks

CodeScannerView(codeTypes: [.qr], scanMode: .continuous, simulatedData: "Paul Hudson") { response in
            switch response {
            case .success(let result):
                print("Found code: \(result.string)")
            case .failure(let error):
                print(error.localizedDescription)
            }

And using scanInterval you can specify the time in seconds between the scans (it takes double so you can specify like 0.5 seconds and so on) In this example I have 10 seconds delay between the scans

CodeScannerView(codeTypes: [.code39], scanMode: .continuous, scanInterval: 10 ,simulatedData: "Paul Hudson") { response in
    switch response {
    case .success(let result):
        print("Found code: \(result.string)")
    case .failure(let error):
        print(error.localizedDescription)
    }
}

Hope this helps!

twostraws commented 2 years ago

@Sat1l I'm not too shy, I'm too busy – have you considered perhaps contributing some documentation in the README, given that this is an open source project?

Sat1l commented 2 years ago

@twostraws seems like fun, just give me some time :) Edit: seems like I'm the one who wasn't reading repo info carefully and there is an actual guidance on .scanmode

nathanfallet commented 2 years ago

Closing as it seems to be solved. If not, please reopen and give details about what you're still struggling with.