This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
MTBBarcodeScanner.m
1021 lines (818 loc) · 36.5 KB
/
MTBBarcodeScanner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MTBBarcodeScanner.m
// MTBBarcodeScannerExample
//
// Created by Mike Buss on 2/8/14.
//
//
#import <QuartzCore/QuartzCore.h>
#import "MTBBarcodeScanner.h"
CGFloat const kFocalPointOfInterestX = 0.5;
CGFloat const kFocalPointOfInterestY = 0.5;
static NSString *kErrorDomain = @"MTBBarcodeScannerError";
// Error Codes
static const NSInteger kErrorCodeStillImageCaptureInProgress = 1000;
static const NSInteger kErrorCodeSessionIsClosed = 1001;
static const NSInteger kErrorCodeNotScanning = 1002;
static const NSInteger kErrorCodeSessionAlreadyActive = 1003;
static const NSInteger kErrorCodeTorchModeUnavailable = 1004;
static const NSInteger kErrorMethodNotAvailableOnIOSVersion = 1005;
@interface MTBBarcodeScanner () <AVCaptureMetadataOutputObjectsDelegate, AVCapturePhotoCaptureDelegate>
/*!
@property privateSessionQueue
@abstract
Starting or stopping the capture session should only be done on this queue.
*/
@property (strong) dispatch_queue_t privateSessionQueue;
/*!
@property session
@abstract
The capture session used for scanning barcodes.
*/
@property (nonatomic, strong) AVCaptureSession *session;
/*!
@property captureDevice
@abstract
Represents the physical device that is used for scanning barcodes.
*/
@property (nonatomic, strong) AVCaptureDevice *captureDevice;
/*!
@property capturePreviewLayer
@abstract
The layer used to view the camera input. This layer is added to the
previewView when scanning starts.
*/
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *capturePreviewLayer;
/*!
@property currentCaptureDeviceInput
@abstract
The current capture device input for capturing video. This is used
to reset the camera to its initial properties when scanning stops.
*/
@property (nonatomic, strong) AVCaptureDeviceInput *currentCaptureDeviceInput;
/*
@property captureDeviceOnput
@abstract
The capture device output for capturing video.
*/
@property (nonatomic, strong) AVCaptureMetadataOutput *captureOutput;
/*!
@property metaDataObjectTypes
@abstract
The MetaDataObjectTypes to look for in the scanning session.
@discussion
Only objects with a MetaDataObjectType found in this array will be
reported to the result block.
*/
@property (nonatomic, copy) NSArray<NSString *> *metaDataObjectTypes;
/*!
@property previewView
@abstract
The view used to preview the camera input.
@discussion
The AVCaptureVideoPreviewLayer is added to this view to preview the
camera input when scanning starts. When scanning stops, the layer is
removed.
*/
@property (nonatomic, weak) UIView *previewView;
/*!
@property initialAutoFocusRangeRestriction
@abstract
The auto focus range restriction the AVCaptureDevice was initially configured for when scanning started.
@discussion
When startScanning is called, the auto focus range restriction of the default AVCaptureDevice
is stored. When stopScanning is called, the AVCaptureDevice is reset to the initial range restriction
to prevent a bug in the AVFoundation framework.
*/
@property (nonatomic, assign) AVCaptureAutoFocusRangeRestriction initialAutoFocusRangeRestriction;
/*!
@property initialFocusPoint
@abstract
The focus point the AVCaptureDevice was initially configured for when scanning started.
@discussion
When startScanning is called, the focus point of the default AVCaptureDevice
is stored. When stopScanning is called, the AVCaptureDevice is reset to the initial focal point
to prevent a bug in the AVFoundation framework.
*/
@property (nonatomic, assign) CGPoint initialFocusPoint;
/*!
@property stillImageOutput
@abstract
Used for still image capture prior to iOS 10
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
@property (nonatomic, strong) AVCaptureStillImageOutput *stillImageOutput;
#pragma GCC diagnostic pop
/*!
@property gestureRecognizer
@abstract
If allowTapToFocus is set to YES, this gesture recognizer is added to the `previewView`
when scanning starts. When the user taps the view, the `focusPointOfInterest` will change
to the location the user tapped.
*/
@property (nonatomic, strong) UITapGestureRecognizer *gestureRecognizer;
/*!
@property stillImageCaptureBlock
@abstract
Reference to the block passed in when capturing a still image.
*/
@property (nonatomic, copy) void (^stillImageCaptureBlock)(UIImage *image, NSError *error);
/*!
@property output
@abstract
Property used for capturing still photos during barcode capture.
*/
@property (nonatomic, strong) AVCapturePhotoOutput *output NS_AVAILABLE_IOS(10.0);
@end
@implementation MTBBarcodeScanner
#pragma mark - Lifecycle
- (instancetype)init {
NSAssert(NO, @"MTBBarcodeScanner init is not supported. Please use initWithPreviewView: \
or initWithMetadataObjectTypes:previewView: to instantiate a MTBBarcodeScanner");
return nil;
}
- (instancetype)initWithPreviewView:(UIView *)previewView {
return [self initWithMetadataObjectTypes:[self defaultMetaDataObjectTypes] previewView:previewView];
}
- (instancetype)initWithMetadataObjectTypes:(NSArray<NSString *> *)metaDataObjectTypes previewView:(UIView *)previewView {
NSParameterAssert(metaDataObjectTypes);
NSAssert(metaDataObjectTypes.count > 0,
@"Must initialize MTBBarcodeScanner with at least one metaDataObjectTypes value.");
self = [super init];
if (self) {
// Library does not support scanning for faces
NSAssert(!([metaDataObjectTypes indexOfObject:AVMetadataObjectTypeFace] != NSNotFound),
@"The type %@ is not supported by MTBBarcodeScanner.", AVMetadataObjectTypeFace);
_metaDataObjectTypes = metaDataObjectTypes;
_previewView = previewView;
_allowTapToFocus = YES;
_preferredAutoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
[self setupSessionQueue];
[self addObservers];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Scanning
+ (BOOL)cameraIsPresent {
// capture device is nil if status is AVAuthorizationStatusRestricted
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] != nil;
}
+ (BOOL)hasCamera:(MTBCamera)camera {
AVCaptureDevicePosition position = [self devicePositionForCamera:camera];
if (@available(iOS 10.0, *)) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera
mediaType:AVMediaTypeVideo
position:position];
return (device != nil);
} else {
// We can ignore the deprecation warning here because
// we are using the new AVCaptureDeviceDiscoverySession when it is available
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// Array is empty if status is AVAuthorizationStatusRestricted
for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if (device.position == position) {
return YES;
}
}
#pragma GCC diagnostic pop
}
return NO;
}
+ (MTBCamera)oppositeCameraOf:(MTBCamera)camera {
switch (camera) {
case MTBCameraBack:
return MTBCameraFront;
case MTBCameraFront:
return MTBCameraBack;
}
NSAssert(NO, @"Invalid camera type: %lu", (unsigned long)camera);
return MTBCameraBack;
}
+ (BOOL)scanningIsProhibited {
switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) {
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
return YES;
break;
default:
return NO;
break;
}
}
+ (void)requestCameraPermissionWithSuccess:(void (^)(BOOL success))successBlock {
if (![self cameraIsPresent]) {
successBlock(NO);
return;
}
switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) {
case AVAuthorizationStatusAuthorized:
successBlock(YES);
break;
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
successBlock(NO);
break;
case AVAuthorizationStatusNotDetermined:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
successBlock(granted);
});
}];
break;
}
}
- (BOOL)startScanningWithError:(NSError **)error {
return [self startScanningWithResultBlock:self.resultBlock error:error];
}
- (BOOL)startScanningWithResultBlock:(void (^)(NSArray<AVMetadataMachineReadableCodeObject *> *codes))resultBlock error:(NSError **)error {
return [self startScanningWithCamera:MTBCameraBack resultBlock:resultBlock error:error];
}
- (BOOL)startScanningWithCamera:(MTBCamera)camera resultBlock:(void (^)(NSArray<AVMetadataMachineReadableCodeObject *> *codes))resultBlock error:(NSError **)error {
NSAssert([MTBBarcodeScanner cameraIsPresent], @"Attempted to start scanning on a device with no camera. Check requestCameraPermissionWithSuccess: method before calling startScanningWithResultBlock:");
NSAssert(![MTBBarcodeScanner scanningIsProhibited], @"Scanning is prohibited on this device. \
Check requestCameraPermissionWithSuccess: method before calling startScanningWithResultBlock:");
NSAssert(resultBlock, @"startScanningWithResultBlock: requires a non-nil resultBlock.");
if (self.session) {
if (error) {
*error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeSessionAlreadyActive
userInfo:@{NSLocalizedDescriptionKey : @"Do not start scanning while another session is in use."}];
}
return NO;
}
// Configure the session
_camera = camera;
self.captureDevice = [self newCaptureDeviceWithCamera:self.camera];
AVCaptureSession *session = [self newSessionWithCaptureDevice:self.captureDevice error:error];
if (!session) {
// we rely on newSessionWithCaptureDevice:error: to populate the error
return NO;
}
self.session = session;
// Configure the preview layer
self.capturePreviewLayer.cornerRadius = self.previewView.layer.cornerRadius;
[self.previewView.layer insertSublayer:self.capturePreviewLayer atIndex:0]; // Insert below all other views
[self refreshVideoOrientation];
// Configure 'tap to focus' functionality
[self configureTapToFocus];
self.resultBlock = resultBlock;
dispatch_async(self.privateSessionQueue, ^{
// Configure the rect of interest
self.captureOutput.rectOfInterest = [self rectOfInterestFromScanRect:self.scanRect];
// Start the session after all configurations:
// Must be dispatched as it is blocking
[self.session startRunning];
if (self.didStartScanningBlock) {
// Call that block now that we've started scanning:
// Dispatch back to main
dispatch_async(dispatch_get_main_queue(), ^{
self.didStartScanningBlock();
});
}
});
return YES;
}
- (void)stopScanning {
if (!self.session) {
return;
}
// Turn the torch off
self.torchMode = MTBTorchModeOff;
// Remove the preview layer
[self.capturePreviewLayer removeFromSuperlayer];
// Stop recognizing taps for the 'Tap to Focus' feature
[self stopRecognizingTaps];
self.resultBlock = nil;
self.capturePreviewLayer.session = nil;
self.capturePreviewLayer = nil;
AVCaptureSession *session = self.session;
AVCaptureDeviceInput *deviceInput = self.currentCaptureDeviceInput;
self.session = nil;
dispatch_async(self.privateSessionQueue, ^{
// When we're finished scanning, reset the settings for the camera
// to their original states
// Must be dispatched as it is blocking
[self removeDeviceInput:deviceInput session:session];
for (AVCaptureOutput *output in session.outputs) {
[session removeOutput:output];
}
// Must be dispatched as it is blocking
[session stopRunning];
});
}
- (BOOL)isScanning {
return [self.session isRunning];
}
- (BOOL)hasOppositeCamera {
MTBCamera otherCamera = [[self class] oppositeCameraOf:self.camera];
return [[self class] hasCamera:otherCamera];
}
- (void)flipCamera {
[self flipCameraWithError:nil];
}
- (BOOL)flipCameraWithError:(NSError **)error {
if (!self.isScanning) {
if (error) {
*error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeNotScanning
userInfo:@{NSLocalizedDescriptionKey : @"Camera cannot be flipped when isScanning is NO"}];
}
return NO;
}
MTBCamera otherCamera = [[self class] oppositeCameraOf:self.camera];
return [self setCamera:otherCamera error:error];
}
#pragma mark - Tap to Focus
- (void)configureTapToFocus {
if (self.allowTapToFocus) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(focusTapped:)];
[self.previewView addGestureRecognizer:tapGesture];
self.gestureRecognizer = tapGesture;
}
}
- (void)focusTapped:(UITapGestureRecognizer *)tapGesture {
CGPoint tapPoint = [self.gestureRecognizer locationInView:self.gestureRecognizer.view];
CGPoint devicePoint = [self.capturePreviewLayer captureDevicePointOfInterestForPoint:tapPoint];
AVCaptureDevice *device = self.captureDevice;
NSError *error = nil;
if ([device lockForConfiguration:&error]) {
if (device.isFocusPointOfInterestSupported &&
[device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
device.focusPointOfInterest = devicePoint;
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
}
[device unlockForConfiguration];
} else {
NSLog(@"Failed to acquire lock for focus change: %@", error);
}
if (self.didTapToFocusBlock) {
self.didTapToFocusBlock(tapPoint);
}
}
- (void)stopRecognizingTaps {
if (self.gestureRecognizer) {
[self.previewView removeGestureRecognizer:self.gestureRecognizer];
}
}
#pragma mark - AVCaptureMetadataOutputObjects Delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if (!self.resultBlock) return;
NSMutableArray *codes = [[NSMutableArray alloc] init];
for (AVMetadataObject *metaData in metadataObjects) {
AVMetadataMachineReadableCodeObject *barCodeObject = (AVMetadataMachineReadableCodeObject *)[self.capturePreviewLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metaData];
if (barCodeObject) {
[codes addObject:barCodeObject];
}
}
self.resultBlock(codes);
}
#pragma mark - Rotation
- (void)handleApplicationDidChangeStatusBarNotification:(NSNotification *)notification {
[self refreshVideoOrientation];
}
- (void)refreshVideoOrientation {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
self.capturePreviewLayer.frame = self.previewView.bounds;
if ([self.capturePreviewLayer.connection isVideoOrientationSupported]) {
self.capturePreviewLayer.connection.videoOrientation = [self captureOrientationForInterfaceOrientation:orientation];
}
}
- (AVCaptureVideoOrientation)captureOrientationForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
return AVCaptureVideoOrientationPortrait;
}
}
#pragma mark - Background Handling
- (void)applicationWillEnterForegroundNotification:(NSNotification *)notification {
// the torch is switched off when the app is backgrounded so we restore the
// previous state once the app is foregrounded again
[self updateForTorchMode:self.torchMode error:nil];
}
#pragma mark - Session Configuration
- (AVCaptureSession *)newSessionWithCaptureDevice:(AVCaptureDevice *)captureDevice error:(NSError **)error {
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:error];
if (!input) {
// we rely on deviceInputWithDevice:error: to populate the error
return nil;
}
AVCaptureSession *newSession = [[AVCaptureSession alloc] init];
[self setDeviceInput:input session:newSession];
// Set an optimized preset for barcode scanning
[newSession setSessionPreset:AVCaptureSessionPresetHigh];
self.captureOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[newSession addOutput:self.captureOutput];
self.captureOutput.metadataObjectTypes = self.metaDataObjectTypes;
[newSession beginConfiguration];
if (@available(iOS 10.0, *)) {
self.output = [[AVCapturePhotoOutput alloc] init];
self.output.highResolutionCaptureEnabled = YES;
if ([newSession canAddOutput:self.output]) {
[newSession addOutput:self.output];
}
} else {
// Still image capture configuration
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
self.stillImageOutput.outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};
if ([self.stillImageOutput isStillImageStabilizationSupported]) {
self.stillImageOutput.automaticallyEnablesStillImageStabilizationWhenAvailable = YES;
}
if ([self.stillImageOutput respondsToSelector:@selector(isHighResolutionStillImageOutputEnabled)]) {
self.stillImageOutput.highResolutionStillImageOutputEnabled = YES;
}
[newSession addOutput:self.stillImageOutput];
#pragma GCC diagnostic pop
}
dispatch_async(self.privateSessionQueue, ^{
self.captureOutput.rectOfInterest = [self rectOfInterestFromScanRect:self.scanRect];
});
self.capturePreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:newSession];
self.capturePreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.capturePreviewLayer.frame = self.previewView.bounds;
[newSession commitConfiguration];
return newSession;
}
- (AVCaptureDevice *)newCaptureDeviceWithCamera:(MTBCamera)camera {
AVCaptureDevice *newCaptureDevice = nil;
AVCaptureDevicePosition position = [[self class] devicePositionForCamera:camera];
if (@available(iOS 10.0, *)) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera
mediaType:AVMediaTypeVideo
position:position];
newCaptureDevice = device;
} else {
// We can ignore the deprecation here because we are using
// AVCaptureDeviceDiscoverySession if it is available
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in videoDevices) {
if (device.position == position) {
newCaptureDevice = device;
break;
}
}
#pragma GCC diagnostic pop
}
// If the front camera is not available, use the back camera
if (!newCaptureDevice) {
newCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
// Using AVCaptureFocusModeContinuousAutoFocus helps improve scan times
NSError *error = nil;
if ([newCaptureDevice lockForConfiguration:&error]) {
if ([newCaptureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
newCaptureDevice.focusMode = AVCaptureFocusModeContinuousAutoFocus;
}
[newCaptureDevice unlockForConfiguration];
} else {
NSLog(@"Failed to acquire lock for initial focus mode: %@", error);
}
return newCaptureDevice;
}
+ (AVCaptureDevicePosition)devicePositionForCamera:(MTBCamera)camera {
switch (camera) {
case MTBCameraFront:
return AVCaptureDevicePositionFront;
case MTBCameraBack:
return AVCaptureDevicePositionBack;
}
NSAssert(NO, @"Invalid camera type: %lu", (unsigned long)camera);
return AVCaptureDevicePositionUnspecified;
}
#pragma mark - Default Values
- (NSArray<NSString *> *)defaultMetaDataObjectTypes {
NSMutableArray *types = [@[AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeAztecCode] mutableCopy];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
[types addObjectsFromArray:@[AVMetadataObjectTypeInterleaved2of5Code,
AVMetadataObjectTypeITF14Code,
AVMetadataObjectTypeDataMatrixCode
]];
}
return [types copy];
}
#pragma mark - Helper Methods
- (void)addObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleApplicationDidChangeStatusBarNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)setupSessionQueue {
NSAssert(self.privateSessionQueue == NULL, @"Queue should only be set up once");
if (self.privateSessionQueue) {
return;
}
self.privateSessionQueue = dispatch_queue_create("com.mikebuss.MTBBarcodeScanner.captureSession", DISPATCH_QUEUE_SERIAL);
}
- (void)setDeviceInput:(AVCaptureDeviceInput *)deviceInput session:(AVCaptureSession *)session {
if (deviceInput == nil) {
// Nil device inputs cannot be added to instances of AVCaptureSession
return;
}
[self removeDeviceInput:self.currentCaptureDeviceInput session:session];
self.currentCaptureDeviceInput = deviceInput;
[self updateFocusPreferencesOfDevice:deviceInput.device reset:NO];
[session addInput:deviceInput];
}
- (void)removeDeviceInput:(AVCaptureDeviceInput *)deviceInput session:(AVCaptureSession *)session {
if (deviceInput == nil) {
// No need to remove the device input if it was never set
return;
}
// Restore focus settings to the previously saved state
[self updateFocusPreferencesOfDevice:deviceInput.device reset:YES];
[session removeInput:deviceInput];
self.currentCaptureDeviceInput = nil;
}
- (void)updateFocusPreferencesOfDevice:(AVCaptureDevice *)inputDevice reset:(BOOL)reset {
NSParameterAssert(inputDevice);
if (!inputDevice) {
return;
}
NSError *lockError;
if (![inputDevice lockForConfiguration:&lockError]) {
NSLog(@"Failed to acquire lock to (re)set focus options: %@", lockError);
return;
}
// Prioritize the focus on objects near to the device
if (inputDevice.isAutoFocusRangeRestrictionSupported) {
if (!reset) {
self.initialAutoFocusRangeRestriction = inputDevice.autoFocusRangeRestriction;
inputDevice.autoFocusRangeRestriction = self.preferredAutoFocusRangeRestriction;
} else {
inputDevice.autoFocusRangeRestriction = self.initialAutoFocusRangeRestriction;
}
}
// Focus on the center of the image
if (inputDevice.isFocusPointOfInterestSupported) {
if (!reset) {
self.initialFocusPoint = inputDevice.focusPointOfInterest;
inputDevice.focusPointOfInterest = CGPointMake(kFocalPointOfInterestX, kFocalPointOfInterestY);
} else {
inputDevice.focusPointOfInterest = self.initialFocusPoint;
}
}
[inputDevice unlockForConfiguration];
// this method will acquire its own lock
[self updateForTorchMode:self.torchMode error:nil];
}
#pragma mark - Torch Control
- (void)setTorchMode:(MTBTorchMode)torchMode {
[self setTorchMode:torchMode error:nil];
}
- (BOOL)setTorchMode:(MTBTorchMode)torchMode error:(NSError **)error {
if ([self updateForTorchMode:torchMode error:error]) {
// we only update our internal state if setting the torch mode was successful
_torchMode = torchMode;
return YES;
}
return NO;
}
- (void)toggleTorch {
switch (self.torchMode) {
case MTBTorchModeOn:
self.torchMode = MTBTorchModeOff;
break;
case MTBTorchModeOff:
self.torchMode = MTBTorchModeOn;
break;
}
}
- (BOOL)updateForTorchMode:(MTBTorchMode)preferredTorchMode error:(NSError **)error {
AVCaptureDevice *backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureTorchMode avTorchMode = [self avTorchModeForMTBTorchMode:preferredTorchMode];
if (!([backCamera isTorchAvailable] && [backCamera isTorchModeSupported:avTorchMode])) {
if (error) {
*error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeTorchModeUnavailable
userInfo:@{NSLocalizedDescriptionKey : @"Torch unavailable or mode not supported."}];
}
return NO;
}
if (![backCamera lockForConfiguration:error]) {
NSLog(@"Failed to acquire lock to update torch mode.");
return NO;
}
[backCamera setTorchMode:avTorchMode];
[backCamera unlockForConfiguration];
return YES;
}
- (BOOL)hasTorch {
AVCaptureDevice *captureDevice = [self newCaptureDeviceWithCamera:self.camera];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
return input.device.hasTorch;
}
- (AVCaptureTorchMode)avTorchModeForMTBTorchMode:(MTBTorchMode)torchMode {
switch (torchMode) {
case MTBTorchModeOn:
return AVCaptureTorchModeOn;
case MTBTorchModeOff:
return AVCaptureTorchModeOff;
}
NSAssert(NO, @"Invalid torch mode: %lu", (unsigned long)torchMode);
return AVCaptureTorchModeOff;
}
#pragma mark - Capture
- (void)freezeCapture {
// we must access the layer on the main thread, but manipulating
// the capture connection is blocking and should be dispatched
AVCaptureConnection *connection = self.capturePreviewLayer.connection;
dispatch_async(self.privateSessionQueue, ^{
connection.enabled = NO;
[self.session stopRunning];
});
}
- (void)unfreezeCapture {
if (!self.session) {
return;
}
AVCaptureConnection *connection = self.capturePreviewLayer.connection;
if (!self.session.isRunning) {
[self setDeviceInput:self.currentCaptureDeviceInput session:self.session];
dispatch_async(self.privateSessionQueue, ^{
[self.session startRunning];
connection.enabled = YES;
});
}
}
- (void)captureStillImage:(void (^)(UIImage *image, NSError *error))captureBlock {
if ([self isCapturingStillImage]) {
if (captureBlock) {
NSError *error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeStillImageCaptureInProgress
userInfo:@{NSLocalizedDescriptionKey : @"Still image capture is already in progress. Check with isCapturingStillImage"}];
captureBlock(nil, error);
}
return;
}
if (@available(iOS 10.0, *)) {
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
settings.autoStillImageStabilizationEnabled = NO;
settings.flashMode = AVCaptureFlashModeOff;
settings.highResolutionPhotoEnabled = YES;
dispatch_async(self.privateSessionQueue, ^{
[self.output capturePhotoWithSettings:settings delegate:self];
self.stillImageCaptureBlock = captureBlock;
});
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
AVCaptureConnection *stillConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (stillConnection == nil) {
if (captureBlock) {
NSError *error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeSessionIsClosed
userInfo:@{NSLocalizedDescriptionKey : @"AVCaptureConnection is closed"}];
captureBlock(nil, error);
}
return;
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (error) {
captureBlock(nil, error);
return;
}
NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:jpegData];
if (captureBlock) {
captureBlock(image, nil);
}
}];
#pragma GCC diagnostic pop
}
}
#pragma mark - AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error NS_AVAILABLE_IOS(11.0) {
NSAssert(_stillImageCaptureBlock, @"Unable to capture photos with a nil stillImageCaptureBlock");
if (@available(iOS 11.0, *)) {
NSData *data = photo.fileDataRepresentation;
UIImage *image = nil;
if (data) {
image = [UIImage imageWithData:data];
}
self.stillImageCaptureBlock(image, error);
} else {
NSError *error = [NSError errorWithDomain:kErrorDomain
code:kErrorMethodNotAvailableOnIOSVersion
userInfo:@{NSLocalizedDescriptionKey : @"Unable to capture still image: the method is not available on this device."}];
self.stillImageCaptureBlock(nil, error);
}
}
// This method uses methods that are deprecated in iOS 10. We also implement the updated method (captureOutput:didFinishProcessingPhoto:error:), so we can ignore the warning here.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wdeprecated-implementations"
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error NS_AVAILABLE_IOS(10.0) {
if (photoSampleBuffer == nil) {
return;
}
NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
UIImage *image = nil;
if (data) {
image = [UIImage imageWithData:data];
}
if (self.stillImageCaptureBlock) {
self.stillImageCaptureBlock(image, error);
}
}
#pragma GCC diagnostic pop
- (BOOL)isCapturingStillImage {
return self.stillImageOutput.isCapturingStillImage;
}
#pragma mark - Setters
- (void)setCamera:(MTBCamera)camera {
[self setCamera:camera error:nil];
}
- (BOOL)setCamera:(MTBCamera)camera error:(NSError **)error {
if (camera == _camera) {
return YES;
}
if (!self.isScanning) {
if (error) {
*error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeNotScanning
userInfo:@{NSLocalizedDescriptionKey : @"Camera cannot be set when isScanning is NO"}];
}
return NO;
}
AVCaptureDevice *captureDevice = [self newCaptureDeviceWithCamera:camera];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:error];
if (!input) {
// we rely on deviceInputWithDevice:error to populate the error in this case
return NO;
}
[self setDeviceInput:input session:self.session];
_camera = camera;
return YES;
}
- (void)setScanRect:(CGRect)scanRect {
NSAssert(!CGRectIsEmpty(scanRect), @"Unable to set an empty rectangle as the scanRect of MTBBarcodeScanner");
NSAssert(self.isScanning, @"Scan rect cannot be set when not (yet) scanning. You may want to set it within didStartScanningBlock.");
if (!self.isScanning) {
return;
}
[self refreshVideoOrientation];
_scanRect = scanRect;
dispatch_async(self.privateSessionQueue, ^{
self.captureOutput.rectOfInterest = [self.capturePreviewLayer metadataOutputRectOfInterestForRect:self->_scanRect];
});
}
- (void)setPreferredAutoFocusRangeRestriction:(AVCaptureAutoFocusRangeRestriction)preferredAutoFocusRangeRestriction {
if (preferredAutoFocusRangeRestriction == _preferredAutoFocusRangeRestriction) {
return;
}
_preferredAutoFocusRangeRestriction = preferredAutoFocusRangeRestriction;
if (!self.currentCaptureDeviceInput) {
// the setting will be picked up once a new session incl. device input is created
return;
}
[self updateFocusPreferencesOfDevice:self.currentCaptureDeviceInput.device reset:NO];