iOS 11 的 Vision 框架条码检测功能

8
我正在实施苹果在WWDC2017中介绍的新Vision框架的测试。我特别关注条形码检测 - 我已经能够在从相机/相册扫描图像后确定它是否为条形码图像。但是,当查看barcodeDescriptor时,我无法看到实际的条形码值或有效载荷数据。在https://developer.apple.com/documentation/coreimage/cibarcodedescriptor页面上似乎没有任何暴露属性的内容。
我遇到了以下错误:
  • 无法连接到远程服务:错误域=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.BarcodeSupport.BarcodeNotificationService"
  • libMobileGestalt MobileGestalt.c:555: no access to InverseDeviceID (see problem/11744455>)
  • 连接到名为com.apple.BarcodeSupport.BarcodeNotificationService的服务时出错 错误域=NSCocoaErrorDomain Code=4097
有没有办法从VNBarcodeObservation中访问条形码值?非常感谢您的帮助。谢谢!这是我使用的代码:
@IBAction func chooseImage(_ sender: Any) {
        imagePicker.allowsEditing = true
        imagePicker.sourceType = .photoLibrary

        present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func takePicture(_ sender: Any) {
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            self .present(imagePicker, animated: true, completion: nil)
        }
        else{
            let alert = UIAlertController(title: "Warning", message: "Camera not available", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }

    //PickerView Delegate Methods

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        imagePicker .dismiss(animated: true, completion: nil)
        classificationLabel.text = "Analyzing Image…"

        guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
            else { fatalError("no image from image picker") }
        guard let ciImage = CIImage(image: pickedImage)
            else { fatalError("can't create CIImage from UIImage") }

        imageView.image = pickedImage
        inputImage = ciImage

        // Run the rectangle detector, which upon completion runs the ML classifier.
        let handler = VNImageRequestHandler(ciImage: ciImage, options: [.properties : ""])
        DispatchQueue.global(qos: .userInteractive).async {
            do {
                try handler.perform([self.barcodeRequest])
            } catch {
                print(error)
            }
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
        picker .dismiss(animated: true, completion: nil)
        print("picker cancel.")
    }

    lazy var barcodeRequest: VNDetectBarcodesRequest = {
        return VNDetectBarcodesRequest(completionHandler: self.handleBarcodes)
    }()

    func handleBarcodes(request: VNRequest, error: Error?) {
        guard let observations = request.results as? [VNBarcodeObservation]
            else { fatalError("unexpected result type from VNBarcodeRequest") }
        guard observations.first != nil else {
            DispatchQueue.main.async {
                self.classificationLabel.text = "No Barcode detected."
            }
            return
        }

        // Loop through the found results
        for result in request.results! {

            // Cast the result to a barcode-observation
            if let barcode = result as? VNBarcodeObservation {

                // Print barcode-values
                print("Symbology: \(barcode.symbology.rawValue)")

                if let desc = barcode.barcodeDescriptor as? CIQRCodeDescriptor {
                    let content = String(data: desc.errorCorrectedPayload, encoding: .utf8)

                    // FIXME: This currently returns nil. I did not find any docs on how to encode the data properly so far.
                    print("Payload: \(String(describing: content))\n")
                    print("Error-Correction-Level: \(desc.errorCorrectedPayload)\n")
                    print("Symbol-Version: \(desc.symbolVersion)\n")
                }
            }
        }
    }

请查看WWDC 2017 - 会话#510 - Core Image的进展:滤镜,Metal,Vision等,在35分钟处开始讨论CIBarcodeDescriptor和errorCorrectedPayload。不幸的是,我也无法读取负载中的消息。 - nathan
是的,我能够通过在“编辑方案”->运行->参数中添加“OS_ACTIVITY_MODE”为禁用来修复这些错误,但我无法从有效负载中提取数据。我能够扫描条形码,然后制作出视频中展示的图像,但我仍然无法从数据类型中提取信息字符串。 - Hitesh Arora
我已经以你的代码为基础进行了自己的测试,并且在 iOS 11 beta 3 上有了一些运气,但结果令人困惑。errorCorrectedPayload 包含的数据有时是可读的。https://stackoverflow.com/questions/45037418/zlib-stream-in-ios-11-vision-framework-barcodes-sometimes-decompress-sometimes - oelna
来自苹果支持团队的更新:工程师已经确定,根据以下信息,此问题的行为符合预期:您需要编写自己的解析器。 - nathan
3个回答

9

显然,在iOS 11 beta 5中,苹果公司推出了VNBarcodeObservation的新属性payloadStringValue。现在你可以轻松读取QR码中的信息。

if let payload = barcodeObservation.payloadStringValue {
    print("payload is \(payload)")
}

2
如果苹果不提供这方面的库,可以使用类似以下代码实现:
extension CIQRCodeDescriptor {
    var bytes: Data? {
        return errorCorrectedPayload.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
            var cursor = pointer

            let representation = (cursor.pointee >> 4) & 0x0f
            guard representation == 4 /* byte encoding */ else { return nil }

            var count = (cursor.pointee << 4) & 0xf0
            cursor = cursor.successor()
            count |= (cursor.pointee >> 4) & 0x0f

            var out = Data(count: Int(count))
            guard count > 0 else { return out }

            var prev = (cursor.pointee << 4) & 0xf0
            for i in 2...errorCorrectedPayload.count {
                if (i - 2) == count { break }

                let cursor = pointer.advanced(by: Int(i))
                let byte = cursor.pointee
                let current = prev | ((byte >> 4) & 0x0f)
                out[i - 2] = current
                prev = (cursor.pointee << 4) & 0xf0
            }
            return out
        }
    }
}

接下来

String(data: descriptor.bytes!, encoding: .utf8 /* or whatever */)

你能解释/链接一下representation变量的保护吗?我正在尝试解析CIQRCodeDescriptor,但是我的representation值(即2)导致我的descriptor.bytes为nil。 - raoul
1
你的表示是“字母数字”,因此你需要一个不同的算法。http://www.thonky.com/qr-code-tutorial/alphanumeric-mode-encoding - Nick Kallen

0

如果您想直接从VNBarcodeObservation获取原始数据,而无需符合某些字符串编码,您可以像这样剥离前2个和1/2字节,从而获得实际数据而不带有QR代码头。

            guard let barcode = barcodeObservation.barcodeDescriptor as? CIQRCodeDescriptor else { return }
            let errorCorrectedPayload = barcode.errorCorrectedPayload
            let payloadData = Data(bytes: zip(errorCorrectedPayload.advanced(by: 2),
                                              errorCorrectedPayload.advanced(by: 3)).map { (byte1, byte2) in
                return byte1 << 4 | byte2 >> 4
            })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接