我有一个单视图应用程序,我正在尝试根据这个解释测试 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];
但是显示很奇怪,不符合屏幕,旋转的方式也没有意义。另一个问题是,当我将相机聚焦在条形码上时,元数据委托方法永远不会被调用。请看下面的图片: