我有一個單視圖應用程序,其中我試圖根據this說明測試iOS7的AVCaptureMetadataOutput。我的ViewController符合AVCaptureMetadataOutputObjectsDelegate
和代碼如下所示(幾乎完全一樣Mattt的):AVCaptureSession輸出未正確顯示,AVCaptureMetadataOutputObjectsDelegate方法未調用
- (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];
但是顯示效果很奇怪,不符合屏幕,它旋轉的方式做沒有道理。另一個問題是,當我將相機聚焦在條形碼上時,從不會調用元數據委託方法。請看下面的圖片:
你是什麼意思,永遠不會打開?你測試了什麼? – Spectravideo328
當視圖加載到我的iPhone上時,avcapturesession不會啓動,因爲相機不會爲我捕獲av。我上面的內容實際上是單個視圖應用程序中的所有附加代碼,再加上AVFoundation的導入。 – JuJoDi