Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local fmt = chilkat.Formats.new()
local jsonOut = chilkat.JsonObject.new()

-- Dark blue modules on a pale yellow background.
local json = chilkat.JsonObject.new()
json:update_string("payload.text", "https://www.chilkatsoft.com/")
json:update_int("render.moduleSize", 6)
json:update_string("render.foreground", "#1B3A6B")
json:update_string("render.background", "#FFF8DC")
json:update_string("output.path", "qa_output/qr_blue.png")
json:update_bool("output.overwrite", true)
json:update_string("output.return", "none")
local success = fmt:qr_code("generate", json, jsonOut)
if not success then
    print(fmt.last_error_text)
    return
end

print("colored: " .. jsonOut:string_of("outputs[0].path") .. ", " .. jsonOut:int_of("outputs[0].byteCount") .. " bytes")

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

-- image

-- The same modules on a fully transparent background.
local json2 = chilkat.JsonObject.new()
json2:update_string("payload.text", "https://www.chilkatsoft.com/")
json2:update_int("render.moduleSize", 6)
json2:update_string("render.foreground", "#1B3A6B")
json2:update_string("render.background.color", "#FFFFFF")
json2:update_number("render.background.opacity", "0")
json2:update_string("output.path", "qa_output/qr_transparent.png")
json2:update_bool("output.overwrite", true)
json2:update_string("output.return", "none")
success = fmt:qr_code("generate", json2, jsonOut)
if not success then
    print(fmt.last_error_text)
    return
end

print("transparent: " .. jsonOut:string_of("outputs[0].path") .. ", " .. jsonOut:int_of("outputs[0].byteCount") .. " 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