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

Split Data across Several QR Codes (Structured Append)

See more QR Code Examples

Demonstrates structured append, the QR feature for data too large for one symbol of the size wanted. The data is divided over up to 16 symbols, and each carries its position, the total, and a parity byte that ties the set together. With encoding.structuredAppend.enabled, QrCode uses as few symbols as it can. output.pathTemplate writes one file per symbol, with {index} replaced by 1, 2, 3 and so on. The reader must support structured append to put the parts back together. Many phone camera apps do not, and show each part separately.

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.

-- Some text that is too long for one symbol of the size allowed below.
local sbText = chilkat.StringBuilder.new()
for i = 1, 20 do
    sbText:append("Line ")
    sbText:append_int(i)
    sbText:append_line(" of a message that is too long for one small QR code.", true)
end

local json = chilkat.JsonObject.new()
json:update_sb("payload.text", sbText)

-- Keep each symbol small (version 10 is 57 modules wide) and allow a set of symbols.
json:update_int("encoding.qr.maxVersion", 10)
json:update_bool("encoding.structuredAppend.enabled", true)

-- One file for each symbol: qr_part_1.png, qr_part_2.png, ...
json:update_string("output.pathTemplate", "qa_output/qr_part_{index}.png")
json:update_bool("output.overwrite", true)
json:update_string("output.return", "none")

local fmt = chilkat.Formats.new()
local jsonOut = chilkat.JsonObject.new()
local success = fmt:qr_code("generate", json, jsonOut)
if not success then
    print(fmt.last_error_text)
    return
end

print("text length: " .. sbText.length)
local numSymbols = jsonOut:size_of_array("symbols")
print("symbols: " .. numSymbols)
for i = 0, numSymbols - 1 do
    jsonOut.i = i
    -- structuredAppend.index counts from 0.
    print(jsonOut:string_of("outputs[i].path") .. ": symbol " .. (jsonOut:int_of("symbols[i].structuredAppend.index") + 1) .. " of " .. jsonOut:int_of("symbols[i].structuredAppend.total") .. ", version " .. jsonOut:int_of("symbols[i].selectedEncoding.version") .. ", " .. jsonOut:int_of("symbols[i].payloadBytes") .. " bytes")
end

-- Every symbol of a set carries the same parity byte, which is how a reader knows they belong together.
print("parity: " .. jsonOut:int_of("symbols[0].structuredAppend.parity"))

-- The six symbols:

-- image

-- Sample output:

-- text length: 1231
-- symbols: 6
-- qa_output/qr_part_1.png: symbol 1 of 6, version 10, 206 bytes
-- qa_output/qr_part_2.png: symbol 2 of 6, version 10, 205 bytes
-- qa_output/qr_part_3.png: symbol 3 of 6, version 10, 205 bytes
-- qa_output/qr_part_4.png: symbol 4 of 6, version 10, 205 bytes
-- qa_output/qr_part_5.png: symbol 5 of 6, version 10, 205 bytes
-- qa_output/qr_part_6.png: symbol 6 of 6, version 10, 205 bytes
-- parity: 50