C Requires Chilkat v11.7.0+
C
QR Code with Non-English Text (UTF-8 and ECI)
See more QR Code Examples
Demonstrates text outside ASCII. QrCode always stores text as UTF-8 bytes, which is what phones expect. A QR code can also say so explicitly with an ECI header: setting encoding.eci to 26 declares the bytes to be UTF-8, for readers that would otherwise assume another character set. The header costs a little space, and a few older readers do not understand it, so it is off by default.Chilkat C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder sbText;
HCkJsonObject json;
HCkFormats fmt;
HCkJsonObject jsonOut;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The text is "Gruesse aus Muenchen" written with u-umlaut and sharp s. It is given here with
// JSON escapes only so that this source is plain ASCII. Decode turns the escapes into characters.
sbText = CkStringBuilder_Create();
CkStringBuilder_Append(sbText,"Gr\u00fc\u00dfe aus M\u00fcnchen");
CkStringBuilder_Decode(sbText,"json","utf-8");
json = CkJsonObject_Create();
CkJsonObject_UpdateSb(json,"payload.text",sbText);
CkJsonObject_UpdateString(json,"output.path","qa_output/qr_utf8.png");
CkJsonObject_UpdateBool(json,"output.overwrite",TRUE);
CkJsonObject_UpdateString(json,"output.return","none");
fmt = CkFormats_Create();
jsonOut = CkJsonObject_Create();
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkStringBuilder_Dispose(sbText);
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
return;
}
// 17 characters, three of which take two bytes in UTF-8.
printf("characters: %d\n",CkStringBuilder_getLength(sbText));
printf("bytes encoded: %d\n",CkJsonObject_IntOf(jsonOut,"symbols[0].payloadBytes"));
printf("ECI header: %d\n",CkJsonObject_HasMember(jsonOut,"symbols[0].selectedEncoding.eci"));
// Without an ECI header (qa_output/qr_utf8.png):
//
// The same text with an ECI header declaring UTF-8.
CkJsonObject_UpdateInt(json,"encoding.eci",26);
CkJsonObject_UpdateString(json,"output.path","qa_output/qr_utf8_eci.png");
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkStringBuilder_Dispose(sbText);
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("with encoding.eci = 26, ECI header: %d, value %d\n",CkJsonObject_HasMember(jsonOut,"symbols[0].selectedEncoding.eci"),CkJsonObject_IntOf(jsonOut,"symbols[0].selectedEncoding.eci"));
// With the ECI header (qa_output/qr_utf8_eci.png). Both scan as the same text:
//
// Sample output:
// characters: 17
// bytes encoded: 20
// ECI header: False
// with encoding.eci = 26, ECI header: True, value 26
CkStringBuilder_Dispose(sbText);
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
}