ios 未调用AVCaptureMetadataOutput委托方法

gab6jxml  于 2023-10-21  发布在  iOS
关注(0)|答案(1)|浏览(102)

我想用AVFoundation > AVCaptureMetadataOutput类扫描QR码。我的代码如下,

@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 = [[AVCaptureSession alloc]init];
[_captureSession addInput:deviceInput];

capturedMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:capturedMetadataOutput];
capturedMetadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];        
    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。

gkn4icbw

gkn4icbw1#

你记得去认领德拉格特了吗?self.delagate = QRScannerViewController

相关问题