AVCaptureSession输出未正确显示,并且未调用AVCaptureMetadataOutputObjectsDelegate方法

时间:2013-09-25 15:00:27

标签: ios avfoundation ios7 avcapturesession

我有一个单一的视图应用程序,我试图根据this解释来测试iOS7的AVCaptureMetadataOutput。我的ViewController符合AVCaptureMetadataOutputObjectsDelegate,代码看起来像这样(几乎和Mattt一样):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Testing the VIN Scanner before I make it part of the library
    NSLog(@"Setting up the vin scanner");
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                        error:&error];
    if (input) {
        [session addInput:input];
    } else {
        NSLog(@"Error: %@", error);
    }

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutput:output];

    [session startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
       fromConnection:(AVCaptureConnection *)connection
{
    NSString *code = nil;
    for (AVMetadataObject *metadata in metadataObjects) {
        if ([metadata.type isEqualToString:AVMetadataObjectTypeCode39Code]) {
            code = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
            break;
        }
    }

    NSLog(@"code: %@", code);
}

当我在iOS7设备上运行时(我尝试过iPhone 4和iPhone 4s)XCode记录“设置vin扫描仪”但摄像机(即AVCaptureSession)从未打开过。

编辑1:

我添加了以下代码以在屏幕上显示相机输出:

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

// Display full screen    
previewLayer.frame = self.view.frame;

// Add the video preview layer to the view
[self.view.layer addSublayer:previewLayer];

但是显示很奇怪,不符合屏幕,旋转的方式没有意义。另一个问题是,当我将相机聚焦在条形码上时,永远不会调用元数据委托方法。请看下面的图片:

Portait Screen Shot Landscape Screen shot

1 个答案:

答案 0 :(得分:6)

相机无法以UIImagePickerController的方式打开。问题是您的代码对输出没有任何作用。您需要添加预览图层以在流入时显示摄像机的输出。

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

// Display full screen    
previewLayer.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);

// Add the video preview layer to the view
[self.view.layer addSublayer:previewLayer];

[session startRunning];

编辑**
在深入了解您的代码后,我发现了一些问题。

首先,您还需要设置要搜索的MetaDataObjectTypes,此时您不需要查找任何有效的对象类型。将输出添加到会话后,应添加此项。您可以在documentation

中查看可用类型的完整列表
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]];

第二个你的AVCaptureSession *会话是viewDidLoad中的局部变量,取下它并将它放在@interface ViewController()之后,如下所示。

@interface ViewController ()
@property (nonatomic, strong) AVCaptureSession *session;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.session = [[AVCaptureSession alloc] init];

    // Testing the VIN Scanner before I make it part of the library
    NSLog(@"Setting up the vin scanner");
    self.session = [[AVCaptureSession alloc] init];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                    error:&error];
    if (input) {
        [self.session addInput:input];
    } else {
        NSLog(@"Error: %@", error);
    }

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [self.session addOutput:output];

    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]];

    [self.session startRunning];
}