2

I am using AVFoundation and AVCaptureMetadataOutput to scan a QR barcode in iOS7, I present a view controller which allows the user to scan a barcode. It is working fine, ie. a barcode is being scanned and I can output the barcode string to console.

But it keeps scanning over and over again, see screen shot. What I want it to do is scan the barcode just the once and then dismissViewController.

Here is my code for the delegate method:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
CGRect highlightViewRect = CGRectZero;

AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
        AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
        AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

for (AVMetadataObject *metadata in metadataObjects) {
    for (NSString *type in barCodeTypes) {
        if ([metadata.type isEqualToString:type])
        {
            barCodeObject = (AVMetadataMachineReadableCodeObject *)[self.preview transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
            highlightViewRect = barCodeObject.bounds;
            detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
            break;
        }
    }

    if (detectionString != nil)
    {
        NSLog(@"Barcode: %@", detectionString);

        break;
    }
    else
        NSLog(@"None");
}

self.highlightView.frame = highlightViewRect;

}

enter image description here

Hassan
  • 183
  • 1
  • 2
  • 7
  • 1
    Fixed by using the following answer: http://stackoverflow.com/questions/19525132/avcapturesession-stoprunning-method-creates-terrible-hang – Hassan Dec 01 '13 at 00:52
  • How did you fix this problem? I have a similar issue and following the above listed SO answer didn't solve mine. – Willam Hill Dec 05 '13 at 00:13
  • Can I see the code you are using? I used the code in the answer and configured it for my use – Hassan Dec 06 '13 at 12:42

2 Answers2

2
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    ....
    self.highlightView.frame = highlightViewRect;
    [_session stopRunning]; //<---I add this and it worked for me.
}

Here is a good link that might help.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

You need to stop the captureSesson using: captureSession.stopRunning() once the barcode is scanned, otherwise it will keep scanning the code even if videoPreviewLayer is stopped.

S.S.D
  • 1,579
  • 2
  • 12
  • 23