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

QR Code Colors and Transparent Background

See more QR Code Examples

Demonstrates colored QR codes. render.foreground is the color of the dark modules and render.background the color behind them, each as #RRGGBB. Either can instead be an object with a color and an opacity from 0 to 1, and a background opacity of 0 gives a PNG with a transparent background to place over a page or a design. Readers need contrast: keep the modules clearly darker than whatever ends up behind them.

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.

    let fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!

    //  Dark blue modules on a pale yellow background.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")
    json.updateInt(jsonPath: "render.moduleSize", value: 6)
    json.updateString(jsonPath: "render.foreground", value: "#1B3A6B")
    json.updateString(jsonPath: "render.background", value: "#FFF8DC")
    json.updateString(jsonPath: "output.path", value: "qa_output/qr_blue.png")
    json.updateBool(jsonPath: "output.overwrite", value: true)
    json.updateString(jsonPath: "output.return", value: "none")
    success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("colored: \(jsonOut.string(of: "outputs[0].path")!), \(jsonOut.int(of: "outputs[0].byteCount").intValue) bytes")

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

    //  image

    //  The same modules on a fully transparent background.
    let json2 = CkoJsonObject()!
    json2.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")
    json2.updateInt(jsonPath: "render.moduleSize", value: 6)
    json2.updateString(jsonPath: "render.foreground", value: "#1B3A6B")
    json2.updateString(jsonPath: "render.background.color", value: "#FFFFFF")
    json2.updateNumber(jsonPath: "render.background.opacity", numericStr: "0")
    json2.updateString(jsonPath: "output.path", value: "qa_output/qr_transparent.png")
    json2.updateBool(jsonPath: "output.overwrite", value: true)
    json2.updateString(jsonPath: "output.return", value: "none")
    success = fmt.qrCode(op: "generate", jsonIn: json2, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("transparent: \(jsonOut.string(of: "outputs[0].path")!), \(jsonOut.int(of: "outputs[0].byteCount").intValue) bytes")

    //  qa_output/qr_transparent.png, placed here over a checkerboard and over a colored page
    //  to show that whatever is behind the image shows through:

    //  image

    //  Sample output:

    //  colored: qa_output/qr_blue.png, 1345 bytes
    //  transparent: qa_output/qr_transparent.png, 1377 bytes

}