我在iOS 7中測試新的條形碼掃描API時遇到了問題。此示例(單視圖應用程序)工作正常,但我想停止AVCaptureSession並在識別出EAN代碼後顯示第一個視圖由相機。iOS正確停止AVCaptureSession
[self.captureSession startRunning];
不起作用。
如何正確停止AVCaptureSession?
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController() <AVCaptureMetadataOutputObjectsDelegate>
@property (strong) AVCaptureSession *captureSession;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(videoInput)
[self.captureSession addInput:videoInput];
else
NSLog(@"Error: %@", error);
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
previewLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:previewLayer];
[self.captureSession startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
for(AVMetadataObject *metadataObject in metadataObjects)
{
AVMetadataMachineReadableCodeObject *readableObject = (AVMetadataMachineReadableCodeObject *)metadataObject;
if([metadataObject.type isEqualToString:AVMetadataObjectTypeQRCode])
{
NSLog(@"QR Code = %@", readableObject.stringValue);
}
else if ([metadataObject.type isEqualToString:AVMetadataObjectTypeEAN13Code])
{
NSLog(@"EAN 13 = %@", readableObject.stringValue);
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
喜代工,感謝您的回覆。我只是想將已識別的ean解析爲變量以備後用,並立即關閉捕獲會話。 – Boeringer
@ Boeringer-見我的更新。希望能幫助到你... – foundry