Java Requires Chilkat v11.7.0+
Chilkat2 Java
QR Code with a Physical Size in Millimeters at a Printer DPI
See more QR Code Examples
Demonstrates sizing a QR code for print. With render.units set to mm (or in, or pt) the module size is a physical length, and render.dpi says how many pixels the printer or the image has per inch. A module is always a whole number of pixels, because modules of uneven width scan badly, so the size asked for is rounded to the nearest pixel and the response reports the size actually used. The PNG records the dpi, so software that honors it prints the image at that physical size.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.
JsonObject json = new JsonObject();
json.updateString("payload.text", "https://www.chilkatsoft.com/");
// Modules of 0.5 mm, for a 300 dpi printer.
json.updateString("render.units", "mm");
json.updateNumber("render.moduleSize", "0.5");
json.updateInt("render.dpi", 300);
json.updateString("output.path", "qa_output/qr_300dpi.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;
}
// 0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.
System.out.println("requested module: " + jsonOut.stringOf("measurements[0].requestedPitch") + " pixels");
System.out.println("module drawn: " + jsonOut.intOf("measurements[0].pitch") + " pixels = " + jsonOut.stringOf("measurements[0].moduleSize") + " mm");
System.out.println("size was rounded: " + jsonOut.boolOf("measurements[0].pitchAdjusted"));
System.out.println("image: " + jsonOut.intOf("measurements[0].canvasWidth") + " x " + jsonOut.intOf("measurements[0].canvasHeight") + " pixels");
System.out.println("printed size: " + jsonOut.stringOf("measurements[0].physicalWidth") + " x " + jsonOut.stringOf("measurements[0].physicalHeight") + " mm");
// The image. A web page shows it pixel for pixel. Printed at 300 dpi it is 18.8 mm wide:
//
// When the exact size matters more than getting an image, requireExactPitch makes a size
// that is not a whole number of pixels a failure (noFit) instead of rounding it.
json.updateBool("render.requireExactPitch", true);
try {
fmt.qrCode("generate", json, jsonOut);
} catch (ChilkatException e) {
System.out.println("requireExactPitch: " + jsonOut.stringOf("errors[0].code") + " (" + jsonOut.stringOf("errors[0].message") + ")");
}
// Sample output:
// requested module: 5.905512 pixels
// module drawn: 6 pixels = 0.508 mm
// size was rounded: True
// image: 222 x 222 pixels
// printed size: 18.796 x 18.796 mm
// requireExactPitch: noFit (the symbol does not fit the render constraints: pitchAdjusted)
}
}