C Requires Chilkat v11.7.0+
C
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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>
#include <C_CkBinData.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject json;
HCkFormats fmt;
HCkJsonObject jsonOut;
HCkBinData bdPng;
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_UpdateInt(json,"render.moduleSize",6);
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;
}
printf("format: %s\n",CkJsonObject_stringOf(jsonOut,"outputs[0].format"));
printf("MIME type: %s\n",CkJsonObject_stringOf(jsonOut,"outputs[0].mimeType"));
printf("image: %d x %d pixels\n",CkJsonObject_IntOf(jsonOut,"outputs[0].width"),CkJsonObject_IntOf(jsonOut,"outputs[0].height"));
// outputs[0].base64 is the PNG file, base64 encoded.
bdPng = CkBinData_Create();
success = CkBinData_AppendEncoded(bdPng,CkJsonObject_stringOf(jsonOut,"outputs[0].base64"),"base64");
if (success == FALSE) {
printf("%s\n",CkBinData_lastErrorText(bdPng));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkBinData_Dispose(bdPng);
return;
}
printf("PNG bytes: %d\n",CkBinData_getNumBytes(bdPng));
// The BinData holds the image. Here it is simply saved.
success = CkBinData_WriteFile(bdPng,"qa_output/qr_in_memory.png");
if (success == FALSE) {
printf("%s\n",CkBinData_lastErrorText(bdPng));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkBinData_Dispose(bdPng);
return;
}
// The PNG held in bdPng:
//
// Sample output:
// format: png
// MIME type: image/png
// image: 222 x 222 pixels
// PNG bytes: 877
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkBinData_Dispose(bdPng);
}