Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

swift - Firebase Barcode Scanner for IOS

So I am trying to use Firebase's barcode scanning API for my IOS application. Everything is running error-free (for now) but I am not sure how to access the barcode information once the picture is sent to the .detect function.

In the documentation on the firebase website it says that: "If the barcode recognition operation succeeds, the detector returns an array of VisionBarcode objects. Each VisionBarcode object represents a barcode that was detected in the image."

Could someone help me understand how to access the barcodes array? Below I have included a link to the documentation (where I am getting the code from) and pictures of my code.

Code

https://firebase.google.com/docs/ml-kit/ios/read-barcodes#swift_7

    func scanBarcode(userImage: UIImage){
    print("SCAN")
    let format = VisionBarcodeFormat.all
    let barcodeOptions = VisionBarcodeDetectorOptions(formats: format)
    var vision = Vision.vision()
    let barcodeDetector = vision.barcodeDetector(options: barcodeOptions)
    let visionImage = VisionImage(image: userImage)
    barcodeDetector.detect(in: visionImage) { features, error in
      guard error == nil, let features = features, !features.isEmpty else {
      //  print(barcodes)
        return
      }
        print("unsuccesful scan")
      // ...
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your logic for a successful scan is a little off: you need to print the features after your guard

func scanBarcode(userImage: UIImage){
    print("SCAN")
    let format = VisionBarcodeFormat.all
    let barcodeOptions = VisionBarcodeDetectorOptions(formats: format)
    var vision = Vision.vision()
    let barcodeDetector = vision.barcodeDetector(options: barcodeOptions)
    let visionImage = VisionImage(image: userImage)
    barcodeDetector.detect(in: visionImage) { features, error in
        guard error == nil, let features = features, !features.isEmpty else {
            print("unsuccessful scan, either there is an error, or features is empty")
            return
        }
        print("successful scan")
        print(features)
        // ...
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...