forked from mintware-de/flutter_barcode_reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarcodeScannerViewController.m
144 lines (127 loc) · 5.21 KB
/
BarcodeScannerViewController.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
//
// Created by Matthew Smith on 11/7/17.
//
#import "BarcodeScannerViewController.h"
#import <MTBBarcodeScanner/MTBBarcodeScanner.h>
#import "ScannerOverlay.h"
@implementation BarcodeScannerViewController {
}
- (void)viewDidLoad {
[super viewDidLoad];
self.previewView = [[UIView alloc] initWithFrame:self.view.bounds];
self.previewView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_previewView];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[previewView]"
options:NSLayoutFormatAlignAllBottom
metrics:nil
views:@{@"previewView": _previewView}]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[previewView]"
options:NSLayoutFormatAlignAllBottom
metrics:nil
views:@{@"previewView": _previewView}]];
self.scanRect = [[ScannerOverlay alloc] initWithFrame:self.view.bounds];
self.scanRect.translatesAutoresizingMaskIntoConstraints = NO;
self.scanRect.backgroundColor = UIColor.clearColor;
[self.view addSubview:_scanRect];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[scanRect]"
options:NSLayoutFormatAlignAllBottom
metrics:nil
views:@{@"scanRect": _scanRect}]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[scanRect]"
options:NSLayoutFormatAlignAllBottom
metrics:nil
views:@{@"scanRect": _scanRect}]];
[_scanRect startAnimating];
self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:_previewView];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
[self updateFlashButton];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.scanner.isScanning) {
[self.scanner stopScanning];
}
[MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) {
if (success) {
[self startScan];
} else {
[self.delegate barcodeScannerViewController:self didFailWithErrorCode:@"PERMISSION_NOT_GRANTED"];
[self dismissViewControllerAnimated:NO completion:nil];
}
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.scanner stopScanning];
[super viewWillDisappear:animated];
if ([self isFlashOn]) {
[self toggleFlash:NO];
}
}
- (void)startScan {
NSError *error;
[self.scanner startScanningWithResultBlock:^(NSArray<AVMetadataMachineReadableCodeObject *> *codes) {
[self.scanner stopScanning];
AVMetadataMachineReadableCodeObject *code = codes.firstObject;
if (code) {
[self.delegate barcodeScannerViewController:self didScanBarcodeWithResult:code.stringValue];
[self dismissViewControllerAnimated:NO completion:nil];
}
} error:&error];
}
- (void)cancel {
[self dismissViewControllerAnimated:true completion:nil];
}
- (void)updateFlashButton {
if (!self.hasTorch) {
return;
}
if (self.isFlashOn) {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Flash Off"
style:UIBarButtonItemStylePlain
target:self action:@selector(toggle)];
} else {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Flash On"
style:UIBarButtonItemStylePlain
target:self action:@selector(toggle)];
}
}
- (void)toggle {
[self toggleFlash:!self.isFlashOn];
[self updateFlashButton];
}
- (BOOL)isFlashOn {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
return device.torchMode == AVCaptureFlashModeOn || device.torchMode == AVCaptureTorchModeOn;
}
return NO;
}
- (BOOL)hasTorch {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
return device.hasTorch;
}
return false;
}
- (void)toggleFlash:(BOOL)on {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device) return;
NSError *err;
if (device.hasFlash && device.hasTorch) {
[device lockForConfiguration:&err];
if (err != nil) return;
if (on) {
device.flashMode = AVCaptureFlashModeOn;
device.torchMode = AVCaptureTorchModeOn;
} else {
device.flashMode = AVCaptureFlashModeOff;
device.torchMode = AVCaptureTorchModeOff;
}
[device unlockForConfiguration];
}
}
@end