@@ -13,7 +13,9 @@ import UIKit
13
13
extension CodeScannerView {
14
14
15
15
public class ScannerViewController : UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate , AVCaptureMetadataOutputObjectsDelegate {
16
-
16
+ private let photoOutput = AVCapturePhotoOutput ( )
17
+ private var isCapturing = false
18
+ private var handler : ( ( UIImage ) -> Void ) ?
17
19
var parentView : CodeScannerView !
18
20
var codesFound = Set < String > ( )
19
21
var didFinishScanning = false
@@ -68,7 +70,7 @@ extension CodeScannerView {
68
70
if qrCodeLink == " " {
69
71
didFail ( reason: . badOutput)
70
72
} else {
71
- let result = ScanResult ( string: qrCodeLink, type: . qr)
73
+ let result = ScanResult ( string: qrCodeLink, type: . qr, image : qrcodeImg )
72
74
found ( result)
73
75
}
74
76
} else {
@@ -122,7 +124,7 @@ extension CodeScannerView {
122
124
// Send back their simulated data, as if it was one of the types they were scanning for
123
125
found ( ScanResult (
124
126
string: parentView. simulatedData,
125
- type: parentView. codeTypes. first ?? . qr
127
+ type: parentView. codeTypes. first ?? . qr, image : nil
126
128
) )
127
129
}
128
130
@@ -280,12 +282,11 @@ extension CodeScannerView {
280
282
didFail ( reason: . badInput)
281
283
return
282
284
}
283
-
284
285
let metadataOutput = AVCaptureMetadataOutput ( )
285
286
286
287
if ( captureSession!. canAddOutput ( metadataOutput) ) {
287
288
captureSession!. addOutput ( metadataOutput)
288
-
289
+ captureSession ? . addOutput ( photoOutput )
289
290
metadataOutput. setMetadataObjectsDelegate ( self , queue: DispatchQueue . main)
290
291
metadataOutput. metadataObjectTypes = parentView. codeTypes
291
292
} else {
@@ -425,32 +426,41 @@ extension CodeScannerView {
425
426
if let metadataObject = metadataObjects. first {
426
427
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
427
428
guard let stringValue = readableObject. stringValue else { return }
429
+
428
430
guard didFinishScanning == false else { return }
429
- let result = ScanResult ( string: stringValue, type: readableObject. type)
430
-
431
- switch parentView. scanMode {
432
- case . once:
433
- found ( result)
434
- // make sure we only trigger scan once per use
435
- didFinishScanning = true
436
-
437
- case . manual:
438
- if !didFinishScanning, isWithinManualCaptureInterval ( ) {
439
- found ( result)
440
- didFinishScanning = true
441
- }
431
+
432
+ let photoSettings = AVCapturePhotoSettings ( )
433
+ guard !isCapturing else { return }
434
+ isCapturing = true
435
+
436
+ handler = { [ self ] image in
437
+ let result = ScanResult ( string: stringValue, type: readableObject. type, image: image)
442
438
443
- case . oncePerCode:
444
- if !codesFound. contains ( stringValue) {
445
- codesFound. insert ( stringValue)
446
- found ( result)
447
- }
448
-
449
- case . continuous:
450
- if isPastScanInterval ( ) {
439
+ switch parentView. scanMode {
440
+ case . once:
451
441
found ( result)
442
+ // make sure we only trigger scan once per use
443
+ didFinishScanning = true
444
+
445
+ case . manual:
446
+ if !didFinishScanning, isWithinManualCaptureInterval ( ) {
447
+ found ( result)
448
+ didFinishScanning = true
449
+ }
450
+
451
+ case . oncePerCode:
452
+ if !codesFound. contains ( stringValue) {
453
+ codesFound. insert ( stringValue)
454
+ found ( result)
455
+ }
456
+
457
+ case . continuous:
458
+ if isPastScanInterval ( ) {
459
+ found ( result)
460
+ }
452
461
}
453
462
}
463
+ photoOutput. capturePhoto ( with: photoSettings, delegate: self )
454
464
}
455
465
}
456
466
@@ -478,3 +488,17 @@ extension CodeScannerView {
478
488
479
489
}
480
490
}
491
+ extension CodeScannerView . ScannerViewController : AVCapturePhotoCaptureDelegate {
492
+ public func photoOutput( _ output: AVCapturePhotoOutput , didFinishProcessingPhoto photo: AVCapturePhoto , error: Error ? ) {
493
+ isCapturing = false
494
+ guard let imageData = photo. fileDataRepresentation ( ) else {
495
+ print ( " Error while generating image from photo capture data. " ) ;
496
+ return
497
+ }
498
+ guard let qrImage = UIImage ( data: imageData) else {
499
+ print ( " Unable to generate UIImage from image data. " ) ;
500
+ return
501
+ }
502
+ handler ? ( qrImage)
503
+ }
504
+ }
0 commit comments