所以,我需要扫描不同颜色的条形码。例如,黑色背景上的黄色条形码或白色背景上的黄色条形码。
传统的线性和CCD条形码扫描器可以很好地识别它们,我没有遇到任何问题。我尝试使用苹果视觉框架,但它不能识别这些条形码。在白色背景上的黑色条形码上它能够完美运行。
我的条形码都是Code 128,所以我使用这段代码:
var barcodeObservations: [String : VNBarcodeObservation] = [:]
for barcode in barcodes {
if let detectedBarcode = barcode as? VNBarcodeObservation {
if detectedBarcode.symbology == .code128 {
barcodeObservations[detectedBarcode.payloadStringValue!] = detectedBarcode
}
}
}
在AVCaptureVideoDataOutputSampleBufferDelegate的'captureOutput'函数中,我使用这个函数将我的实时视频流转换成黑白模式,这有助于识别银色背景上的金色条形码(第一张图片):
let context = CIContext(options: nil)
let currentFilter = CIFilter(name: "CIPhotoEffectMono")
currentFilter!.setValue(CIImage(cvImageBuffer: pixelBuffer), forKey: kCIInputImageKey)
let output = currentFilter!.outputImage!
context.render(output, to: pixelBuffer)
如何使Vision Framework检测反色条形码?
“CIColorInvert”滤镜不起作用。
编辑:这些是条形码:
我的条形码都是 Code 128,所以我使用这个代码
- 所以接受不同颜色的条形码
意味着我认为你需要使用所有支持的符号 - https://developer.apple.com/documentation/vision/vndetectbarcodesrequest/2879281-supportedsymbologies - Ram