Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.7.0+

QR Code with Binary Data

See more QR Code Examples

Demonstrates a QR code that holds arbitrary bytes rather than text. The payload type is binary and the bytes are given as base64 in payload.base64. Any byte value may be used, including zero. Keep in mind that many phone camera apps expect text, so binary payloads are for readers that you control.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The bytes to encode.  Here they come from hex, but they could be loaded from a file with LoadFile.
    let bd = CkoBinData()!
    success = bd.appendEncoded(encData: "000102030405FF00FE4368696C6B6174", encoding: "hex")
    if success == false {
        print("\(bd.lastErrorText!)")
        return
    }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "payload.type", value: "binary")
    json.updateString(jsonPath: "payload.base64", value: bd.getEncoded(encoding: "base64"))
    json.updateString(jsonPath: "output.path", value: "qa_output/qr_binary.png")
    json.updateBool(jsonPath: "output.overwrite", value: true)
    json.updateString(jsonPath: "output.return", value: "none")

    let fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!
    success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("bytes given: \(bd.numBytes.intValue)")
    print("bytes encoded: \(jsonOut.int(of: "symbols[0].payloadBytes").intValue)")
    print("mode: \(jsonOut.string(of: "symbols[0].selectedEncoding.mode")!)")
    print("QR version: \(jsonOut.int(of: "symbols[0].selectedEncoding.version").intValue)")

    //  The image written to qa_output/qr_binary.png:

    //  image

    //  Sample output:

    //  bytes given: 16
    //  bytes encoded: 16
    //  mode: byte
    //  QR version: 2

}