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

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

Chilkat Java Downloads

Chilkat2 Java
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.

        JsonObject json = new JsonObject();
        json.updateString("payload.text", "https://www.chilkatsoft.com/");
        json.updateInt("render.moduleSize", 6);

        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("format: " + jsonOut.stringOf("outputs[0].format"));
        System.out.println("MIME type: " + jsonOut.stringOf("outputs[0].mimeType"));
        System.out.println("image: " + jsonOut.intOf("outputs[0].width") + " x " + jsonOut.intOf("outputs[0].height") + " pixels");

        // outputs[0].base64 is the PNG file, base64 encoded.
        BinData bdPng = new BinData();
        try {
            bdPng.appendEncoded(jsonOut.stringOf("outputs[0].base64"), "base64");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("PNG bytes: " + bdPng.getNumBytes());

        // The BinData holds the image.  Here it is simply saved.
        try {
            bdPng.writeFile("qa_output/qr_in_memory.png");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The PNG held in bdPng:

        // image

        // Sample output:

        // format: png
        // MIME type: image/png
        // image: 222 x 222 pixels
        // PNG bytes: 877
    }
}