我想使用
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 = [[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。
您记得领取延迟吗? self.delagate = QRScannerViewController