Skip to content

Commit

Permalink
[ADD] Allow multiple metadata object types
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickl committed Feb 8, 2015
1 parent 29f52f5 commit 6cecbb1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
32 changes: 31 additions & 1 deletion QRCodeReaderViewController/QRCodeReaderViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@
* @abstract Initializes a view controller to read QRCodes from a displayed
* video preview and a cancel button to be go back.
* @param cancelTitle The title of the cancel button.
* @discussion This convenient method is used to instanciate a reader with
* only one supported metadata object types: the QRCode.
* @see initWithCancelButtonTitle:metadataObjectTypes:
* @since 1.0.0
*/
- (id)initWithCancelButtonTitle:(NSString *)cancelTitle;

/**
* @abstract Initializes a view controller to read wanter metadata object
* types from a displayed video preview and a cancel button to be go back.
* @param cancelTitle The title of the cancel button.
* @param metadataObjectTypes The type (“symbology”) of barcode to scan.
* @since 2.0.0
*/
- (id)initWithCancelButtonTitle:(NSString *)cancelTitle metadataObjectTypes:(NSArray *)metadataObjectTypes;

/**
* @abstract Creates a view controller to read QRCodes from a displayed
* video preview and a cancel button to be go back.
Expand All @@ -65,15 +77,33 @@
*/
+ (instancetype)readerWithCancelButtonTitle:(NSString *)cancelTitle;

/**
* @abstract Creates a view controller to read wanter metadata object types
* from a displayed video preview and a cancel button to be go back.
* @param cancelTitle The title of the cancel button.
* @param metadataObjectTypes The type (“symbology”) of barcode to scan.
* @since 2.0.0
*/
+ (instancetype)readerWithCancelButtonTitle:(NSString *)cancelTitle metadataObjectTypes:(NSArray *)metadataObjectTypes;

#pragma mark - Checking the Metadata Items Types
/** @name Checking the Metadata Items Types */

/**
* @abstract Returns whether you can scan a QRCode with the current device.
* @return a Boolean value indicating whether you can scan a QRCode with the current device.
* @return a Boolean value indicating whether you can scan a QRCode with the
* current device.
* @since 1.0.0
*/
+ (BOOL)isAvailable;

/**
* @abstract An array of strings identifying the types of metadata objects to
* process.
* @since 2.0.0
*/
@property (strong, nonatomic, readonly) NSArray *metadataObjectTypes;

#pragma mark - Managing the Block
/** @name Managing the Block */

Expand Down
18 changes: 14 additions & 4 deletions QRCodeReaderViewController/QRCodeReaderViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ @interface QRCodeReaderViewController () <AVCaptureMetadataOutputObjectsDelegate
@property (strong, nonatomic) QRCodeReaderView *cameraView;
@property (strong, nonatomic) UIButton *cancelButton;

@property (strong, nonatomic) NSArray *metadataObjectTypes;
@property (strong, nonatomic) AVCaptureDevice *defaultDevice;
@property (strong, nonatomic) AVCaptureDeviceInput *defaultDeviceInput;
@property (strong, nonatomic) AVCaptureDevice *frontDevice;
Expand All @@ -53,9 +54,15 @@ - (id)init
}

- (id)initWithCancelButtonTitle:(NSString *)cancelTitle
{
return [self initWithCancelButtonTitle:cancelTitle metadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
}

- (id)initWithCancelButtonTitle:(NSString *)cancelTitle metadataObjectTypes:(NSArray *)metadataObjectTypes
{
if ((self = [super init])) {
self.view.backgroundColor = [UIColor blackColor];
self.metadataObjectTypes = metadataObjectTypes;

[self setupAVComponents];
[self configureDefaultComponents];
Expand All @@ -72,6 +79,11 @@ + (instancetype)readerWithCancelButtonTitle:(NSString *)cancelTitle
return [[self alloc] initWithCancelButtonTitle:cancelTitle];
}

+ (instancetype)readerWithCancelButtonTitle:(NSString *)cancelTitle metadataObjectTypes:(NSArray *)metadataObjectTypes
{
return [[self alloc] initWithCancelButtonTitle:cancelTitle metadataObjectTypes:metadataObjectTypes];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
Expand Down Expand Up @@ -208,9 +220,7 @@ - (void)configureDefaultComponents
}

[_metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
if ([[_metadataOutput availableMetadataObjectTypes] containsObject:AVMetadataObjectTypeQRCode]) {
[_metadataOutput setMetadataObjectTypes:@[ AVMetadataObjectTypeQRCode ]];
}
[_metadataOutput setMetadataObjectTypes:_metadataObjectTypes];
[_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_previewLayer setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

Expand Down Expand Up @@ -276,7 +286,7 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:
{
for(AVMetadataObject *current in metadataObjects) {
if ([current isKindOfClass:[AVMetadataMachineReadableCodeObject class]]
&& [current.type isEqualToString:AVMetadataObjectTypeQRCode]) {
&& [_metadataObjectTypes containsObject:current.type]) {
NSString *scannedResult = [(AVMetadataMachineReadableCodeObject *) current stringValue];

if (_completionBlock) {
Expand Down

0 comments on commit 6cecbb1

Please sign in to comment.