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

Unreliable torch #85

Closed nitricware closed 1 year ago

nitricware commented 1 year ago

Tested on an iPhone 11 Pro running iOS 16;

With isTorchOn set to true the torch can show three behaviours - none of which is reliable:

  1. The torch will not light up
  2. The torch will light up for a brief moment and then be off again.
  3. The torch will not go out when the View is dismissed.

The View is presented in a Sheet:

.sheet(isPresented: $isShowingScanner) {
    CodeScannerView(
        codeTypes: [.code39],
        simulatedData: "04 ASO SCANTEST123",
        isTorchOn: true,
        completion: handleScan)
}
nathanfallet commented 1 year ago

Can you provide a project (for example a zip archive) to reproduce this issue? I tried in my app and it is not happening...

nitricware commented 1 year ago

Yes, it happens in this project: https://github.com/nitricware/pSTaRT

ClementCardonnel commented 1 year ago

I was experiencing this issue as well. This seems to be related to these stack overflow questions:

However, this only happened when I tried to access my own AVCaptureDevice and enabling the torch myself. @nitricware question made me discover the isTorchOn parameter for CodeScannerView, and after I tried using it I saw the torch working as expected.

I don't know much more about this bug, I'm sorry 😕

nathanfallet commented 1 year ago

Maybe related to #70

marckrenn commented 1 year ago

I think the problem boils down to these LOCs here

let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)

and here

if let backCamera = AVCaptureDevice.default(for: AVMediaType.video),

So the quickfix is to use the exact same AVCaptureDevice on both lines, the proper fix would be to pass explicitly set AVCaptureDevice to the updateViewController-function, I think.

nitricware commented 1 year ago

I changed my code, so that the CodeScannerView gets passed a @State and the torch is off initially. I also implemented a button as suggested in #70 but when I press the button the torch switches on as expected, however the camera freezes.

When I touch the button again, the torch switches off and the camera comes back to life.

Edit: I think i fixed it with #111