iOS快速扫描多个条形码
我使用以下代码扫描条形码:iOS快速扫描多个条形码,ios,swift,barcode-scanner,Ios,Swift,Barcode Scanner,我使用以下代码扫描条形码: func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { var highlightViewRect = CGRectZero var barCodeObject : AVMetadataOb
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)
}
func captureOutput(captureOutput:AVCaptureOutput!,didOutputMetadataObjects metadataObjects:[AnyObject]!,fromConnection连接:AVCaptureConnection!){
var highlightViewRect=CGRectZero
var barCodeObject:AVMetadataObject!
变量检测字符串:字符串!
让barCodeTypes=[AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeAn13Code,
AVMetadataObjectTypeAna8Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417代码,
AVMetadataObjectTypeQRCode,
AVMetadataObjectTypeAztecCode
]
//扫描仪能够在一次扫描中捕获多个二维条形码。
用于元数据对象中的元数据{
对于barCodeTypes中的barcodeType{
如果metadata.type==barcodeType{
barCodeObject=self.previewLayer.TransformedMetadataObjectFormMetadataObject(元数据为!AVMetadataMachineReadableCodeObject)
highlightViewRect=barCodeObject.bounds
detectionString=(元数据为!avMetadataMachineAdableCodeObject)。stringValue
设len=detectionString.characters.count
打印(“原始=\(检测字符串)”)
如果len==25{
detectionString=detectionString.substringWithRange(范围(开始:detectionString.startIndex.advancedBy(3),结束:detectionString.endIndex.advancedBy(0)))
}否则,如果len>22{
detectionString=detectionString.substringWithRange(范围(开始:detectionString.startIndex.advancedBy(22),结束:detectionString.endIndex.advancedBy(0)))
}
打印(“mod=\(detectionString)”)
}
}
}
self.session.stopRunning()
发送扫描(检测字符串)
self.highlightView.frame=highlightView-rect
self.view.bringsubview到前面(self.highlightView)
}
看起来它应该从同一次扫描中捕获多个条形码。然而,它只捕获了一个。我肯定我在这里做错了什么,但我不确定是什么。同时检测的最大数量是4,这个数字仅适用于二维条形码。一维条码识别仅限于一次检测。有关更多信息,请参阅此参考: 运行您的代码(就所提供的范围而言),可以获得多个(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.