Swift:无法检测线性类型条形码 [英] Swift: Unable to detect linear type Barcodes

查看:147
本文介绍了Swift:无法检测线性类型条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写条形码阅读器应用程序,但无法检测线性类型的条形码,即代码128.我对Qr代码,EAN13类型和ISBN类型没有任何疑问.我的代码非常简单:

I am writing a barcode reader app but I am unable to detect linear type barcodes, ie code 128. I have no issues with Qr codes, EAN13 types and ISBN types. My code is pretty straightforward:

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

        if metadataObjects.count != 0 {
            if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
                if object.type == AVMetadataObject.ObjectType.qr {
                    if let text = object.stringValue {
                        print(text)

                        session.stopRunning()

                        let alertVC = UIAlertController(title: "QR Code", message: text, preferredStyle: UIAlertControllerStyle.alert)
                        alertVC.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                            self.session.startRunning()
                        }))
                        present(alertVC, animated: true, completion: nil)
                    }
                } else {
                    if let text = object.stringValue {
                        print("Other code detected: ", text)
                    }
                }
            }
        }

我使用了免费在线条形码生成器测试我的应用程序.除线性代码外,我还能检测到其他大多数条形码.有人也遇到过这样的问题吗?

I used this free online barcode generator to test my app. I am able to detect most of other barcodes, except Linear Codes. Has anyone encountered such problems too?

推荐答案

在设置AVCaptureMetadataOutput对象类型metadataObjectTypes时,请确保包括AVMetadataObjectTypeCode128Code [.code128].

Make sure to include AVMetadataObjectTypeCode128Code [.code128] when setting AVCaptureMetadataOutput object types metadataObjectTypes.

metadataOutput.metadataObjectTypes = [.qr, .ean13, .code128]

如果要允许所有可用的元数据对象类型,可以使用AVCaptureMetadataOutput属性availableMetadataObjectTypes,该属性将返回所有可用的类型:

If you would like to allow all available metadata object types you can use AVCaptureMetadataOutput property availableMetadataObjectTypeswhich return all available types:

metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes

如果您要允许除所有面之外的所有面(第一个面),则可以删除availableMetadataObjectTypes的第一个元素.请注意,Apple可能会在不久的将来更改元数据元素的顺序,因此最好手动选择应用程序所需的条形码类型:

If you would like to allow all of then except the faces which is the first one, you can drop the first element of the availableMetadataObjectTypes. Note that Apple might change the metadata elements order in the near future so it is better to manually select only the barcode types required by your app:

metadataOutput.metadataObjectTypes =  Array(metadataOutput.availableMetadataObjectTypes.dropFirst())

关于您的实际代码的几点说明.您不应该检查元素计数是否等于零来检查其是否为空.数组具有一个恰好适合该if !metadataObjects.isEmpty { //...的名为isEmpty的属性.另一个选择是,如果仅使用数组的第一个元素,我建议使用数组.first而不是下标[0],它返回可选元素AVMetadataObject?,而AVMetadataObject.ObjectType在您的比较中是多余的:

Just a few notes on your actual code. You shouldn't check the elements count if it is equal to zero to check if it is empty. Array has a property called isEmpty exactly for that if !metadataObjects.isEmpty { //.... Another option is if only the first element of the array is used I recommend using Array .first instead of subscript [0] which returns an optional element AVMetadataObject? and AVMetadataObject.ObjectType is redundant in your comparison:

if let object = metadataObjects.first as? AVMetadataMachineReadableCodeObject {
    if object.type == .qr {
        if let text = object.stringValue {
            print(text)
            session.stopRunning()
            let alertVC = UIAlertController(title: "QR Code", message: text, preferredStyle: .alert)
            alertVC.addAction(UIAlertAction(title: "Ok", style: .default) { _ in
                self.session.startRunning()
            })
            present(alertVC, animated: true)
        }
    }
    else
    if let text = object.stringValue {
        print("Other code detected: ", text)
    }
}

这篇关于Swift:无法检测线性类型条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆