Warm tip: This article is reproduced from serverfault.com, please click

Firebase Barcode Scanner for IOS

发布于 2020-12-31 17:50:31

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")
      // ...
    }
    
}
Questioner
Manov Jain
Viewed
0
valosip 2021-01-01 02:07:29

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)
        // ...
     }
}