我遵循了各种SO答案以及此博客https://github.com/appcoda/QRCodeReader/blob/master/QRCodeReader/QRScannerController.swift?但问题是它只扫描一种类型的条形码“AVMetadataObject.ObjectType.code128”。
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
let barCodeTypes = [AVMetadataObject.ObjectType.upce,AVMetadataObject.ObjectType.code39,AVMetadataObject.ObjectType.code39Mod43, AVMetadataObject.ObjectType.ean13,AVMetadataObject.ObjectType.ean8,AVMetadataObject.ObjectType.code93, AVMetadataObject.ObjectType.code128,AVMetadataObject.ObjectType.pdf417,AVMetadataObject.ObjectType.qr, AVMetadataObject.ObjectType.aztec]
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects.count == 0 {
print("No Barcode is detected")
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if barCodeTypes.contains(metadataObj.type) {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
_ = video.transformedMetadataObject(for: metadataObj)
if metadataObj.stringValue != nil {
let alert = UIAlertController(title: "Barcode", message: metadataObj.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: {(nil) in UIPasteboard.general.string = metadataObj.stringValue
self.barCode_Generation(barCodeValue: metadataObj.stringValue!)
}))
present(alert, animated: true, completion: nil)
print(metadataObj.stringValue!)
}
}
}