Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Choose the QR Code Version and Mask Pattern

See more QR Code Examples

Demonstrates fixing the QR version and the mask pattern. The version (1 to 40) is the size of the symbol: version 1 is 21 modules wide and each version adds 4. By default QrCode picks the smallest version that holds the data and the mask pattern that scores best under the QR standard. Fixing the version is useful when a series of codes must all be the same size, and minVersion and maxVersion bound the automatic choice instead.

Chilkat Java Downloads

Chilkat2 Java
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 json = new JsonObject();
        JsonObject jsonOut = new JsonObject();
        json.updateString("payload.text", "https://www.chilkatsoft.com/");

        // Automatic: the smallest version that holds the text, and the best-scoring mask.
        try {
            fmt.qrCode("generate", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("automatic: version " + jsonOut.intOf("symbols[0].selectedEncoding.version") + ", " + jsonOut.intOf("symbols[0].dimensions.modulesWide") + " modules wide, mask " + jsonOut.intOf("symbols[0].selectedEncoding.mask"));

        // A fixed version.  The call fails with capacityExceeded if the text does not fit it.
        json.updateInt("encoding.qr.version", 8);
        try {
            fmt.qrCode("generate", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("version 8: version " + jsonOut.intOf("symbols[0].selectedEncoding.version") + ", " + jsonOut.intOf("symbols[0].dimensions.modulesWide") + " modules wide, mask " + jsonOut.intOf("symbols[0].selectedEncoding.mask"));

        // A fixed mask pattern, 0 to 7.  Every mask gives a valid symbol holding the same data.
        json.updateInt("encoding.qr.mask", 5);
        try {
            fmt.qrCode("generate", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("version 8, mask 5: version " + jsonOut.intOf("symbols[0].selectedEncoding.version") + ", mask " + jsonOut.intOf("symbols[0].selectedEncoding.mask"));

        // Bounds for the automatic choice: no smaller than version 4, no larger than version 10.
        JsonObject json2 = new JsonObject();
        json2.updateString("payload.text", "https://www.chilkatsoft.com/");
        json2.updateInt("encoding.qr.minVersion", 4);
        json2.updateInt("encoding.qr.maxVersion", 10);
        try {
            fmt.qrCode("generate", json2, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("minVersion 4: version " + jsonOut.intOf("symbols[0].selectedEncoding.version"));

        // The four symbols, in the order they were made.  The second and the third hold the same data
        // in the same version and differ only in the mask:

        // image

        // Sample output:

        // automatic: version 3, 29 modules wide, mask 2
        // version 8: version 8, 49 modules wide, mask 2
        // version 8, mask 5: version 8, mask 5
        // minVersion 4: version 4
    }
}