适用于iOS 11的Vision Framework条形码检测 [英] Vision Framework Barcode detection for iOS 11

查看:735
本文介绍了适用于iOS 11的Vision Framework条形码检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对Apple在WWDC2017中引入的新Vision框架进行测试.我正在专门查看条形码检测-从相机/图库扫描图像后,我已经能够知道它是否是条形码图像.但是,当我查看BarcodeDescriptor时,看不到实际的条形码值或有效载荷数据. https://developer.apple.com/documentation/coreimage/cibarcodedescriptor上似乎没有暴露任何内容页以标识任何属性.

I've been implementing a test of the new Vision framework which Apple introduced in WWDC2017. I am specifically looking at the barcode detection - I've been able to get after scanning the image from Camera/Gallery that it's a barcode image or not. However, I can't see what the actual barcode value or the payload data when looking at the barcodeDescriptor. There appears to be nothing exposed on the https://developer.apple.com/documentation/coreimage/cibarcodedescriptor page to identify any of the properties.

我遇到这些错误:

  • 无法连接到远程服务:错误域= NSCocoaErrorDomain代码= 4097连接到名为
    的服务 com.apple.BarcodeSupport.BarcodeNotificationService"
  • libMobileGestalt MobileGestalt.c:555:无法访问InverseDeviceID(请参阅问题/11744455>)
  • 连接到名为com.apple.BarcodeSupport.BarcodeNotificationService的服务错误
    域= NSCocoaErrorDomain代码= 4097
  • Cannot connect to remote service: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named
    com.apple.BarcodeSupport.BarcodeNotificationService"
  • libMobileGestalt MobileGestalt.c:555: no access to InverseDeviceID (see problem/11744455>)
  • connection to service named com.apple.BarcodeSupport.BarcodeNotificationService Error
    Domain=NSCocoaErrorDomain Code=4097

是否可以通过VNBarcodeObservation访问条形码值? 任何帮助将不胜感激.谢谢! 这是我正在使用的代码:

Is there any way to access the barcode value from the VNBarcodeObservation? Any help would be greatly appreciated. Thank you! Here is the code I am using:

@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")
                }
            }
        }
    }

推荐答案

显然,在iOS 11 beta 5中,Apple引入了新的 payloadStringValue 属性.现在,您可以毫无问题地从QR码中读取信息了

Apparently, in the iOS 11 beta 5 Apple introduced new payloadStringValue property of VNBarcodeObservation. Now you can read info from QR-code with no problems

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

这篇关于适用于iOS 11的Vision Framework条形码检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆