C Requires Chilkat v11.7.0+
C
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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject json;
HCkFormats fmt;
HCkJsonObject jsonOut;
HCkStringBuilder sbSvg;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"payload.text","https://www.chilkatsoft.com/");
CkJsonObject_UpdateString(json,"output.format","svg");
// An SVG is text, so it can be returned as text rather than as base64.
CkJsonObject_UpdateString(json,"output.return","text");
fmt = CkFormats_Create();
jsonOut = CkJsonObject_Create();
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
return;
}
sbSvg = CkStringBuilder_Create();
CkJsonObject_StringOfSb(jsonOut,"outputs[0].text",sbSvg);
printf("MIME type: %s\n",CkJsonObject_stringOf(jsonOut,"outputs[0].mimeType"));
printf("size: %d x %d %s\n",CkJsonObject_IntOf(jsonOut,"outputs[0].width"),CkJsonObject_IntOf(jsonOut,"outputs[0].height"),CkJsonObject_stringOf(jsonOut,"outputs[0].units"));
printf("SVG length: %d characters\n",CkStringBuilder_getLength(sbSvg));
// The SVG is a complete document that can be saved, or placed inline in an HTML page.
success = CkStringBuilder_WriteFile(sbSvg,"qa_output/qr_chilkat.svg","utf-8",FALSE);
if (success == FALSE) {
printf("%s\n",CkStringBuilder_lastErrorText(sbSvg));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkStringBuilder_Dispose(sbSvg);
return;
}
// The SVG:
//
// Sample output:
// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkStringBuilder_Dispose(sbSvg);
}