diff --git a/QRCodeReaderViewController/QRCodeReader.m b/QRCodeReaderViewController/QRCodeReader.m index 5850e23..9bff12d 100644 --- a/QRCodeReaderViewController/QRCodeReader.m +++ b/QRCodeReaderViewController/QRCodeReader.m @@ -25,6 +25,7 @@ */ #import "QRCodeReader.h" +#import "QRCodeReaderView.h" @interface QRCodeReader () @property (strong, nonatomic) AVCaptureDevice *defaultDevice; @@ -248,18 +249,21 @@ - (void)setCompletionWithBlock:(void (^) (NSString *resultAsString))completionBl - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { - for (AVMetadataObject *current in metadataObjects) { - if ([current isKindOfClass:[AVMetadataMachineReadableCodeObject class]] - && [_metadataObjectTypes containsObject:current.type]) { - NSString *scannedResult = [(AVMetadataMachineReadableCodeObject *)current stringValue]; - - if (_completionBlock) { - _completionBlock(scannedResult); - } - - break; + for (AVMetadataObject *current in metadataObjects) { + if ([current isKindOfClass:[AVMetadataMachineReadableCodeObject class]] && [_metadataObjectTypes containsObject:current.type]) { + NSString *scannedResult = [(AVMetadataMachineReadableCodeObject *)current stringValue]; + AVMetadataObject* transformed = [self.previewLayer transformedMetadataObjectForMetadataObject:current]; + CGRect scanarea = [QRCodeReaderView scanArea]; + if (CGRectIntersectsRect(scanarea, transformed.bounds)) + { + NSLog(@"/////// Intersect"); + if (_completionBlock) { + _completionBlock(scannedResult); + break; + } + } + } } - } } @end diff --git a/QRCodeReaderViewController/QRCodeReaderView.h b/QRCodeReaderViewController/QRCodeReaderView.h index 260350f..ce77d5d 100644 --- a/QRCodeReaderViewController/QRCodeReaderView.h +++ b/QRCodeReaderViewController/QRCodeReaderView.h @@ -32,5 +32,5 @@ * @since 2.0.0 */ @interface QRCodeReaderView : UIView - ++(CGRect)scanArea; @end diff --git a/QRCodeReaderViewController/QRCodeReaderView.m b/QRCodeReaderViewController/QRCodeReaderView.m index 60a1192..642be21 100644 --- a/QRCodeReaderViewController/QRCodeReaderView.m +++ b/QRCodeReaderViewController/QRCodeReaderView.m @@ -26,6 +26,8 @@ #import "QRCodeReaderView.h" +static CGRect scanarea; + @interface QRCodeReaderView () @property (nonatomic, strong) CAShapeLayer *overlay; @@ -38,7 +40,7 @@ - (id)initWithFrame:(CGRect)frame if ((self = [super initWithFrame:frame])) { [self addOverlay]; } - + scanarea = CGRectZero; return self; } @@ -58,10 +60,16 @@ - (void)drawRect:(CGRect)rect CGRect offsetRect = CGRectOffset(innerRect, 0, 15); - + scanarea = offsetRect; + _overlay.path = [UIBezierPath bezierPathWithRoundedRect:offsetRect cornerRadius:5].CGPath; } ++(CGRect)scanArea +{ + return scanarea; +} + #pragma mark - Private Methods - (void)addOverlay