@@ -12,7 +12,7 @@ import UIKit
12
12
@available ( macCatalyst 14 . 0 , * )
13
13
extension CodeScannerView {
14
14
15
- public class ScannerViewController : UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate , AVCaptureMetadataOutputObjectsDelegate , UIAdaptivePresentationControllerDelegate {
15
+ public class ScannerViewController : UIViewController , UINavigationControllerDelegate {
16
16
private let photoOutput = AVCapturePhotoOutput ( )
17
17
private var isCapturing = false
18
18
private var handler : ( ( UIImage ) -> Void ) ?
@@ -56,50 +56,6 @@ extension CodeScannerView {
56
56
openGallery ( )
57
57
}
58
58
59
- public func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ UIImagePickerController . InfoKey : Any ] ) {
60
- isGalleryShowing = false
61
-
62
- if let qrcodeImg = info [ . originalImage] as? UIImage {
63
- let detector = CIDetector ( ofType: CIDetectorTypeQRCode, context: nil , options: [ CIDetectorAccuracy: CIDetectorAccuracyHigh] ) !
64
- let ciImage = CIImage ( image: qrcodeImg) !
65
- var qrCodeLink = " "
66
-
67
- let features = detector. features ( in: ciImage)
68
-
69
- for feature in features as! [ CIQRCodeFeature ] {
70
- qrCodeLink = feature. messageString!
71
- if qrCodeLink == " " {
72
- didFail ( reason: . badOutput)
73
- } else {
74
- let corners = [
75
- feature. bottomLeft,
76
- feature. bottomRight,
77
- feature. topRight,
78
- feature. topLeft
79
- ]
80
- let result = ScanResult ( string: qrCodeLink, type: . qr, image: qrcodeImg, corners: corners)
81
- found ( result)
82
- }
83
-
84
- }
85
-
86
- } else {
87
- print ( " Something went wrong " )
88
- }
89
-
90
- dismiss ( animated: true , completion: nil )
91
- }
92
-
93
- public func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
94
- isGalleryShowing = false
95
- dismiss ( animated: true , completion: nil )
96
- }
97
-
98
- public func presentationControllerDidDismiss( _ presentationController: UIPresentationController ) {
99
- // Gallery is no longer being presented
100
- isGalleryShowing = false
101
- }
102
-
103
59
#if targetEnvironment(simulator)
104
60
override public func loadView( ) {
105
61
view = UIView ( )
@@ -449,48 +405,6 @@ extension CodeScannerView {
449
405
lastTime = Date ( )
450
406
}
451
407
452
- public func metadataOutput( _ output: AVCaptureMetadataOutput , didOutput metadataObjects: [ AVMetadataObject ] , from connection: AVCaptureConnection ) {
453
- if let metadataObject = metadataObjects. first {
454
- guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
455
- guard let stringValue = readableObject. stringValue else { return }
456
-
457
- guard didFinishScanning == false else { return }
458
-
459
- let photoSettings = AVCapturePhotoSettings ( )
460
- guard !isCapturing else { return }
461
- isCapturing = true
462
-
463
- handler = { [ self ] image in
464
- let result = ScanResult ( string: stringValue, type: readableObject. type, image: image, corners: readableObject. corners)
465
-
466
- switch parentView. scanMode {
467
- case . once:
468
- found ( result)
469
- // make sure we only trigger scan once per use
470
- didFinishScanning = true
471
-
472
- case . manual:
473
- if !didFinishScanning, isWithinManualCaptureInterval ( ) {
474
- found ( result)
475
- didFinishScanning = true
476
- }
477
-
478
- case . oncePerCode:
479
- if !codesFound. contains ( stringValue) {
480
- codesFound. insert ( stringValue)
481
- found ( result)
482
- }
483
-
484
- case . continuous:
485
- if isPastScanInterval ( ) {
486
- found ( result)
487
- }
488
- }
489
- }
490
- photoOutput. capturePhoto ( with: photoSettings, delegate: self )
491
- }
492
- }
493
-
494
408
func isPastScanInterval( ) -> Bool {
495
409
Date ( ) . timeIntervalSince ( lastTime) >= parentView. scanInterval
496
410
}
@@ -516,6 +430,109 @@ extension CodeScannerView {
516
430
}
517
431
}
518
432
433
+ // MARK: - AVCaptureMetadataOutputObjectsDelegate
434
+
435
+ @available ( macCatalyst 14 . 0 , * )
436
+ extension CodeScannerView . ScannerViewController : AVCaptureMetadataOutputObjectsDelegate {
437
+ public func metadataOutput( _ output: AVCaptureMetadataOutput , didOutput metadataObjects: [ AVMetadataObject ] , from connection: AVCaptureConnection ) {
438
+ if let metadataObject = metadataObjects. first {
439
+ guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
440
+ guard let stringValue = readableObject. stringValue else { return }
441
+
442
+ guard didFinishScanning == false else { return }
443
+
444
+ let photoSettings = AVCapturePhotoSettings ( )
445
+ guard !isCapturing else { return }
446
+ isCapturing = true
447
+
448
+ handler = { [ self ] image in
449
+ let result = ScanResult ( string: stringValue, type: readableObject. type, image: image, corners: readableObject. corners)
450
+
451
+ switch parentView. scanMode {
452
+ case . once:
453
+ found ( result)
454
+ // make sure we only trigger scan once per use
455
+ didFinishScanning = true
456
+
457
+ case . manual:
458
+ if !didFinishScanning, isWithinManualCaptureInterval ( ) {
459
+ found ( result)
460
+ didFinishScanning = true
461
+ }
462
+
463
+ case . oncePerCode:
464
+ if !codesFound. contains ( stringValue) {
465
+ codesFound. insert ( stringValue)
466
+ found ( result)
467
+ }
468
+
469
+ case . continuous:
470
+ if isPastScanInterval ( ) {
471
+ found ( result)
472
+ }
473
+ }
474
+ }
475
+ photoOutput. capturePhoto ( with: photoSettings, delegate: self )
476
+ }
477
+ }
478
+ }
479
+
480
+ // MARK: - UIImagePickerControllerDelegate
481
+
482
+ @available ( macCatalyst 14 . 0 , * )
483
+ extension CodeScannerView . ScannerViewController : UIImagePickerControllerDelegate {
484
+ public func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ UIImagePickerController . InfoKey : Any ] ) {
485
+ isGalleryShowing = false
486
+
487
+ if let qrcodeImg = info [ . originalImage] as? UIImage {
488
+ let detector = CIDetector ( ofType: CIDetectorTypeQRCode, context: nil , options: [ CIDetectorAccuracy: CIDetectorAccuracyHigh] ) !
489
+ let ciImage = CIImage ( image: qrcodeImg) !
490
+ var qrCodeLink = " "
491
+
492
+ let features = detector. features ( in: ciImage)
493
+
494
+ for feature in features as! [ CIQRCodeFeature ] {
495
+ qrCodeLink = feature. messageString!
496
+ if qrCodeLink == " " {
497
+ didFail ( reason: . badOutput)
498
+ } else {
499
+ let corners = [
500
+ feature. bottomLeft,
501
+ feature. bottomRight,
502
+ feature. topRight,
503
+ feature. topLeft
504
+ ]
505
+ let result = ScanResult ( string: qrCodeLink, type: . qr, image: qrcodeImg, corners: corners)
506
+ found ( result)
507
+ }
508
+
509
+ }
510
+
511
+ } else {
512
+ print ( " Something went wrong " )
513
+ }
514
+
515
+ dismiss ( animated: true , completion: nil )
516
+ }
517
+
518
+ public func imagePickerControllerDidCancel( _ picker: UIImagePickerController ) {
519
+ isGalleryShowing = false
520
+ dismiss ( animated: true , completion: nil )
521
+ }
522
+ }
523
+
524
+ // MARK: - UIAdaptivePresentationControllerDelegate
525
+
526
+ @available ( macCatalyst 14 . 0 , * )
527
+ extension CodeScannerView . ScannerViewController : UIAdaptivePresentationControllerDelegate {
528
+ public func presentationControllerDidDismiss( _ presentationController: UIPresentationController ) {
529
+ // Gallery is no longer being presented
530
+ isGalleryShowing = false
531
+ }
532
+ }
533
+
534
+ // MARK: - AVCapturePhotoCaptureDelegate
535
+
519
536
@available ( macCatalyst 14 . 0 , * )
520
537
extension CodeScannerView . ScannerViewController : AVCapturePhotoCaptureDelegate {
521
538
@@ -552,6 +569,8 @@ extension CodeScannerView.ScannerViewController: AVCapturePhotoCaptureDelegate {
552
569
553
570
}
554
571
572
+ // MARK: - AVCaptureDevice
573
+
555
574
@available ( macCatalyst 14 . 0 , * )
556
575
public extension AVCaptureDevice {
557
576
0 commit comments