2014-02-11 40 views
4

我目前正在嘗試使用iOS 7的最新api掃描代碼39條形碼,但這讓我瘋狂。爲了讓它檢測到,我必須按照特定的方式保持電話10秒。我將它與Red Laser,Zbar等進行了比較,即使它有一點偏斜,它們也可以在1秒鐘內對其進行分析。我不確定是否因爲我加載捕獲會話的方式或者是什麼。我會很感激的幫助。有關如何提高性能的任何建議?爲什麼使用新的iOS 7 API掃描條形碼真的很慢?

下面是如何加載掃描儀在我viewDidLoad方法:

//Initialize Laser View 
    laserView = [[UIView alloc] init]; 
    laserView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 
    laserView.layer.borderColor = [UIColor redColor].CGColor; 
    laserView.layer.borderWidth = 8; 
    laserView.layer.cornerRadius = 10; 
    [self.view addSubview:laserView]; 

    //Start Session 
    scannerSession = [[AVCaptureSession alloc] init]; 
    scannerDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    //Define Error Messages 
    NSError *error = nil; 

    //Define Input 
    scannerInput = [AVCaptureDeviceInput deviceInputWithDevice:scannerDevice error:&error]; 

    //Check if Device has a Camera 
    if (scannerInput) { 
     [scannerSession addInput:scannerInput]; 
    } else { 
     NSLog(@"Error: %@", error); 
    } 

    // Locks the configuration 
    BOOL success = [scannerDevice lockForConfiguration:nil]; 
    if (success) { 
     if ([scannerDevice isAutoFocusRangeRestrictionSupported]) { 

      // Restricts the autofocus to near range (new in iOS 7) 
      [scannerDevice setAutoFocusRangeRestriction:AVCaptureAutoFocusRangeRestrictionNear]; 
     } 
    } 
    // unlocks the configuration 
    [scannerDevice unlockForConfiguration]; 

    //Define Output & Metadata Object Types 
    scannerOutput = [[AVCaptureMetadataOutput alloc] init]; 
    [scannerOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; 
    [scannerSession addOutput:scannerOutput]; 
    scannerOutput.metadataObjectTypes = [scannerOutput availableMetadataObjectTypes]; 

    //Create Video Preview Layer 
    scannerPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:scannerSession]; 
    scannerPreviewLayer.frame = self.view.bounds; 
    scannerPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [self.view.layer addSublayer:scannerPreviewLayer]; 

    //Start Session 
    [scannerSession startRunning]; 
    [self.view bringSubviewToFront:cancelButton]; 
    [self.view bringSubviewToFront:laserView]; 

和:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { 

    //Prepare Laser View 
    CGRect laser = CGRectZero; 

    //Format Date 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"M/d"]; 

    //Format Time 
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; 
    [timeFormatter setDateFormat:@"h:ma"]; 

    //Define Barcode Types to Recognize 
    AVMetadataMachineReadableCodeObject *barCodeObject; 
    NSString *idNumber = nil; 
    NSArray *barCodeTypes = @[AVMetadataObjectTypeCode39Code]; 

    if ([metadataObjects count] > 1) { 

     NSLog(@"%lu Barcodes Found.", (unsigned long)[metadataObjects count]); 

    } 

    //Get String Value For Every Barcode (That Matches The Type We're Looking For) 
    for (AVMetadataObject *metadata in metadataObjects) { 


     for (NSString *type in barCodeTypes) { 


      //If The Barcode Is The Type We Need Then Get Data 
      if ([metadata.type isEqualToString:type]) { 

       barCodeObject = (AVMetadataMachineReadableCodeObject *)[scannerPreviewLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata]; 
       laser = barCodeObject.bounds; 
       idNumber = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; 
       break; 
      } 
     } 

     // If IDNumber Found 
     if (idNumber != nil) { 

      //Stop Session 
      [scannerSession stopRunning]; 
      [self vibrate]; 

      NSLog(@"ID: %@", idNumber); 

      break; 
     } 

     //If IDNumber Is Not Found 
     else { 

      NSLog(@"No ID Found."); 
     } 
    } 

    //Update Laser 
    laserView.frame = laser; 
} 
+4

你有沒有時間分析,得到什麼代碼行正在放慢你的應用程序? – Larme

+0

我實際上經歷了完全相反的結果。 – nhgrif

+0

@KingPolygon在提高條碼檢測性能方面運氣良好。我正在經歷類似的結果,並感到困惑。 –

回答

0

很多它與圖像的質量做(焦點,眩光,照明等等)在非常好的條件下,您應該幾乎是瞬間掃描。

0

我建議你把NSLog在您的代理功能captureOutput:

你會看到它被稱爲多次只是掃描條形碼一次。根據Why is allocating or initializing NSDateFormatter considered "expensive"?初始化NSDateFormatter是非常昂貴的操作。

我建議你在委託函數外部創建NSDateFormatter並重新使用它,而不是每次調用該函數時都創建它。這應該會讓你的應用更具響應能力。

0

我有一個與AVCaptureSession類似的問題,捕獲非常緩慢,有時需要很長時間才能完成。

不知道我的解決方案是否對您有好處,但對於尋找這個問題的其他人來說可以是有幫助的,就像我一樣。

AVCaptureSession *captureSession = [AVCaptureSession new]; 
captureSession.sessionPreset = AVCaptureSessionPresetHigh; 

使用此代碼,您可以將相機強制爲高質量預設。

希望這會幫助別人。

+0

爲什麼? 'AVCaptureSessionPresetPhoto'具有更好的分辨率 –

1

嘗試放大一點... videoDevice.videoZoomFactor = 2.0;