2013-09-25 48 views
1

我有一個單視圖應用程序,其中我試圖根據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]; 

但是顯示效果很奇怪,不符合屏幕,它旋轉的方式做沒有道理。另一個問題是,當我將相機聚焦在條形碼上時,從不會調用元數據委託方法。請看下面的圖片:

Portait Screen Shot Landscape Screen shot

+0

你是什麼意思,永遠不會打開?你測試了什麼? – Spectravideo328

+0

當視圖加載到我的iPhone上時,avcapturesession不會啓動,因爲相機不會爲我捕獲av。我上面的內容實際上是單個視圖應用程序中的所有附加代碼,再加上AVFoundation的導入。 – JuJoDi

回答

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視圖控制器()爲後。

@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]; 
} 
+0

我認爲這讓我更接近,我已經編輯了更清晰的問題。至少現在相機顯示,雖然絕對不正確 – JuJoDi

+0

完美,非常感謝你 – JuJoDi

+1

喬,我的兒子,你做了一個夢幻般的工作回答這個。我爲你感到驕傲。 - 爸 –