@@ -185,11 +185,11 @@ extension CodeScannerView {
185
185
previewLayer. frame = view. layer. bounds
186
186
previewLayer. videoGravity = . resizeAspectFill
187
187
view. layer. addSublayer ( previewLayer)
188
- addviewfinder ( )
188
+ addViewFinder ( )
189
189
190
190
reset ( )
191
191
192
- if ( captureSession. isRunning == false ) {
192
+ if ! captureSession. isRunning {
193
193
DispatchQueue . global ( qos: . userInteractive) . async {
194
194
self . captureSession? . startRunning ( )
195
195
}
@@ -232,7 +232,7 @@ extension CodeScannerView {
232
232
NotificationCenter . default. addObserver (
233
233
self ,
234
234
selector: #selector( updateOrientation) ,
235
- name: Notification . Name ( " UIDeviceOrientationDidChangeNotification " ) ,
235
+ name: UIDevice . orientationDidChangeNotification ,
236
236
object: nil
237
237
)
238
238
}
@@ -257,17 +257,17 @@ extension CodeScannerView {
257
257
return
258
258
}
259
259
260
- if ( captureSession!. canAddInput ( videoInput) ) {
260
+ if captureSession!. canAddInput ( videoInput) {
261
261
captureSession!. addInput ( videoInput)
262
262
} else {
263
263
didFail ( reason: . badInput)
264
264
return
265
265
}
266
266
let metadataOutput = AVCaptureMetadataOutput ( )
267
267
268
- if ( captureSession!. canAddOutput ( metadataOutput) ) {
268
+ if captureSession!. canAddOutput ( metadataOutput) {
269
269
captureSession!. addOutput ( metadataOutput)
270
- captureSession? . addOutput ( photoOutput)
270
+ captureSession! . addOutput ( photoOutput)
271
271
metadataOutput. setMetadataObjectsDelegate ( self , queue: DispatchQueue . main)
272
272
metadataOutput. metadataObjectTypes = parentView. codeTypes
273
273
} else {
@@ -276,7 +276,7 @@ extension CodeScannerView {
276
276
}
277
277
}
278
278
279
- private func addviewfinder ( ) {
279
+ private func addViewFinder ( ) {
280
280
guard showViewfinder, let imageView = viewFinder else { return }
281
281
282
282
view. addSubview ( imageView)
@@ -292,7 +292,7 @@ extension CodeScannerView {
292
292
override public func viewDidDisappear( _ animated: Bool ) {
293
293
super. viewDidDisappear ( animated)
294
294
295
- if ( captureSession? . isRunning == true ) {
295
+ if captureSession? . isRunning == true {
296
296
DispatchQueue . global ( qos: . userInteractive) . async {
297
297
self . captureSession? . stopRunning ( )
298
298
}
@@ -329,7 +329,7 @@ extension CodeScannerView {
329
329
return
330
330
}
331
331
332
- // Focus to the correct point, make continiuous focus and exposure so the point stays sharp when moving the device closer
332
+ // Focus to the correct point, make continuous focus and exposure so the point stays sharp when moving the device closer
333
333
device. focusPointOfInterest = focusPoint
334
334
device. focusMode = . continuousAutoFocus
335
335
device. exposurePointOfInterest = focusPoint
@@ -383,7 +383,7 @@ extension CodeScannerView {
383
383
videoCaptureDevice. unlockForConfiguration ( )
384
384
}
385
385
386
- if isGalleryPresented && !isGalleryShowing {
386
+ if isGalleryPresented, !isGalleryShowing {
387
387
openGallery ( )
388
388
}
389
389
@@ -405,11 +405,11 @@ extension CodeScannerView {
405
405
lastTime = Date ( )
406
406
}
407
407
408
- func isPastScanInterval( ) -> Bool {
408
+ var isPastScanInterval : Bool {
409
409
Date ( ) . timeIntervalSince ( lastTime) >= parentView. scanInterval
410
410
}
411
411
412
- func isWithinManualCaptureInterval( ) -> Bool {
412
+ var isWithinManualCaptureInterval : Bool {
413
413
Date ( ) . timeIntervalSince ( lastTime) <= 0.5
414
414
}
415
415
@@ -435,53 +435,56 @@ extension CodeScannerView {
435
435
@available ( macCatalyst 14 . 0 , * )
436
436
extension CodeScannerView . ScannerViewController : AVCaptureMetadataOutputObjectsDelegate {
437
437
public func metadataOutput( _ output: AVCaptureMetadataOutput , didOutput metadataObjects: [ AVMetadataObject ] , from connection: AVCaptureConnection ) {
438
- if let metadataObject = metadataObjects. first {
439
- guard !parentView. isPaused && !didFinishScanning && !isCapturing,
440
- let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject ,
441
- let stringValue = readableObject. stringValue
442
- else {
443
- return
444
- }
445
438
446
- handler = { [ self ] image in
447
- let result = ScanResult ( string: stringValue, type: readableObject. type, image: image, corners: readableObject. corners)
448
439
449
- switch parentView. scanMode {
450
- case . once:
440
+ guard let metadataObject = metadataObjects. first,
441
+ !parentView. isPaused,
442
+ !didFinishScanning,
443
+ !isCapturing,
444
+ let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject ,
445
+ let stringValue = readableObject. stringValue else {
446
+
447
+ return
448
+ }
449
+
450
+ handler = { [ self ] image in
451
+ let result = ScanResult ( string: stringValue, type: readableObject. type, image: image, corners: readableObject. corners)
452
+
453
+ switch parentView. scanMode {
454
+ case . once:
455
+ found ( result)
456
+ // make sure we only trigger scan once per use
457
+ didFinishScanning = true
458
+
459
+ case . manual:
460
+ if !didFinishScanning, isWithinManualCaptureInterval {
451
461
found ( result)
452
- // make sure we only trigger scan once per use
453
462
didFinishScanning = true
463
+ }
454
464
455
- case . manual:
456
- if !didFinishScanning, isWithinManualCaptureInterval ( ) {
457
- found ( result)
458
- didFinishScanning = true
459
- }
460
-
461
- case . oncePerCode:
462
- if !codesFound. contains ( stringValue) {
463
- codesFound. insert ( stringValue)
464
- found ( result)
465
- }
465
+ case . oncePerCode:
466
+ if !codesFound. contains ( stringValue) {
467
+ codesFound. insert ( stringValue)
468
+ found ( result)
469
+ }
466
470
467
- case . continuous:
468
- if isPastScanInterval ( ) {
469
- found ( result)
470
- }
471
+ case . continuous:
472
+ if isPastScanInterval {
473
+ found ( result)
474
+ }
471
475
472
- case . continuousExcept( let ignoredList) :
473
- if isPastScanInterval ( ) && !ignoredList. contains ( stringValue) {
474
- found ( result)
475
- }
476
+ case . continuousExcept( let ignoredList) :
477
+ if isPastScanInterval, !ignoredList. contains ( stringValue) {
478
+ found ( result)
476
479
}
477
480
}
481
+ }
478
482
479
- if parentView. requirePhotoOutput {
480
- isCapturing = true
481
- photoOutput. capturePhoto ( with: AVCapturePhotoSettings ( ) , delegate: self )
482
- } else {
483
- handler ? ( nil )
484
- }
483
+ if parentView. requiresPhotoOutput {
484
+ isCapturing = true
485
+ photoOutput. capturePhoto ( with: AVCapturePhotoSettings ( ) , delegate: self )
486
+ } else {
487
+ handler ? ( nil )
485
488
}
486
489
}
487
490
}
@@ -493,40 +496,41 @@ extension CodeScannerView.ScannerViewController: UIImagePickerControllerDelegate
493
496
public func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ UIImagePickerController . InfoKey : Any ] ) {
494
497
isGalleryShowing = false
495
498
496
- if let qrcodeImg = info [ . originalImage] as? UIImage {
497
- let detector = CIDetector ( ofType: CIDetectorTypeQRCode, context: nil , options: [ CIDetectorAccuracy: CIDetectorAccuracyHigh] ) !
498
- let ciImage = CIImage ( image: qrcodeImg) !
499
- var qrCodeLink = " "
500
-
501
- let features = detector. features ( in: ciImage)
502
-
503
- for feature in features as! [ CIQRCodeFeature ] {
504
- qrCodeLink = feature. messageString!
505
- if qrCodeLink == " " {
506
- didFail ( reason: . badOutput)
507
- } else {
508
- let corners = [
509
- feature. bottomLeft,
510
- feature. bottomRight,
511
- feature. topRight,
512
- feature. topLeft
513
- ]
514
- let result = ScanResult ( string: qrCodeLink, type: . qr, image: qrcodeImg, corners: corners)
515
- found ( result)
516
- }
499
+ defer {
500
+ dismiss ( animated: true )
501
+ }
517
502
518
- }
503
+ guard let qrcodeImg = info [ . originalImage] as? UIImage ,
504
+ let detector = CIDetector ( ofType: CIDetectorTypeQRCode, context: nil , options: [ CIDetectorAccuracy: CIDetectorAccuracyHigh] ) ,
505
+ let ciImage = CIImage ( image: qrcodeImg) else {
519
506
520
- } else {
521
- print ( " Something went wrong " )
507
+ return
522
508
}
523
509
524
- dismiss ( animated: true , completion: nil )
510
+ var qrCodeLink = " "
511
+
512
+ let features = detector. features ( in: ciImage)
513
+
514
+ for feature in features as! [ CIQRCodeFeature ] {
515
+ qrCodeLink = feature. messageString!
516
+ if qrCodeLink. isEmpty {
517
+ didFail ( reason: . badOutput)
518
+ } else {
519
+ let corners = [
520
+ feature. bottomLeft,
521
+ feature. bottomRight,
522
+ feature. topRight,
523
+ feature. topLeft
524
+ ]
525
+ let result = ScanResult ( string: qrCodeLink, type: . qr, image: qrcodeImg, corners: corners)
526
+ found ( result)
527
+ }
528
+ }
525
529
}
526
530
527
531
public func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
528
532
isGalleryShowing = false
529
- dismiss ( animated: true , completion : nil )
533
+ dismiss ( animated: true )
530
534
}
531
535
}
532
536
0 commit comments