So, I have to scan different barcodes with various colours. For example, a yellow barcode on black background or yellow barcode on white background.
I don't have any issues with them being recognized by traditional linear and CCD barcode scanners. I have tried using Apple Vision framework but it doesn't work on them. They work perfectly fine on black barcodes with white background.
My barcodes are all Code 128 so I use this code for it:
var barcodeObservations: [String : VNBarcodeObservation] = [:]
for barcode in barcodes {
if let detectedBarcode = barcode as? VNBarcodeObservation {
if detectedBarcode.symbology == .code128 {
barcodeObservations[detectedBarcode.payloadStringValue!] = detectedBarcode
}
}
}
And in 'captureOutput' function under AVCaptureVideoDataOutputSampleBufferDelegate, I use this to filter my live feed as black and white which helps in the recognition of the golden barcode on silver background (The first image):
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)
How can I make the Vision Framework detect barcodes with invert colors?
The 'CIColorInvert' filter doesn't work.
Edit: These are the barcodes: