AVCaptureMetadataOutput 委托方法未调用

问题描述 投票:0回答:1

我想使用

AVFoundation > AVCaptureMetadataOutput
类扫描二维码。我的代码如下,

@interface QRScannerViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>{

- (void)startReading {
    NSError *error;
    
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    
    if(!deviceInput) {
        NSLog(@"deviceInput Error %@", error.localizedDescription);
    }
    [_captureSession addInput:[[AVCaptureSession alloc]init]];
    [_captureSession addOutput:[[AVCaptureMetadataOutput alloc] init]];
    
    dispatch_queue_t dispatchQueue = dispatch_queue_create("myQueue", NULL);
    [capturedMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    [capturedMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
    
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
        
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self->_captureSession startRunning];
    });
}

- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    
    if (metadataObjects != nil && metadataObjects.count > 0) {
        
        NSLog(@"%@", [metadataObjects objectAtIndex:0]);
         
        }
    }
}

我不明白为什么我的

captureOutput:output didOutputMetadataObjects:metadataObjects fromConnectionconnection
没有接到电话。我将 AVMetadataObjectTypeQRCode 元数据值设置为 AVCaptureMetadataOutput。

ios swift objective-c avfoundation
1个回答
0
投票

您记得领取延迟吗? self.delagate = QRScannerViewController

© www.soinside.com 2019 - 2024. All rights reserved.