Lua Requires Chilkat v11.7.0+
Lua
Generate a QR Code PNG in Memory
See more QR Code Examples
Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.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", "https://www.chilkatsoft.com/")
json:update_int("render.moduleSize", 6)
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("format: " .. jsonOut:string_of("outputs[0].format"))
print("MIME type: " .. jsonOut:string_of("outputs[0].mimeType"))
print("image: " .. jsonOut:int_of("outputs[0].width") .. " x " .. jsonOut:int_of("outputs[0].height") .. " pixels")
-- outputs[0].base64 is the PNG file, base64 encoded.
local bdPng = chilkat.BinData.new()
success = bdPng:append_encoded(jsonOut:string_of("outputs[0].base64"), "base64")
if not success then
print(bdPng.last_error_text)
return
end
print("PNG bytes: " .. bdPng.num_bytes)
-- The BinData holds the image. Here it is simply saved.
success = bdPng:write_file("qa_output/qr_in_memory.png")
if not success then
print(bdPng.last_error_text)
return
end
-- The PNG held in bdPng:
--
-- Sample output:
-- format: png
-- MIME type: image/png
-- image: 222 x 222 pixels
-- PNG bytes: 877