首页 > 解决方案 > 是否可以从 iOS 读取 QR 码的纠错值?

问题描述

所有 QR 码都定义了一个纠错级别,由左下角的两个“位/像素”定义(有关详细信息,请参见此处。)虽然我知道如何在 Swift 中扫描 QR 码,但我不知道如何了解我正在扫描的代码的纠错。

这是必需的,因为我们的应用程序正在尝试扫描,然后以编程方式重新创建扫描的 QR 码。

这是我们当前的代码,我们会在其中收到成功扫描 QR 码的通知,但我看不到任何获取 QR 特定详细信息的方法,只有成功扫描的结果......

extension QRCodeScannerViewController : AVCaptureMetadataOutputObjectsDelegate {

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

        guard let metadataObject = metadataObjects.first,
              let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject,
              let stringValue = readableObject.stringValue else {
            return
        }

        captureSession.stopRunning()

        performSegue(withIdentifier: "ShowResults", sender: stringValue)
    }
}

浏览文档,我还看到CIQRCodeDescriptorhttps://developer.apple.com/documentation/coreimage/ciqrcodedescriptor确实具有我们正在寻找的价值,但我不确定如何获取要检查的实例。

标签: swiftqr-codeerror-correction

解决方案


呸!它就在我面前!

if let qrCodeDescriptor = readableObject.descriptor as? CIQRCodeDescriptor{

    switch qrCodeDescriptor.errorCorrectionLevel {
        case .levelL : print("L")
        case .levelM : print("M")
        case .levelH : print("H")
        case .levelQ : print("Q")
    }
}

推荐阅读