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

AVCaptureMetadataOutput unsupported type found error in XCode

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

这是调试器日志中的可用元数据对象类型。我不明白为什么这是空的。

(lldb) po [输出可用元数据对象类型] <__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
                               ];

}

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

提示来自 AVCaptureOutput.h 中对 availableMetadataObjectTypes 的评论:

Available metadata object types are dependent on the capabilities of the AVCaptureInputPort to which this receiver's AVCaptureConnection is connected.

例如我的后置摄像头支持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];