7

So, i'am trying to scan a barcode with MLKit, but there are no barcodes in the barcode variable in the completion block of self.barcodeDetector?.detect.

The UIImage(named: "barcode.jpg") exists and is loaded correctly (and has barcodes).

So what am I doing wrong/what am I missing?

private var barcodeDetector: VisionBarcodeDetector?
private lazy var vision = Vision.vision()

override func viewDidLoad() {
    super.viewDidLoad()

    guard let barcodeImage = self.imageView.image else { return }

    let format = VisionBarcodeFormat.all
    let barcodeOptions = VisionBarcodeDetectorOptions(formats: format)
    let barcodeDetector = self.vision.barcodeDetector(options: barcodeOptions)

    let imageMetadata = VisionImageMetadata()
    imageMetadata.orientation = UIUtilities.visionImageOrientation(from: barcodeImage.imageOrientation)

    let visionImage = VisionImage(image: barcodeImage)
    visionImage.metadata = imageMetadata

    self.textView.text = ""

    barcodeDetector.detect(in: visionImage) { (barcodes, error) in
    guard error == nil, let barcodes = barcodes, !barcodes.isEmpty else {
        let errorString = error?.localizedDescription ?? "No error description available"
        self.textView.text = "On-Device barcode detection failed with error: \(errorString)"
        return
    }

    self.textView.text = self.textView.text + "\(self.dateFormatter.string(from: Date())) detecting ...\n"
    self.textView.text = self.textView.text + "barcodes.count = \(barcodes.count)" + "\n"
    print(barcodes.count)

    for barcode in barcodes {
        self.textView.text = self.textView.text + "\(barcode)" + "\n"
        print(barcode)
    }
}

So the problem is that the barcodes var is empty so the code in the for loop is not reached...:

        for barcode in barcodes! {
            print(barcode.rawValue!)
        }

P.s. The error variable = nil, so that's not the problem.

image used: enter image description here

Arjen M
  • 123
  • 1
  • 10
  • Is there an error? any warnings? give us a clue – Scriptable Aug 09 '18 at 10:53
  • There are no barcodes detected, so the barcode variable is empty and thus the for loop is not entered. – Arjen M Aug 09 '18 at 11:07
  • would help if you checked for an output from : `print(error.localizedDescription)` and posted the image you are using. You could also test very quickly with the built in barcode scanner in the standard camera app – Scriptable Aug 09 '18 at 11:23
  • The error variable = nil, so that's not the problem. – Arjen M Aug 09 '18 at 11:36
  • Does it work if you just use an image with a single barcode in? – Ian Barber Aug 13 '18 at 23:00
  • @IanBarber: No unfortunately not... – Arjen M Aug 14 '18 at 06:54
  • Are you using all barcodes together at one image? I used some of my own examples, and had no problems. https://github.com/firebase/quickstart-ios/blob/master/mlvision/MLVisionExample/ViewController.swift#L559 – Ibrahim Ulukaya Aug 14 '18 at 16:24
  • If you can post here some single images, I can debug it. And give it a try with our https://github.com/firebase/quickstart-ios/blob/master/mlvision/Resources/barcode_128.png . https://github.com/firebase/quickstart-ios/blob/master/mlvision/Resources/qr_code.jpg . I want to find out if the issue is on the code, or the SDK failing on the specific barcodes. – Ibrahim Ulukaya Aug 14 '18 at 16:26
  • @IbrahimUlukaya, We've updated our testproject with your suggested barcodes: https://github.com/tychop/TestScan We still have no success in getting barcodes from the barcodeDetector.detect(in: visionImage) method... – Tycho Pandelaar Aug 16 '18 at 09:49
  • Have you tested with the device in landscape mode? I'm facing a similar issue, where no barcodes are detected unless the device is in landscape mode. The orientation in the metadata makes no difference in my case. – rednuht Aug 31 '18 at 17:40
  • yes true same happening with me. @rednuht have you fixed this how? – Pooja M. Bohora Nov 29 '18 at 12:20
  • @PoojaM.Bohora I'm using the native barcode scanning on iOS now. It's as easy as using MLKit, and works much perfectly. http://www.davidgyoungtech.com/2018/01/24/native-bar-code-scanning-in-ios – rednuht Nov 30 '18 at 13:30
  • @rednuht ok but I want to scan on image and not camera – Pooja M. Bohora Dec 03 '18 at 06:32
  • @PoojaM.Bohora If you target iOS 11 you could use the Vision Framework to detect barcodes in images: https://github.com/hansemannn/iOS11-QR-Code-Example - I don't think it's possible with AVFoundation alone. See https://stackoverflow.com/questions/19480466/how-can-i-extract-an-avmetadataobject-from-a-uiimage – rednuht Dec 03 '18 at 15:49

2 Answers2

2

You are most likely missing the "orientation" of the image.

// Define the metadata for the image.
let imageMetadata = VisionImageMetadata()
imageMetadata.orientation = UIUtilities.visionImageOrientation(from: image.imageOrientation)

// Initialize a VisionImage object with the given UIImage.
let visionImage = VisionImage(image: image)
visionImage.metadata = imageMetadata

if that doesn't resolve, try setting the barcode format to "all"

let format = VisionBarcodeFormat.all
let barcodeOptions = VisionBarcodeDetectorOptions(formats: format)
// Create a barcode detector.
let barcodeDetector = vision.barcodeDetector(options: barcodeOptions)
Ibrahim Ulukaya
  • 12,767
  • 1
  • 33
  • 36
1

Verify that you have added this line in your pod file:

pod 'Firebase / MLVisionBarcodeModel'