twostraws / CodeScanner

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

Camera not focusing #113

Closed A-Fairooz closed 8 months ago

A-Fairooz commented 1 year ago

I ran into the issue where my camera wouldn't refocus, I narrowed down the cause to the initialization of the CodeScannerView

 public init(
        codeTypes: [AVMetadataObject.ObjectType],
        scanMode: ScanMode = .once,
        manualSelect: Bool = false,
        scanInterval: Double = 2.0,
        showViewfinder: Bool = false,
        simulatedData: String = "",
        shouldVibrateOnSuccess: Bool = true,
        isTorchOn: Bool = false,
        isGalleryPresented: Binding<Bool> = .constant(false),
        videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo, // <---- THIS
        completion: @escaping (Result<ScanResult, ScanError>) -> Void
    )

When setting the videoCaptureDevice, having it set to bestForVideo on later iPhones sets it to the wide-angle lens and stops you from manually being able to focus, my current fix was replacing it with this:

videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.default(for: .video)