AVCaptureMetadataOutput不支持的类型在XCode中发现错误

时间:2015-10-07 20:24:21

标签: objective-c avfoundation

错误日志: 由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' * - [AVCaptureMetadataOutput setMetadataObjectTypes:] - 找不到支持的类型。使用-availableMetadataObjectTypes。' * 首先抛出调用堆栈:

这是调试器日志中的availableMetadataObjectTypes。我不明白为什么这是空的。

(lldb)po [输出availableMetadataObjectTypes] < __ NSArrayM 0x810ae990>()

这是代码     NSError *错误;

session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetHigh];

AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput* deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if ([session canAddInput:deviceInput]) {
    [session addInput:deviceInput];
}

previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

CALayer *rootLayer = [[self scannerView] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(self.scannerView.frame.origin.x, self.scannerView.frame.origin.y, self.scannerView.frame.size.width, self.scannerView.frame.size.height)];
[rootLayer insertSublayer:previewLayer atIndex:0];

_labelBarcode = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 300, 40)];
_labelBarcode.backgroundColor = [UIColor darkGrayColor];
_labelBarcode.textColor = [UIColor whiteColor];
[self.scannerView addSubview:_labelBarcode];

[self rotatePreviewLayerToDeviceOrientation];

[session startRunning];

AVCaptureMetadataOutput* output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
if ([session canAddOutput:output]) {
    [session addOutput:output];
}
[output availableMetadataObjectTypes];
output.metadataObjectTypes = @[
                               AVMetadataObjectTypeAztecCode,
                               AVMetadataObjectTypeCode128Code,
                               AVMetadataObjectTypeCode39Code,
                               AVMetadataObjectTypeCode39Mod43Code,
                               AVMetadataObjectTypeCode93Code,
                               AVMetadataObjectTypeEAN13Code,
                               AVMetadataObjectTypeEAN8Code
                               ];

}

1 个答案:

答案 0 :(得分:6)

您应该检查availableMetadataObjectTypes,但如果您还没有添加输入设备,则该列表将为空。

该提示来自对availableMetadataObjectTypesAVCaptureOutput.h的评论:

  

可用       元数据对象类型取决于此接收器所属的AVCaptureInputPort的功能       AVCaptureConnection已连接。

e.g。我的后置摄像头支持aztec和code128。

NSError *error;
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

// Check input and error here.

[session addInput:input];


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

// NOW try adding metadata types
session.metadataObjectTypes = @[AVMetadataObjectTypeAztecCode,
                           AVMetadataObjectTypeCode128Code];