3

How is it possible to make a AVCaptureSession only scan AVCaptureMetadataOutput ONCE. I have been having issues with it scanning one barcode over 30 times, delaying the scan sound by around 2-3 secs and then it goes beeping crazy (literally) and displaying ~30 UIAlertViews!!

Code below is my attempt to only scan once...

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {

        CGRect highlightViewRect = CGRectZero;
        AVMetadataMachineReadableCodeObject *barCodeObject;
        NSString *detectionString = nil;
        NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code];

        for (AVMetadataObject *metadata in metadataObjects) {
            for (NSString *type in barCodeTypes) {
                if ([metadata.type isEqualToString:type])
                {
                    barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                    highlightViewRect = barCodeObject.bounds;
                    detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                    break;
                }
            }
            if (detectionString != nil)
            {
                [_session removeOutput:_output];
                [_session stopRunning];
                _session = nil;
                _output = nil;
                [_prevLayer removeFromSuperlayer];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/barcodeBeep.wav", [[NSBundle mainBundle] resourcePath]]];
            AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
            [audioPlayer play];

            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
    }

Any help appreciated.

1 Answer 1

2

The barcode scanner in iOS is implemented as part of the AV pipeline. The scanner will look at each captured image and call the delegate if it recognizes a barcode in the image. So if it recognizes a barcode in 30 consecutive images, it will call the delegate 30 times in a row.

It's up to your app how you want to handle such a case. Some apps might want to be continuously informed about the recognized barcodes. You are obviously only interested in a single recognition event. To achieve this, you have several options:

  1. Stop the video capture completely. This is usually appropriate if your app switches to a different scene if it has captured a barcode.

  2. Remove the barcode scanner (AVCaptureMetadataOutput) from the AV pipeline.

  3. Remember the last captured barcode and the time of the last capture and do not act if the same barcode was captured again within 2 to 3 seconds of the last capture.

The code you posted is incomplete. It could be that you already implement something similar to option 2 (and/or 1). These options might not be sufficient as the AV pipeline might have a backlog of several frames. Once you stop the capturing, it continues to process the frames it has already captures but not processed for barcodes.

I'd expect about five frames to be in the pipeline. If you really experience up to 30 frames, it would indicate that your main thread is too busy to keep up with the capturing process.

So the best approach is likely to implement option 3 (in addition to what you already have) and to make sure your main thread is not too busy.

if (detectionString != nil)
{
    if ([detectionString isEqualToString:_lastCapturedBarcode]
         && [_lastCaptureTime timeIntervalSinceNow] < -3.0)
        return; // do nothing; the barcode was already captured

    _lastCapturedBarcode = detectionString;
    _lastCapturedBarcode = [NSDate date];

    [_session removeOutput:_output];
    [_session stopRunning];
    _session = nil;
    _output = nil;
    [_prevLayer removeFromSuperlayer];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.