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

Generate a QR Code as SVG

See more QR Code Examples

Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oFmt
LOCAL oJsonOut
LOCAL oSbSvg

nSuccess := 0

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("payload.text", "https://www.chilkatsoft.com/")
oJson:UpdateString("output.format", "svg")

//  An SVG is text, so it can be returned as text rather than as base64.
oJson:UpdateString("output.return", "text")

oFmt := CreateObject("Chilkat.Formats")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oFmt:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

oSbSvg := CreateObject("Chilkat.StringBuilder")
oJsonOut:StringOfSb("outputs[0].text", oSbSvg)

? "MIME type: " + oJsonOut:StringOf("outputs[0].mimeType")
? "size: " + Str(oJsonOut:IntOf("outputs[0].width")) + " x " + Str(oJsonOut:IntOf("outputs[0].height")) + " " + oJsonOut:StringOf("outputs[0].units")
? "SVG length: " + Str(oSbSvg:Length) + " characters"

//  The SVG is a complete document that can be saved, or placed inline in an HTML page.
nSuccess := oSbSvg:WriteFile("qa_output/qr_chilkat.svg", "utf-8", 0)
IF (nSuccess == 0)
    ? oSbSvg:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    oSbSvg:destroy()
    RETURN
ENDIF

//  The SVG:

//  image

//  Sample output:

//  MIME type: image/svg+xml
//  size: 148 x 148 px
//  SVG length: 12500 characters

oJson:destroy()
oFmt:destroy()
oJsonOut:destroy()
oSbSvg:destroy()