Java Requires Chilkat v11.7.0+
Chilkat2 Java
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 Java Downloads
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.
Formats fmt = new Formats();
JsonObject jsonOut = new JsonObject();
// Dark blue modules on a pale yellow background.
JsonObject json = new JsonObject();
json.updateString("payload.text", "https://www.chilkatsoft.com/");
json.updateInt("render.moduleSize", 6);
json.updateString("render.foreground", "#1B3A6B");
json.updateString("render.background", "#FFF8DC");
json.updateString("output.path", "qa_output/qr_blue.png");
json.updateBool("output.overwrite", true);
json.updateString("output.return", "none");
try {
fmt.qrCode("generate", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("colored: " + jsonOut.stringOf("outputs[0].path") + ", " + jsonOut.intOf("outputs[0].byteCount") + " bytes");
// The image written to qa_output/qr_blue.png:
//
// The same modules on a fully transparent background.
JsonObject json2 = new JsonObject();
json2.updateString("payload.text", "https://www.chilkatsoft.com/");
json2.updateInt("render.moduleSize", 6);
json2.updateString("render.foreground", "#1B3A6B");
json2.updateString("render.background.color", "#FFFFFF");
json2.updateNumber("render.background.opacity", "0");
json2.updateString("output.path", "qa_output/qr_transparent.png");
json2.updateBool("output.overwrite", true);
json2.updateString("output.return", "none");
try {
fmt.qrCode("generate", json2, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("transparent: " + jsonOut.stringOf("outputs[0].path") + ", " + jsonOut.intOf("outputs[0].byteCount") + " bytes");
// 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
}
}