我正在尝试从用户选择的图像中检测条形码。我能够从图像中检测到QR码,但是找不到任何与从图像扫描条形码相关的内容。我用来从图像中检测QR码的代码如下:
func detectQRCode(_ image: UIImage?) -> [CIFeature]? {
if let image = image, let ciImage = CIImage.init(image: image){
var options: [String: Any]
let context = CIContext()
options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let qrDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: options)
if ciImage.properties.keys.contains((kCGImagePropertyOrientation as String)){
options = [CIDetectorImageOrientation: ciImage.properties[(kCGImagePropertyOrientation as String)] ?? 1]
}else {
options = [CIDetectorImageOrientation: 1]
}
let features = qrDetector?.features(in: ciImage, options: options)
return features
}
return nil
}
当我进入 CIDetectorTypeQRCode
的文档时,它说:
/* Specifies a detector type for barcode detection. */
@available(iOS 8.0, *)
public let CIDetectorTypeQRCode: String
虽然这是QR码类型的文档,但它也可以检测条形码。但是,当我使用相同的函数解码条形码时,它会返回一个空数组的功能。即使它返回一些功能,我如何转换为
CIQRCodeFeature
的条形码替代品呢?我在CIQRCodeFeature
的文档中没有看到任何条形码替代品。我知道使用ZBar SDK可以做到这一点,但我试图避免在这里使用任何第三方库,或者在这方面必须使用它吗?请帮助,非常感谢。
CIDetectorTypeQRCode
只能检测 QR 码。唯一支持的类型可以在文档 https://developer.apple.com/documentation/coreimage/cidetector 中找到。如果您不想依赖第三方库,您将需要自己编写一个CIDetector
,但我无法确定这种尝试会有多复杂。 - jmsVNDetectBarcodesRequest
。 - rmaddyCIRectangleFeature
被支持。你需要自己编写一个包装器来检测条形码。这似乎是一个非常有趣的练习^^ - jms