Java Requires Chilkat v11.7.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Formats;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// 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.
StringBuilder sbText = new StringBuilder();
sbText.append("Gr\u00fc\u00dfe aus M\u00fcnchen");
sbText.decode("json", "utf-8");
JsonObject json = new JsonObject();
json.updateSb("payload.text", sbText);
json.updateString("output.path", "qa_output/qr_utf8.png");
json.updateBool("output.overwrite", true);
json.updateString("output.return", "none");
Formats fmt = new Formats();
JsonObject jsonOut = new JsonObject();
try {
fmt.qrCode("generate", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// 17 characters, three of which take two bytes in UTF-8.
System.out.println("characters: " + sbText.getLength());
System.out.println("bytes encoded: " + jsonOut.intOf("symbols[0].payloadBytes"));
System.out.println("ECI header: " + jsonOut.hasMember("symbols[0].selectedEncoding.eci"));
// Without an ECI header (qa_output/qr_utf8.png):
//
// The same text with an ECI header declaring UTF-8.
json.updateInt("encoding.eci", 26);
json.updateString("output.path", "qa_output/qr_utf8_eci.png");
try {
fmt.qrCode("generate", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("with encoding.eci = 26, ECI header: " + jsonOut.hasMember("symbols[0].selectedEncoding.eci") + ", value " + jsonOut.intOf("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
}
}