-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOBCaptureViewController.m
More file actions
208 lines (155 loc) · 7.46 KB
/
DOBCaptureViewController.m
File metadata and controls
208 lines (155 loc) · 7.46 KB
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
//
// DOBCaptureViewController.m
//
// Created by David Ortega on 19/02/14.
//
#import "DOBCaptureViewController.h"
#import "DOBCaptureConfigManager.h"
@import AVFoundation;
@import QuartzCore;
@interface DOBCaptureViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic) AVCaptureSession *captureSession;
@property (nonatomic) AVCaptureDeviceInput *captureDeviceInput;
@property (nonatomic) AVCaptureVideoPreviewLayer *capturePreviewLayer;
@property (nonatomic) AVCaptureVideoDataOutput *captureVideoDataOutput;
@property (nonatomic) AVCaptureDevice* captureDevice;
@end
@implementation DOBCaptureViewController
- (void)viewDidLoad {
[super viewDidLoad];
_buttonFlash = [UIButton buttonWithType:UIButtonTypeCustom];
[_buttonFlash setTranslatesAutoresizingMaskIntoConstraints:NO];
[_buttonFlash setImage:[UIImage imageNamed:@"flash_off.png"] forState:UIControlStateNormal];
[_buttonFlash setImage:[UIImage imageNamed:@"flash_on.png"] forState:UIControlStateSelected];
[_buttonFlash addTarget:self action:@selector(switchFlash:) forControlEvents:UIControlEventTouchUpInside];
NSLayoutConstraint *topConstraint =
[NSLayoutConstraint constraintWithItem:_buttonFlash attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:15];
NSLayoutConstraint *leadingConstraint =
[NSLayoutConstraint constraintWithItem:_buttonFlash attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:15];
NSLayoutConstraint *widthConstraint =
[NSLayoutConstraint constraintWithItem:_buttonFlash attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:30];
NSLayoutConstraint *heightConstraint =
[NSLayoutConstraint constraintWithItem:_buttonFlash attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:30];
[self.view addSubview: self.buttonFlash];
[self.view addConstraints:@[topConstraint, leadingConstraint]];
[self.buttonFlash addConstraints:@[widthConstraint, heightConstraint]];
[self setupCameraSession];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_buttonFlash setSelected:[DOBCaptureConfigManager sharedInstance].flash];
[self setFlashState:[DOBCaptureConfigManager sharedInstance].flash];
[self.captureSession startRunning];
}
-(void) viewWillDisappear:(BOOL)animated {
if ([_buttonFlash isSelected])
[self setFlashState:NO];
[self.captureSession stopRunning];
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (AVCaptureDevice *) captureDeviceWithPosition:(AVCaptureDevicePosition)position {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device.position == position) {
return device;
}
}
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
#pragma mark - Camera configuration
- (void)setupCameraSession {
// Creates a capture session
if (self.captureSession == nil) {
self.captureSession = [[AVCaptureSession alloc] init];
}
// Begins the capture session configuration
[self.captureSession beginConfiguration];
_captureDevice = [self captureDeviceWithPosition:AVCaptureDevicePositionBack];
// Locks the configuration
BOOL success = [_captureDevice lockForConfiguration:nil];
if (success) {
if ([_captureDevice isAutoFocusRangeRestrictionSupported]) {
// Restricts the autofocus to near range (new in iOS 7)
[_captureDevice setAutoFocusRangeRestriction:AVCaptureAutoFocusRangeRestrictionNear];
}
}
// unlocks the configuration
[_captureDevice unlockForConfiguration];
NSError *error;
// Adds the device input to capture session
self.captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:_captureDevice error:&error];
if ( [self.captureSession canAddInput:self.captureDeviceInput] )
[self.captureSession addInput:self.captureDeviceInput];
// Prepares the preview layer
self.capturePreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
CGRect frame = [[UIScreen mainScreen] bounds];
[self.capturePreviewLayer setFrame:frame];
[self.capturePreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
// Adds the preview layer to the main view layer
[self.view.layer insertSublayer:self.capturePreviewLayer atIndex:0];
// Creates and adds the metadata output to the capture session
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
if ([self.captureSession canAddOutput:metadataOutput]) {
[self.captureSession addOutput:metadataOutput];
}
// Creates a GCD queue to dispatch the metadata
dispatch_queue_t metadataQueue = dispatch_queue_create("com.davidberdun.metadataqueue", DISPATCH_QUEUE_SERIAL);
[metadataOutput setMetadataObjectsDelegate:self queue:metadataQueue];
// Sets the metadata object types. Essentially, here you can choose the barcode type.
NSArray *metadataTypes = @[ AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code ];
[metadataOutput setMetadataObjectTypes:metadataTypes];
// Commits the camera configuration
[self.captureSession commitConfiguration];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
if ([metadataObjects count] < 1) {
return;
}
for (id item in metadataObjects) {
if ([item isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) {
if (item) {
NSLog(@"%@", [item stringValue]);
[self.captureSession stopRunning];
if (_closeAfterRead) {
[self dismissViewControllerAnimated:YES completion:^{
[self.delegate codeCaptured:[item stringValue]];
}];
} else {
[self.delegate codeCaptured:[item stringValue]];
}
}
}
}
}
- (IBAction)switchFlash:(id)sender {
BOOL state = ! [_buttonFlash isSelected];
[self setFlashState:state];
[_buttonFlash setSelected:state];
[DOBCaptureConfigManager sharedInstance].flash = state;
}
- (void) setFlashState: (BOOL) state {
NSError* error;
if ([_captureDevice hasTorch]) {
[_captureDevice lockForConfiguration:&error];
(state)?[_captureDevice setTorchMode:AVCaptureTorchModeOn]:[_captureDevice setTorchMode:AVCaptureTorchModeOff];
[_captureDevice unlockForConfiguration];
}
if (error)
NSLog(@"Error switching on flash: %@", error);
}
- (void) reStartRunningReader {
BOOL state = [DOBCaptureConfigManager sharedInstance].flash;
[self setFlashState:state];
[self.captureSession startRunning];
}
@end