Scanning barcode with AVCaptureMetadataOutput and AVFoundation

喜夏-厌秋 提交于 2019-12-08 01:32:48

问题


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;

}


回答1:


- (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.




回答2:


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.



来源:https://stackoverflow.com/questions/20300152/scanning-barcode-with-avcapturemetadataoutput-and-avfoundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!