Search code examples
iosswiftcifiltercode128

swift - Generate Code128 from cyrillic string


I need to create a barcode 128 from cyrillic string, so this code, which is working fine for A-Z and numbers, not helps, even if use encoding let data = string.data(using: String.Encoding.windowsCP1251)

func generateBarcode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)

    if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
        filter.setValue(data, forKey: "inputMessage")
        let transform = CGAffineTransform(scaleX: 3, y: 3)
        if let output = filter.outputImage?.transformed(by: transform) {
            return UIImage(ciImage: output)
        } else {
            print("no image!")
        }
    }
    return nil
}

Also I tried RSBarcodes_Swift, but still no result for cyrillic strings. Can please someone help me? Any suggestions are welcome.


Solution

  • Code 128 only supports ASCII (and only a subset of ASCII in any given code set).

    You could perform your own mappings to reuse the 128 values as Cyrillic rather than ASCII (i.e. write code to map "A -> A, Б -> B" or something like that), or use Code Set C to encode 2-digit numbers rather than letters, but it'd be up to you to handle encoding and decoding that on both ends. No standard Code 128 system I'm aware of is going to decode it as Cyrillic.

    By using FNC4, you can encode Latin-1. However, I do not believe this is very commonly implemented, and doesn't help for Cyrillic.