2014-04-22 25 views
4

我使用AV Foundation框架進行條碼掃描功能。iOS條碼掃描器

session = [[AVCaptureSession alloc] init]; 
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
NSError *error = nil; 

input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 
if (input) { 
    [session addInput:input]; 
} else { 
    NSLog(@"Error: %@", error); 
} 

output = [[AVCaptureMetadataOutput alloc] init]; 
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; 
[session addOutput:output]; 

output.metadataObjectTypes = [output availableMetadataObjectTypes]; 

prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; 
prevLayer.frame = self.view.bounds; 

使用delegete方法我會得到條形碼結果。即條形碼編號

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection 
{ 
    CGRect highlightViewRect = CGRectZero; 
    AVMetadataMachineReadableCodeObject *barCodeObject; 
    NSString *detectionString = nil; 
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, 
      AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, 
      AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]; 

    for (AVMetadataObject *metadata in metadataObjects) { 
     for (NSString *type in barCodeTypes) { 
      if ([metadata.type isEqualToString:type]) 
      { 
       barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; 
       barcodeString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; 
       break; 
      } 
     } 

     if (barcodeString != nil) 
     { 
      NSLog(@"Barcode String: %@",barcodeString); 
     } 
     else 
      label.text = @"(none)"; 
    } 
} 

注:其工作。但是,主要原因,我沒有得到條碼編號。請你幫我解決這個問題。

+0

什麼是你面對的確切問題?你無法看到結果或沒有得到結果? –

+0

供參考:這是一個關於QR碼掃描的好教程:http://www.ama-dev.com/iphone-qr-code-library-ios-7/對於通過任何類型的代碼掃描,這幾乎是相同的程序AVFoundation – Alexander

回答

2

你的委託方法看起來不錯,但你應該添加:videoGravity到你的AVCaptureVideoPreviewLayer(prevLayer)並開始會話。試着在你的第一種方法的末尾添加幾行代碼:

prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
[self.view.layer addSublayer:prevLayer]; 
[session startRunning]; 
+0

嗨,我在我的應用程序中添加了上面的代碼,但它的工作方式相同, – Ravi

+0

@Ravan您是否創建了兩個不同的prevLayer實例?嘗試在第一種方法中將所有prevLayer更改爲_prevLayer。 –

+1

我正在使用AVpresenceVideoPreviewLayer的_prevLayer對象,但其工作原理相同。 – Ravi

0

在您的m聲明這些對象(實現)文件:

AVCaptureSession *_session; 
AVCaptureDevice *_device; 
AVCaptureDeviceInput *_input; 
AVCaptureMetadataOutput *_output; 
AVCaptureVideoPreviewLayer *_prevLayer; 

UIView *_highlightView; 
UILabel *_label; 

再加入此方法(將被稱爲你):

-(void)readBarcode:(UIViewController*)myView 
{ 
    _highlightView = [[UIView alloc] init]; 
    _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 
    _highlightView.layer.borderColor = [UIColor greenColor].CGColor; 
    _highlightView.layer.borderWidth = 3; 
    [self.view addSubview:_highlightView]; 

    _label = [[UILabel alloc] init]; 
    _label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40); 
    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 
    _label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65]; 
    _label.textColor = [UIColor whiteColor]; 
    _label.textAlignment = NSTextAlignmentCenter; 
    _label.text = @"(none)"; 
    [self.view addSubview:_label]; 

    _session = [[AVCaptureSession alloc] init]; 
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    NSError *error = nil; 

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error]; 
    if (_input) { 
     [_session addInput:_input]; 
    } else { 
     NSLog(@"Error: %@", error); 
    } 

    _output = [[AVCaptureMetadataOutput alloc] init]; 
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; 
    [_session addOutput:_output]; 

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes]; 

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; 
    _prevLayer.frame = self.view.bounds; 
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [self.view.layer addSublayer:_prevLayer]; 

    [_session startRunning]; 

    [self.view bringSubviewToFront:_highlightView]; 
    [self.view bringSubviewToFront:_label]; 
} 

最後加AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection 
{ 
    CGRect highlightViewRect = CGRectZero; 
    AVMetadataMachineReadableCodeObject *barCodeObject; 
    NSString *detectionString = nil; 
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, 
           AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, 
           AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]; 

    for (AVMetadataObject *metadata in metadataObjects) { 
     for (NSString *type in barCodeTypes) { 
      if ([metadata.type isEqualToString:type]) 
      { 
       barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; 
       highlightViewRect = barCodeObject.bounds; 
       detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; 
       break; 
      } 
     } 
if (detectionString != nil) 
     { 
      _label.text = detectionString; 
      break; 
     } 
     else 
      _label.text = @"(none)"; 
    } 

    _highlightView.frame = highlightViewRect; 
} 
0

只要看看post。您應該仔細閱讀並檢查蘋果文檔。您可以從該帖子下載示例項目。

我不知道你面對的是什麼問題,因爲你的代碼看起來很好。可能問題是您正在掃描不受支持的條形碼,如DataMatrix

請註冊您的detectionString,看看您是否每次獲得價值。

0

在我的情況我使用串行隊列,因此面臨問題。 總是使用下面的主隊列 [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];