Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You can provide a variety of extra customization options to `CodeScannerView` in

- `scanMode` can be `.once` to scan a single code, `.oncePerCode` to scan many codes but only trigger finding each unique code once, and `.continuous` will keep finding codes until you dismiss the scanner. Default: `.once`.
- `scanInterval` controls how fast individual codes should be scanned (in seconds) when running in `.continuous` scan mode.
- `zoomFactor` controls zoom of used capture device. By default it is nil to not interfere with videoCaptureDevice.
- `showViewfinder` determines whether to show a box-like viewfinder over the UI. Default: false.
- `simulatedData` allows you to provide some test data to use in Simulator, when real scanning isn’t available. Default: an empty string.
- `shouldVibrateOnSuccess` allows you to determine whether device should vibrate when a code is found. Default: true.
Expand Down
4 changes: 4 additions & 0 deletions Sources/CodeScanner/CodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
public let scanMode: ScanMode
public let manualSelect: Bool
public let scanInterval: Double
public let zoomFactor: CGFloat?
public let showViewfinder: Bool
public let requiresPhotoOutput: Bool
public var simulatedData = ""
Expand All @@ -96,6 +97,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
scanMode: ScanMode = .once,
manualSelect: Bool = false,
scanInterval: Double = 2.0,
zoomFactor: CGFloat? = nil,
showViewfinder: Bool = false,
requiresPhotoOutput: Bool = true,
simulatedData: String = "",
Expand All @@ -112,6 +114,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
self.showViewfinder = showViewfinder
self.requiresPhotoOutput = requiresPhotoOutput
self.scanInterval = scanInterval
self.zoomFactor = zoomFactor
self.simulatedData = simulatedData
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
self.isTorchOn = isTorchOn
Expand All @@ -129,6 +132,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
uiViewController.parentView = self
uiViewController.updateViewController(
isTorchOn: isTorchOn,
zoomFactor: zoomFactor,
isGalleryPresented: isGalleryPresented.wrappedValue,
isManualCapture: scanMode.isManual,
isManualSelect: manualSelect
Expand Down
28 changes: 21 additions & 7 deletions Sources/CodeScanner/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,11 @@ extension CodeScannerView {
}
#endif

func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
func updateViewController(isTorchOn: Bool, zoomFactor: CGFloat?, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else {
return
}

if videoCaptureDevice.hasTorch {
try? videoCaptureDevice.lockForConfiguration()
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
videoCaptureDevice.unlockForConfiguration()
}

if isGalleryPresented, !isGalleryShowing {
openGallery()
}
Expand All @@ -392,6 +386,26 @@ extension CodeScannerView {
showManualCaptureButton(isManualCapture)
showManualSelectButton(isManualSelect)
#endif

let needsZoomUpdate = zoomFactor != nil && videoCaptureDevice.videoZoomFactor != zoomFactor

guard videoCaptureDevice.hasTorch || needsZoomUpdate else { return }

try? videoCaptureDevice.lockForConfiguration()

if videoCaptureDevice.hasTorch {
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
}

if needsZoomUpdate, let zoomFactor {
let newFactor = max(
videoCaptureDevice.minAvailableVideoZoomFactor,
min(videoCaptureDevice.maxAvailableVideoZoomFactor, zoomFactor)
)
videoCaptureDevice.videoZoomFactor = newFactor
}

videoCaptureDevice.unlockForConfiguration()
}

public func reset() {
Expand Down