2015-12-21 78 views
0

我用下面的代碼掃描條形碼:iOS版雨燕掃描多個條形碼

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 

    var highlightViewRect = CGRectZero 

    var barCodeObject : AVMetadataObject! 

    var detectionString : String! 

    let barCodeTypes = [AVMetadataObjectTypeUPCECode, 
     AVMetadataObjectTypeCode39Code, 
     AVMetadataObjectTypeCode39Mod43Code, 
     AVMetadataObjectTypeEAN13Code, 
     AVMetadataObjectTypeEAN8Code, 
     AVMetadataObjectTypeCode93Code, 
     AVMetadataObjectTypeCode128Code, 
     AVMetadataObjectTypePDF417Code, 
     AVMetadataObjectTypeQRCode, 
     AVMetadataObjectTypeAztecCode 
    ] 


    // The scanner is capable of capturing multiple 2-dimensional barcodes in one scan. 

    for metadata in metadataObjects { 
     for barcodeType in barCodeTypes { 

      if metadata.type == barcodeType { 
       barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject) 

       highlightViewRect = barCodeObject.bounds 

       detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue 

       let len = detectionString.characters.count 
       print("raw=\(detectionString)") 
       if len == 25 { 
        detectionString=detectionString.substringWithRange(Range<String.Index>(start: detectionString.startIndex.advancedBy(3), end: detectionString.endIndex.advancedBy(0))) 
       } else if len > 22 { 
        detectionString=detectionString.substringWithRange(Range<String.Index>(start: detectionString.startIndex.advancedBy(22), end: detectionString.endIndex.advancedBy(0))) 
       } 
       print("mod=\(detectionString)") 
      } 

     } 
    } 

    self.session.stopRunning() 
    sendScan(detectionString) 

    self.highlightView.frame = highlightViewRect 
    self.view.bringSubviewToFront(self.highlightView) 

} 

它看起來像它應該來自同一掃描捕獲多個條碼。但是,它只捕獲一個。我確信我在這裏做錯了事,但我不確定是什麼。

回答

1

同時檢測的最大數量是4,該數字僅適用於二維條形碼。 1維條形碼識別被限制爲1次檢測。更多信息請參見本參考:

Apple: Technical Note TN2325

運行的代碼(儘可能提供),讓我多(2D)檢測。請注意,sendScan方法僅發送上次檢測到的項目。

少許修改代碼:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 

    var captures : [String] = [] 

    for metadata in metadataObjects { 

     var detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue 


     let len = detectionString.characters.count 
     print("raw=\(detectionString)") 
     if len == 25 { 
      detectionString = detectionString.substringWithRange(Range<String.Index>(start: detectionString.startIndex.advancedBy(3), end: detectionString.endIndex.advancedBy(0))) 
     } else if len > 22 { 
      detectionString = detectionString.substringWithRange(Range<String.Index>(start: detectionString.startIndex.advancedBy(22), end: detectionString.endIndex.advancedBy(0))) 
     } 
     print("mod=\(detectionString)") 


     captures.append(detectionString) 

    } 

    print("Captured \(captures.count) barcodes.") 

} 

產生這樣的輸出:

raw=www.barcode1.co.za 
mod=www.barcode1.co.za 
raw=http://www.moxx.in 
mod=http://www.moxx.in 
raw=http://www.harrowhouse.com/ 
mod=.com/ 
raw=Ver1 
mod=Ver1 
Captured 4 barcodes.