2017-06-21 44 views
5

Apple'ın WWDC2017'de tanıttığı yeni Vision çerçevesinin bir testini gerçekleştiriyorum. Özellikle barkod tespitine bakıyorum - Kamera/Galeri'den görüntüyü taradıktan sonra barkodlu bir resim olsun ya da olmasın. Bununla birlikte, barkodDescriptor'a bakarken gerçek barkod değerinin veya yük bilgisinin ne olduğunu göremiyorum. Özelliklerden herhangi birini tanımlamak için https://developer.apple.com/documentation/coreimage/cibarcodedescriptor sayfasında hiçbir şey görünmemektedir.Vision Framework iOS 11 için Barkod algılama

Bu hataları alıyorum:

  • uzaktan hizmet bağlanamıyor: Hata NSCocoaErrorDomain Kod = 4097
  • libMobileGestalt "bağlantı
    com.apple.BarcodeSupport.BarcodeNotificationService adlı servisine" = Alan MobileGestalt.c: 555: InverseDeviceID'ye erişim yok (bkz. Sorun/11744455>)
  • hizmete verilen bağlantı com.apple.BarcodeSupport.BarcodeNotificationService Error
    Alan = NSCocoaErrorDomain Kod = 4097

VNBarcodeObservation gelen barkod değerini erişmek için herhangi bir yolu var mı? Herhangi bir yardım büyük takdir edilecektir. Teşekkür ederim! İşte kullanıyorum kodu: Elma bunun için bir kütüphane sağlamak için gidiş değilse

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

atın [- - Filtreler, Metal, Vizyon ve Diğer WWDC 2017 Core Image gelişmeler Oturum # 510] herhangi bir sorun ile QR-kodundan bilgi okuyabilir .com/videos/play/wwdc2017/510 /) 35dk'de CIBarcodeDescriptor ve errorCorrectedPayload hakkında konuşmaya başladılar. Ne yazık ki mesajı da yükte okuyamamıştım. – nathan

+0

Evet, "Edit Scheme" -> Run-> Arguments içinde "OS_ACTIVITY_MODE" özelliğini ekleyerek bu hataları düzeltebilirim, ancak veri yükünden veri ayıklayamıyorum. Barkodu tarayabilir ve daha sonra videoda gösterdiklerinden bir görüntü çıkartabiliyorum, ancak veri türünden bilgi dizesi çıkarmak için hala sıkışmış durumdayım. –

+0

Kodunuzu kendi testlerim için temel olarak kullandım ve * bazı şanslar (iOS 11 beta 3) aldım, ancak sonuçlar şaşırtıcıydı. "ErrorCorrectedPayload", * bazen * okunabilen verileri içerir. https://stackoverflow.com/questions/45037418/zlib-stream-in-ios-11-vision-framework-barcodes-sometimes-decompress-sometimes – oelna

cevap

2

, aşağıdaki gibi bir şey çalışır:

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 
     } 
    } 
} 

Sonra

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

' temsil 'değişkeninin korumasını açıklar/bağlar mısınız? Ben 'CIQRCodeDescriptor' ayrıştırmak için çalışıyorum ve 'temsili' değeri (yani, 2) benim 'descriptor.bytes 'nil olmasına neden oluyor. – raoul

+1

Temsiliniz 'alfanümerik' olduğundan farklı bir algoritmaya ihtiyacınız olacak. http://www.thonky.com/qr-code-tutorial/alphanumeric-mode-encoding –

6

Görünüşe göre, iOS 11 beta 5'de Apple, VNBarcodeObservation'un payloadStringValue numaralı özelliğini tanıttı. (Https://developer.apple: Artık

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