我在我的应用程序中使用二维码,它也以 AVMetadataMachineReadableCodeObject 的形式给我响应。这是我处理响应的委托(delegate)。
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
if strFound == ""
{
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameVieww?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
// Get the metadata object.
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if supportedCodeTypes.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
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
//qrCodeFrameVieww?.frame = barCodeObject!.bounds
if defaults.value(forKey: "beep") as! Bool != false
{
playSound()
}
if defaults.value(forKey: "vibrate") as! Bool != false
{
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
}
print("type is",metadataObj.type)
print("metaDataObj",metadataObj)
foundCode(code: metadataObj.stringValue as NSString)
if metadataObj.stringValue != nil {
let stUrl = metadataObj.stringValue as String
print("The Url Is \(stUrl)")
if defaults.value(forKey: "copyToClipboard") as! Bool == true
{
UIPasteboard.general.string = stUrl
}
我的问题是二维码可以扫描电子名片、电话号码、电子邮件、地理位置和日历事件等。如果它与vcard 或者它是一个 url 或一个日历事件??
最佳答案
guard let metadataObject = metadataObjects.first as? AVMetadataMachineReadableCodeObject else {
return
}
guard let codeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObject) else {
return
}
switch codeObject.type {
case .AVMetadataObjectTypeQRCode:
//...
default:
break
}
关于Ios - 识别二维码响应类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43136201/