Java Requires Chilkat v11.7.0+
Chilkat2 Java
QR Code with Binary Data
See more QR Code Examples
Demonstrates a QR code that holds arbitrary bytes rather than text. The payload type is binary and the bytes are given as base64 in payload.base64. Any byte value may be used, including zero. Keep in mind that many phone camera apps expect text, so binary payloads are for readers that you control.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Formats;
import com.chilkatsoft.chilkat2.JsonObject;
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 bytes to encode. Here they come from hex, but they could be loaded from a file with LoadFile.
BinData bd = new BinData();
try {
bd.appendEncoded("000102030405FF00FE4368696C6B6174", "hex");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject json = new JsonObject();
json.updateString("payload.type", "binary");
json.updateString("payload.base64", bd.getEncoded("base64"));
json.updateString("output.path", "qa_output/qr_binary.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;
}
System.out.println("bytes given: " + bd.getNumBytes());
System.out.println("bytes encoded: " + jsonOut.intOf("symbols[0].payloadBytes"));
System.out.println("mode: " + jsonOut.stringOf("symbols[0].selectedEncoding.mode"));
System.out.println("QR version: " + jsonOut.intOf("symbols[0].selectedEncoding.version"));
// The image written to qa_output/qr_binary.png:
//
// Sample output:
// bytes given: 16
// bytes encoded: 16
// mode: byte
// QR version: 2
}
}