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 197
/
MTBBarcodeScanner.h
330 lines (289 loc) · 10.7 KB
/
MTBBarcodeScanner.h
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
//
// MTBBarcodeScanner.h
// MTBBarcodeScannerExample
//
// Created by Mike Buss on 2/8/14.
//
//
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, MTBCamera) {
MTBCameraBack,
MTBCameraFront
};
/**
* Available torch modes when scanning barcodes.
*
* While AVFoundation provides an additional automatic
* mode, it is not supported here because it only works
* with video recordings, not barcode scanning.
*/
typedef NS_ENUM(NSUInteger, MTBTorchMode) {
MTBTorchModeOff,
MTBTorchModeOn,
};
@interface MTBBarcodeScanner : NSObject
/**
* The currently set camera. See MTBCamera for options.
*
* @sa setCamera:error:
*/
@property (nonatomic, assign, readonly) MTBCamera camera;
/**
* Control the torch on the device, if present.
*
* Attempting to set the torch mode to an unsupported state
* will fail silently, and the value passed into the setter
* will be discarded.
*
* @sa setTorchMode:error:
*/
@property (nonatomic, assign) MTBTorchMode torchMode;
/**
* Allow the user to tap the previewView to focus a specific area.
* Defaults to YES.
*/
@property (nonatomic, assign) BOOL allowTapToFocus;
/**
* If set, only barcodes inside this area will be scanned.
*
* Setting this property is only supported while the scanner is active.
* Use the didStartScanningBlock if you want to set it as early as
* possible.
*/
@property (nonatomic, assign) CGRect scanRect;
/**
* Layer used to present the camera input. If the previewView
* does not use auto layout, it may be necessary to adjust the layers frame.
*/
@property (nonatomic, strong) CALayer *previewLayer;
/*!
@property didStartScanningBlock
@abstract
Optional callback block that's called when the scanner finished initializing.
@discussion
Optional callback that will be called when the scanner is initialized and the view
is presented on the screen. This is useful for presenting an activity indicator
while the scanner is initializing.
The block is always called on the main queue.
*/
@property (nonatomic, copy) void (^didStartScanningBlock)(void);
/*!
@property didTapToFocusBlock
@abstract
Block that's called when the user taps the screen to focus the camera. If allowsTapToFocus
is set to NO, this will never be called.
*/
@property (nonatomic, copy) void (^didTapToFocusBlock)(CGPoint point);
/*!
@property resultBlock
@abstract
Block that's called every time one or more barcodes are recognized.
@discussion
The resultBlock is called on the main queue once for every frame that at least one valid barcode is found.
This block is automatically set when you call startScanningWithResultBlock:
*/
@property (nonatomic, copy) void (^resultBlock)(NSArray<AVMetadataMachineReadableCodeObject *> *codes);
/*!
@property preferredAutoFocusRangeRestriction
@abstract
Auto focus range restriction, if supported.
@discussion
Defaults to AVCaptureAutoFocusRangeRestrictionNear. Will be ignored on unsupported devices.
*/
@property (nonatomic, assign) AVCaptureAutoFocusRangeRestriction preferredAutoFocusRangeRestriction;
/**
* Initialize a scanner that will feed the camera input
* into the given UIView.
*
* @param previewView View that will be overlayed with the live feed from the camera input.
*
* @return An instance of MTBBarcodeScanner
*/
- (instancetype)initWithPreviewView:(UIView *)previewView;
/**
* Initialize a scanner that will feed the camera input
* into the given UIView. Only codes with a type given in
* the metaDataObjectTypes array will be reported to the result
* block when scanning is started using startScanningWithResultBlock:
*
* @see startScanningWithResultBlock:
*
* @param metaDataObjectTypes Array of AVMetadataObjectTypes to scan for. Only codes with types given in this array will be reported to the resultBlock.
* @param previewView View that will be overlayed with the live feed from the camera input.
*
* @return An instance of MTBBarcodeScanner
*/
- (instancetype)initWithMetadataObjectTypes:(NSArray<NSString *> *)metaDataObjectTypes
previewView:(UIView *)previewView;
/**
* Returns whether any camera exists in this device.
* Be aware that this returns NO if camera access is restricted.
*
* @return YES if the device has a camera and authorization state is not AVAuthorizationStatusRestricted
*/
+ (BOOL)cameraIsPresent;
/**
* You can use this flag to check whether flipCamera can potentially
* be successful. You may want to hide your button to flip the camera
* if the device only has one camera.
*
* @return YES if a second camera is present and authorization state is not AVAuthorizationStatusRestricted.
* @sa flipCamera
*/
- (BOOL)hasOppositeCamera;
/**
* Returns whether scanning is prohibited by the user of the device.
*
* @return YES if the user has prohibited access to (or is prohibited from accessing) the camera.
*/
+ (BOOL)scanningIsProhibited;
/**
* Request permission to access the camera on the device.
*
* The success block will return YES if the user granted permission, has granted permission in the past, or if the device is running iOS 7.
* The success block will return NO if the user denied permission, is restricted from the camera, or if there is no camera present.
*/
+ (void)requestCameraPermissionWithSuccess:(void (^)(BOOL success))successBlock;
/**
* Start scanning for barcodes. The camera input will be added as a sublayer
* to the UIView given for previewView during initialization.
*
* This method assumes you have already set the `resultBlock` property directly.
*
* This method returns quickly and does not wait for the internal session to
* start. Set the didStartScanningBlock to get a callback when the session
* is ready, i.e., a camera picture is visible.
*
* @param error Error supplied if the scanning could not start.
*
* @return YES if scanning started successfully, NO if there was an error.
*/
- (BOOL)startScanningWithError:(NSError **)error;
/**
* Start scanning for barcodes. The camera input will be added as a sublayer
* to the UIView given for previewView during initialization.
*
* This method returns quickly and does not wait for the internal session to
* start. Set the didStartScanningBlock to get a callback when the session
* is ready, i.e., a camera picture is visible.
*
* @param resultBlock Callback block for captured codes. If the scanner was instantiated with initWithMetadataObjectTypes:previewView, only codes with a type given in metaDataObjectTypes will be reported.
* @param error Error supplied if the scanning could not start.
*
* @return YES if scanning started successfully, NO if there was an error.
*/
- (BOOL)startScanningWithResultBlock:(void (^)(NSArray<AVMetadataMachineReadableCodeObject *> *codes))resultBlock error:(NSError **)error;
/**
* Start scanning for barcodes using a specific camera. The camera input will be added as a sublayer
* to the UIView given for previewView during initialization.
*
* This method returns quickly and does not wait for the internal session to
* start. Set the didStartScanningBlock to get a callback when the session
* is ready, i.e., a camera picture is visible.
*
* @param camera The camera to use when scanning starts.
* @param resultBlock Callback block for captured codes. If the scanner was instantiated with initWithMetadataObjectTypes:previewView, only codes with a type given in metaDataObjectTypes will be reported.
* @param error Error supplied if the scanning could not start.
*
*
* @return YES if scanning started successfully, NO if there was an error.
*/
- (BOOL)startScanningWithCamera:(MTBCamera)camera resultBlock:(void (^)(NSArray<AVMetadataMachineReadableCodeObject *> *codes))resultBlock error:(NSError **)error;
/**
* Stop scanning for barcodes. The live feed from the camera will be removed as a sublayer from the previewView given during initialization.
*/
- (void)stopScanning;
/**
* Whether the scanner is currently scanning for barcodes
*
* @return YES if the scanner is currently scanning for barcodes
*/
- (BOOL)isScanning;
/**
* If using the front camera, switch to the back, or visa-versa.
* If this method is called when isScanning=NO, it has no effect
*
* If the opposite camera is not available, this method will do nothing.
*
* @sa hasOppositeCamera
*/
- (void)flipCamera;
/**
* Sets the camera and ignores any errors.
*
* Deprecated.
*
* @sa setCamera:error:
*/
- (void)setCamera:(MTBCamera)camera NS_DEPRECATED_IOS(2.0, 2.0, "Use setCamera:error: instead");
/**
* Sets the camera. This operation may fail, e.g., when the device
* does not have the specified camera.
*
* @param camera The camera to use, see MTBCamera for options.
* @error Any error that occurred while trying to set the camera.
* @return YES, if the specified camera was set successfully, NO if any error occurred.
*/
- (BOOL)setCamera:(MTBCamera)camera error:(NSError **)error;
/**
* If using the front camera, switch to the back, or visa-versa.
*
* If this method is called when (isScanning == NO), it will return
* NO and provide an error.
*
* If the opposite camera is not available, the error parameter
* will explain the error.
*
* @return YES if the camera was flipped, NO if any error occurred.
*/
- (BOOL)flipCameraWithError:(NSError **)error;
/**
* Return a BOOL value that specifies whether the current capture device has a torch.
*
* @return YES if the the current capture device has a torch.
*/
- (BOOL)hasTorch;
/**
* Toggle the torch from on to off, or off to on.
* If the device does not support a torch or the opposite mode, calling
* this method will have no effect.
* To set the torch to on/off directly, set the `torchMode` property, or
* use setTorchMode:error: if you care about errors.
*/
- (void)toggleTorch;
/**
* Attempts to set a new torch mode.
*
* @return YES, if setting the new mode was successful, and the torchMode
* property reflects the new state. NO if there was an error - use the
* error parameter to learn about the reason.
*
* @sa torchMode
*/
- (BOOL)setTorchMode:(MTBTorchMode)torchMode error:(NSError **)error;
/**
* Freeze capture keeping the last frame on previewView.
* If this method is called before startScanning, it has no effect.
*
* Returns immediately – actually freezing the capture is
* done asynchronously.
*/
- (void)freezeCapture;
/**
* Unfreeze a frozen capture.
*
* Returns immediately – actually unfreezing the capture is
* done asynchronously.
*/
- (void)unfreezeCapture;
/**
* Captures a still image of the current camera feed
*/
- (void)captureStillImage:(void (^)(UIImage *image, NSError *error))captureBlock;
/**
* Determine if currently capturing a still image
*/
- (BOOL)isCapturingStillImage;
@end