我正在使用AVFoundation和AVCaptureMetadataOutput来扫描iOS7中的QR条形码,我提供了一个允许用户扫描条形码的视图控制器。它工作正常,即。正在扫描条形码,我可以将条形码字符串输出到控制台。
但是它一遍又一遍地扫描,看屏幕截图。我想要它做的是扫描条形码只是一次然后dismissViewController。
以下是委托方法的代码:
- (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;
}
答案 0 :(得分:2)
- (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可能有所帮助。
答案 1 :(得分:0)
您需要使用以下命令停止captureSesson:
扫描条形码后captureSession.stopRunning()
,否则即使videoPreviewLayer
停止,它也会继续扫描代码。