▩ Chilkat.Formats · Output

Output: PNG, SVG and Files
output

Choose the image format, get it back as base64 or SVG text, or write files directly — including numbered file sets for structured append. Optionally include the module bit-matrix and a reusable definition.

The output object

Format, return channel, destinations

MemberValuesDefaultNotes
output.format"png" · "svg""png"
output.return"base64" · "text" · "none""base64"text is for textual formats (SVG) only; none requires a file destination.
output.pathfile path—Write the single output to this file. With a structured-append set of more than one symbol this is conflictingOptions.
output.pathTemplatefile path containing {index}—Write every output to a numbered file; {index} is replaced by 1, 2, 3, … Mutually exclusive with path.
output.overwriteboolfalseWhen false, an existing destination fails the request with outputFailed before anything is written.
output.includeModuleDataboolfalseAdds symbols[i].moduleData (the module bit-matrix).
output.includeDefinitionboolfalseAdds a top-level definition: a reusable request fragment.

Each delivered image is described by an outputs[] entry (one per symbol, in order):

MemberMeaning
index0-based position; matches symbols[index] and measurements[index].
format
mimeType
png / image/png or svg / image/svg+xml.
width
height
Canvas size in pixels (after rotation).
units
physicalWidth
physicalHeight
The physical size when render.units is mm, in or pt.
byteCountSize of the image in bytes.
base64The image bytes, base64-encoded (return: "base64").
textThe SVG document (return: "text").
pathThe file written (when path or pathTemplate was given).

PNG

The default format

PNG output is a standard 8-bit image: grayscale when both paints are neutral and opaque, RGB when the background is opaque, RGBA when the background is translucent or transparent. The render.dpi value is recorded in the file (pHYs chunk) so printers and image tools know the intended physical size.

PNG as base64 to a BinData — C#
Chilkat.JsonObject jsonIn = new Chilkat.JsonObject();
jsonIn.UpdateString("payload.text", "https://www.chilkatsoft.com");
jsonIn.UpdateString("output.format", "png");        // the default
jsonIn.UpdateString("output.return", "base64");     // the default

Chilkat.JsonObject jsonOut = new Chilkat.JsonObject();
Chilkat.Formats fmt = new Chilkat.Formats();
if (!fmt.QrCode("generate", jsonIn, jsonOut)) { Console.WriteLine(fmt.LastErrorText); return; }

Chilkat.BinData png = new Chilkat.BinData();
png.AppendEncoded(jsonOut.StringOf("outputs[0].base64"), "base64");
// png.GetBytes() for an in-memory image, or:
png.WriteFile("qr.png");
PNG as base64 to a BinData — C++
CkJsonObject jsonIn;
jsonIn.UpdateString("payload.text", "https://www.chilkatsoft.com");
jsonIn.UpdateString("output.format", "png");        // the default
jsonIn.UpdateString("output.return", "base64");     // the default

CkJsonObject jsonOut;
CkFormats fmt;
if (!fmt.QrCode("generate", jsonIn, jsonOut)) { std::cout << fmt.lastErrorText() << "\r\n"; return; }

CkBinData png;
png.AppendEncoded(jsonOut.stringOf("outputs[0].base64"), "base64");
png.WriteFile("qr.png");
PNG as base64 to a BinData — Python
jsonIn = chilkat2.JsonObject()
jsonIn.UpdateString("payload.text", "https://www.chilkatsoft.com")
jsonIn.UpdateString("output.format", "png")        # the default
jsonIn.UpdateString("output.return", "base64")     # the default

jsonOut = chilkat2.JsonObject()
fmt = chilkat2.Formats()
if not fmt.QrCode("generate", jsonIn, jsonOut):
    print(fmt.LastErrorText); exit()

png = chilkat2.BinData()
png.AppendEncoded(jsonOut.StringOf("outputs[0].base64"), "base64")
png.WriteFile("qr.png")
Not using C#, C++ or Python? The example is shown in three languages only to give a feel for the code. The Chilkat classes, including Formats, have the same methods and the same JSON requests in every supported language: C, C++, C#, VB.NET, Java, Objective-C, Swift, Python, PHP, Perl, Ruby, Node.js, Electron, React Native, Dart / Flutter, Go, Rust, Zig, Delphi, C++Builder, Free Pascal / Lazarus, Classic ASP, VB6, VBScript, FoxPro, DataFlex, PowerBuilder, Xojo, Xbase++, PureBasic, AutoIt, B4A / B4J, LabVIEW, SQL Server and Tcl. See Chilkat Downloads for Every Language.
The base64 string is ready to drop into an HTML <img src="data:image/png;base64,...">, a JSON API response, or an email.

SVG

Resolution-independent vector output

An SVG output is a small standalone document: one <rect> per run of dark modules (and per caption glyph rectangle), integer coordinates, shape-rendering="crispEdges", no text elements, transforms or CSS. It scales to any size without blurring and is trivially embedded in web pages or printed documents. With physical units the root element carries real-world width/height (e.g. 18.80mm) and a pixel viewBox.

the generated SVG (excerpt)
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="195px" height="195px" viewBox="0 0 195 195" shape-rendering="crispEdges">
<rect x="34" y="24" width="42" height="6" fill="#128A76"/>
<rect x="88" y="24" width="6" height="6" fill="#128A76"/>
<rect x="100" y="24" width="12" height="6" fill="#128A76"/>
<rect x="118" y="24" width="42" height="6" fill="#128A76"/>
  ...
</svg>

Above: an SVG with a transparent background, green modules and a caption, embedded directly in this page over a tinted box so the transparency is visible.

SVG as text — C#
jsonIn.UpdateString("output.format", "svg");
jsonIn.UpdateString("output.return", "text");
if (!fmt.QrCode("generate", jsonIn, jsonOut)) { Console.WriteLine(fmt.LastErrorText); return; }
string svg = jsonOut.StringOf("outputs[0].text");   // "<?xml version=..."
SVG as text — C++
jsonIn.UpdateString("output.format", "svg");
jsonIn.UpdateString("output.return", "text");
if (!fmt.QrCode("generate", jsonIn, jsonOut)) { std::cout << fmt.lastErrorText() << "\r\n"; return; }
const char *svg = jsonOut.stringOf("outputs[0].text");   // "<?xml version=..."
SVG as text — Python
jsonIn.UpdateString("output.format", "svg")
jsonIn.UpdateString("output.return", "text")
if not fmt.QrCode("generate", jsonIn, jsonOut):
    print(fmt.LastErrorText); exit()
svg = jsonOut.StringOf("outputs[0].text")   # "<?xml version=..."

return: "text" with format: "png" is rejected (conflictingOptions at /output/return); PNG is binary and travels as base64.


Files

Writing output directly to disk

Give output.path (one symbol) or output.pathTemplate (any number of symbols) and Chilkat writes the file(s) for you. Combine with "return": "none" when the image is not needed in the response, or keep the default to get both.

a single file
{ "payload": { "text": "https://www.chilkatsoft.com" },
  "output":  { "format": "svg", "return": "none", "path": "/var/www/images/qr.svg", "overwrite": true } }
a structured-append set of files
{ "payload":  { "type": "binary", "base64": "...up to 64 KiB..." },
  "encoding": { "structuredAppend": { "enabled": true, "maxSymbols": 8 }, "qr": { "errorCorrection": "L" } },
  "output":   { "return": "none", "pathTemplate": "/labels/part{index}.png" } }
// writes /labels/part1.png, /labels/part2.png, ... and reports each in outputs[].path

Overwrite semantics

  • With "overwrite": false (the default), every destination is checked for existence before anything is written. If any exists, the request fails with outputFailed at /output/path or /output/pathTemplate and no file is touched.
  • With "overwrite": true, existing files are replaced.
  • If a write fails part-way (disk full, missing directory, permissions), the request fails with outputFailed; the message states how many files had already been written. Files written before the failure are left in place — there is no rollback.
  • Files are written with Chilkat's ordinary file routines; paths are UTF-8 and may be relative to the process's working directory.
response after a failed write
{ "errors": [ { "code": "outputFailed", "path": "",
               "message": "failed to write the output file (output 2); 2 output file(s) were written before the failure" } ],
  "warnings": [] }

Module data

The symbol as a bit-matrix

With "includeModuleData": true, each symbol carries its raw module grid — useful for custom rendering (a different drawing library, e-ink displays, embroidery, …) while leaving the encoding to Chilkat.

symbols[0].moduleData
{ "kind": "bitMatrix", "width": 29, "height": 29, "rowBytes": 4,
  "bytesBase64": "/n6+ ... ",            // rows top to bottom, MSB-first bits, 1 = dark, zero-padded to rowBytes
  "protectedRegionsBase64": "/n6+ ... "  // same layout; 1 = function pattern (finder, timing, alignment, format/version) }

The matrix covers the symbol only (no quiet zone, no caption). protectedRegions marks the function patterns that a reader relies on, which is what a future logo/styling feature must leave untouched.

Walk the bit-matrix — C#
Chilkat.BinData bits = new Chilkat.BinData();
bits.AppendEncoded(jsonOut.StringOf("symbols[0].moduleData.bytesBase64"), "base64");
int w = jsonOut.IntOf("symbols[0].moduleData.width");
int h = jsonOut.IntOf("symbols[0].moduleData.height");
int rowBytes = jsonOut.IntOf("symbols[0].moduleData.rowBytes");
byte[] m = bits.GetBytes();
for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        bool dark = (m[y * rowBytes + x / 8] & (0x80 >> (x % 8))) != 0;
        // draw (x, y) if dark
    }
}
Walk the bit-matrix — Python
import base64
md = base64.b64decode(jsonOut.StringOf("symbols[0].moduleData.bytesBase64"))
w = jsonOut.IntOf("symbols[0].moduleData.width")
h = jsonOut.IntOf("symbols[0].moduleData.height")
rowBytes = jsonOut.IntOf("symbols[0].moduleData.rowBytes")
for y in range(h):
    row = "".join("#" if md[y * rowBytes + x // 8] & (0x80 >> (x % 8)) else " " for x in range(w))
    print(row)

Definition

A reusable request fragment

With "includeDefinition": true the response contains definition: the canonical symbology, payload and normalized encoding constraints of the request, with defaults made explicit but automatic choices kept as "auto". Copy it into a later generate request to reproduce the same symbol family (e.g. the same label with a different size), or store it alongside the image as a record of how it was made.

definition
{ "symbology": "qr",
  "payload":  { "type": "text", "text": "ORDER-12345" },
  "encoding": { "qr": { "version": "auto", "errorCorrection": "Q", "mask": "auto", "mode": "auto" },
                "structuredAppend": { "enabled": false } } }
The definition contains the payload verbatim. Do not request it for payloads that carry credentials or personal data unless you intend to store them.

verifyReadback

Read the symbol back before it is delivered

Starting in v11.8.0, "verifyReadback": true at the top level of a generate request makes Chilkat read every symbol back with its own reader before anything is delivered. If a symbol does not read back as what was encoded, the call fails, no file is written and no image is returned.

request
{ "payload": { "text": "https://www.chilkatsoft.com" }, "render": { "moduleSize": 4 }, "verifyReadback": true }
the verification member of the response
{
  "verification": {
    "state": "passed", "method": "pngDecoded",
    "symbols": [
      {
        "index": 0, "width": 148, "height": 148, "moduleSize": 4, "inverted": false,
        "correctedCodewords": 0
      }
    ]
  }
}
MemberMeaning
state"passed" when verification ran, "notRun" without verifyReadback. There is no failed state in a response: a failure fails the call.
method"pngDecoded": the PNG bytes that are delivered were decoded and read. "drawingPlanRasterized" (SVG output): the list of rectangles the SVG is written from was painted at one pixel per unit and read; the SVG text itself is not parsed.
symbols[]One entry per symbol: width and height of the raster that was read, moduleSize as the reader measured it, inverted, correctedCodewords, and alphaBackground when the image has transparency (it is then read over white and, if that fails, over black).

The symbol read must be the symbol that was encoded: the same bytes, version, error-correction level, mask and structured-append header. The output is byte for byte what the same request produces without verifyReadback, and limits.timeoutMs covers the verification. For a structured-append set every symbol is verified before the first file is written. measure and validate do not render and reject the member (unknownOption).

request: white ink on white paper
{ "payload": { "text": "https://www.chilkatsoft.com" },
  "render":  { "foreground": "#FFFFFF", "background": "#FFFFFF" }, "verifyReadback": true }
response: the method returns false
{
  "errors": [
    {
      "code": "readbackFailed", "path": "/verifyReadback",
      "message": "symbol 1 of 1 did not read back: no symbol was found in the rendered image"
    }
  ],
  "warnings": []
}
What it proves, and what it does not. A passed verification proves that the delivered image carries the encoded content for Chilkat's reader, on the exact pixels. It is not a judgement of contrast or print quality: two colors that differ at all are told apart, so a pale gray symbol on white passes. A light-on-dark symbol passes too, and is reported with "inverted": true because some scanners do not read such symbols.