Skip to content

Commit 3d18104

Browse files
committed
[#89] Use the Ultra WIde Camera per default if available
Autofocus on devices that are equipped with an Ultra Wide Camera seems to be off using the default Camera. This is why we select the Ultra Wide Camera per default now.
1 parent d8f10d9 commit 3d18104

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Sources/CodeScanner/CodeScanner.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
8383
shouldVibrateOnSuccess: Bool = true,
8484
isTorchOn: Bool = false,
8585
isGalleryPresented: Binding<Bool> = .constant(false),
86-
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.default(for: .video),
86+
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo,
8787
completion: @escaping (Result<ScanResult, ScanError>) -> Void
8888
) {
8989
self.codeTypes = codeTypes
@@ -115,6 +115,16 @@ public struct CodeScannerView: UIViewControllerRepresentable {
115115

116116
}
117117

118+
public extension AVCaptureDevice {
119+
120+
/// This returns the Ultra Wide Camera on capable devices and the default Camera for Video otherwise.
121+
static var bestForVideo: AVCaptureDevice? {
122+
let deviceHasUltraWideCamera = !AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInUltraWideCamera], mediaType: .video, position: .back).devices.isEmpty
123+
return deviceHasUltraWideCamera ? AVCaptureDevice.default(.builtInUltraWideCamera, for: .video, position: .back) : AVCaptureDevice.default(for: .video)
124+
}
125+
126+
}
127+
118128
@available(macCatalyst 14.0, *)
119129
struct CodeScannerView_Previews: PreviewProvider {
120130
static var previews: some View {

0 commit comments

Comments
 (0)