简体   繁体   English

无法使用Firebase的MLKit从图像中检测到条形码

[英]Unable to detect a barcode from an image using Firebase's MLKit

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 . 所以,我试图用MLKit扫描条形码,但是self.barcodeDetector的完成块中的条形码变量中没有条形码self.barcodeDetector?.detect

The UIImage(named: "barcode.jpg") exists and is loaded correctly (and has barcodes). UIImage(名称:“barcode.jpg”)存在并正确加载(并具有条形码)。

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...: 所以问题是条形码var是空的,因此没有达到for循环中的代码......:

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

Ps The error variable = nil, so that's not the problem. Ps错误变量= nil,所以这不是问题。

image used: 使用的图像: 在此输入图像描述

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)

确认您已在pod文件中添加此行:

pod 'Firebase / MLVisionBarcodeModel'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM