Lua Requires Chilkat v11.7.0+
Lua
Get the Modules of a QR Code (Bit Matrix)
See more QR Code Examples
Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local json = chilkat.JsonObject.new()
json:update_string("payload.text", "CHILKAT")
json:update_bool("output.includeModuleData", true)
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
local width = jsonOut:int_of("symbols[0].moduleData.width")
local height = jsonOut:int_of("symbols[0].moduleData.height")
local rowBytes = jsonOut:int_of("symbols[0].moduleData.rowBytes")
print("kind: " .. jsonOut:string_of("symbols[0].moduleData.kind"))
print("modules: " .. width .. " x " .. height .. ", " .. rowBytes .. " bytes per row")
-- The matrix is height rows of rowBytes bytes.
local bd = chilkat.BinData.new()
success = bd:append_encoded(jsonOut:string_of("symbols[0].moduleData.bytesBase64"), "base64")
if not success then
print(bd.last_error_text)
return
end
local sbRow = chilkat.StringBuilder.new()
local b = 0
local mask = 128
for row = 0, height - 1 do
sbRow:clear()
for col = 0, width - 1 do
-- The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
-- The leftmost module of each byte is its most significant bit.
if (col % 8) == 0 then
mask = 128
end
b = bd:get_byte((row * rowBytes) + (math.floor(col / 8)))
if ((math.floor(b / mask)) % 2) == 1 then
sbRow:append("##")
else
sbRow:append(" ")
end
mask = math.floor(mask / 2)
end
print(sbRow:get_as_string())
end
-- The same symbol as the PNG that the call also returned:
--
-- Sample output:
-- kind: bitMatrix
-- modules: 21 x 21, 3 bytes per row
-- ############## ##############
-- ## ## #### ## ##
-- ## ###### ## ## #### ## ###### ##
-- ## ###### ## ## ## ###### ##
-- ## ###### ## ## ## ## ## ###### ##
-- ## ## #### ## ## ##
-- ############## ## ## ## ##############
-- ####
-- ## ########## #### ##########
-- #### ## ######## ######
-- #### ######## #### ## #### ####
-- ## ## ############ #### ##
-- ###### #### ###### ## ## ##
-- ## ## ## ## ## ##
-- ############## ## ## ## ##
-- ## ## ## ## ###### ####
-- ## ###### ## #### ## ## ##
-- ## ###### ## ## ############ ##
-- ## ###### ## ## ## #### ##
-- ## ## ## ######## ##
-- ############## ## ## ######