I am trying to generate a codabar barcode but haven't been able to get it working.
Here is my current code for it:
import SwiftUI
import VisionKit
import CoreImage.CIFilterBuiltins
import RSBarcodes_Swift
import AVFoundation
class BarcodeViewModel: ObservableObject {
@Published var barcodeImage: Image? = nil
func generateBarcode(contents: String) {
let gen = RSUnifiedCodeGenerator.shared
gen.fillColor = UIColor.white
gen.strokeColor = UIColor.black
// Specify Codabar barcode type
let machineReadableCodeObjectType = AVMetadataObject.ObjectType.codabar.rawValue
// Ensure compatibility with the specified iOS version
if #available(iOS 15.4, *) {
print("Generating image with Codabar barcode: \(contents)")
if let uiImage = gen.generateCode("A00031326002A", machineReadableCodeObjectType: AVMetadataObject.ObjectType.codabar.rawValue) {
DispatchQueue.main.async {
self.barcodeImage = Image(uiImage: uiImage)
}
} else {
DispatchQueue.main.async {
self.barcodeImage = Image(systemName: "xmark.circle")
}
}
} else {
print("iOS version does not support RSCodaBarGenerator")
}
}
}
I am trying to build a simple barcode scanning and displaying tool as part of a larger app. I'd be happy to attach the rest of the code for this file if it would be helpful at all.
I've been banging my head against this for awhile now and thought I would ask here.
Would love any help with this I haven't found any good resources online as of yet.
So far I have tried the different example statements in https://github.com/yeahdongcn/RSBarcodes_Swift but have been unable to figure it out. I just keep getting the "No code generator selected." error and I don't know how to overcome that.
This is how I was able to fix it: https://github.com/Coledunsby/CDCodabarView
Please note that as of release 1.1.0 you have to do a manual edit of the code as described here: https://github.com/Coledunsby/CDCodabarView/issues/8
After that this code worked for me:
Hope that helps someone who also runs into this issue!