How do I generate a Codabar barcode in Swift?

51 views Asked by At

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.

1

There are 1 answers

0
Boaztheostrich On

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:

import CDCodabarView

// Define your SwiftUI wrapper for the CDCodabarView
struct SwiftUIBarcodeView: UIViewRepresentable {
    var code: String
    
    func makeUIView(context: Context) -> CDCodabarView {
        let codabarView = CDCodabarView()
        codabarView.backgroundColor = .white // Set background color to white
        return codabarView
    }

    func updateUIView(_ uiView: CDCodabarView, context: Context) {
        uiView.code = code
        uiView.barColor = .black
        uiView.textColor = .red
        uiView.padding = 50
        uiView.hideCode = false
        uiView.font = UIFont(name: "AvenirNext-Regular", size: 15.0)!
        uiView.backgroundColor = .white // Ensure background color is set to white on update as well
    }
}

Hope that helps someone who also runs into this issue!