我想用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。
1条答案
按热度按时间gkn4icbw1#
你记得去认领德拉格特了吗?self.delagate = QRScannerViewController