8

Apple が WWDC2017 で導入した新しい Vision フレームワークのテストを実装しています。私は特にバーコード検出を検討しています-カメラ/ギャラリーから画像をスキャンした後、それがバーコード画像であるかどうかを取得できました。しかし、barcodeDescriptor を見ると、実際のバーコード値やペイロード データがわかりません。https://developer.apple.com/documentation/coreimage/cibarcodedescriptorページには、プロパティを特定するための情報は何も公開されていないようです。

次のエラーが表示されます。

  • リモート サービスに接続できません: エラー Domain=NSCocoaErrorDomain Code=4097 "
    com.apple.BarcodeSupport.BarcodeNotificationService という名前のサービスへの接続"
  • libMobileGestalt MobileGestalt.c:555: InverseDeviceID にアクセスできません (problem/11744455 を参照>)
  • com.apple.BarcodeSupport.BarcodeNotificationService という名前のサービスへの接続 エラー
    Domain=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")
                }
            }
        }
    }
4

3 に答える 3