C Requires Chilkat v11.7.0+
C
QR Code Colors and Transparent Background
See more QR Code Examples
Demonstrates colored QR codes. render.foreground is the color of the dark modules and render.background the color behind them, each as #RRGGBB. Either can instead be an object with a color and an opacity from 0 to 1, and a background opacity of 0 gives a PNG with a transparent background to place over a page or a design. Readers need contrast: keep the modules clearly darker than whatever ends up behind them.Chilkat C Downloads
#include <C_CkFormats.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkFormats fmt;
HCkJsonObject jsonOut;
HCkJsonObject json;
HCkJsonObject json2;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
fmt = CkFormats_Create();
jsonOut = CkJsonObject_Create();
// Dark blue modules on a pale yellow background.
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"payload.text","https://www.chilkatsoft.com/");
CkJsonObject_UpdateInt(json,"render.moduleSize",6);
CkJsonObject_UpdateString(json,"render.foreground","#1B3A6B");
CkJsonObject_UpdateString(json,"render.background","#FFF8DC");
CkJsonObject_UpdateString(json,"output.path","qa_output/qr_blue.png");
CkJsonObject_UpdateBool(json,"output.overwrite",TRUE);
CkJsonObject_UpdateString(json,"output.return","none");
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(json);
return;
}
printf("colored: %s, %d bytes\n",CkJsonObject_stringOf(jsonOut,"outputs[0].path"),CkJsonObject_IntOf(jsonOut,"outputs[0].byteCount"));
// The image written to qa_output/qr_blue.png:
//
// The same modules on a fully transparent background.
json2 = CkJsonObject_Create();
CkJsonObject_UpdateString(json2,"payload.text","https://www.chilkatsoft.com/");
CkJsonObject_UpdateInt(json2,"render.moduleSize",6);
CkJsonObject_UpdateString(json2,"render.foreground","#1B3A6B");
CkJsonObject_UpdateString(json2,"render.background.color","#FFFFFF");
CkJsonObject_UpdateNumber(json2,"render.background.opacity","0");
CkJsonObject_UpdateString(json2,"output.path","qa_output/qr_transparent.png");
CkJsonObject_UpdateBool(json2,"output.overwrite",TRUE);
CkJsonObject_UpdateString(json2,"output.return","none");
success = CkFormats_QrCode(fmt,"generate",json2,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
return;
}
printf("transparent: %s, %d bytes\n",CkJsonObject_stringOf(jsonOut,"outputs[0].path"),CkJsonObject_IntOf(jsonOut,"outputs[0].byteCount"));
// qa_output/qr_transparent.png, placed here over a checkerboard and over a colored page
// to show that whatever is behind the image shows through:
//
// Sample output:
// colored: qa_output/qr_blue.png, 1345 bytes
// transparent: qa_output/qr_transparent.png, 1377 bytes
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(json2);
}