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

Create a vCard (Contact) QR Code

See more QR Code Examples

Demonstrates a QR code that adds a contact to a phone when scanned. The text is a vCard, the standard format for contact details: lines between BEGIN:VCARD and END:VCARD, each ending with CRLF. A vCard is fairly long, so the symbol is larger than one holding a web address. Leave out the fields that are not needed to keep it small.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Formats;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

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 second argument of AppendLine is cktrue to end each line with CRLF.
        StringBuilder sbCard = new StringBuilder();
        sbCard.appendLine("BEGIN:VCARD", true);
        sbCard.appendLine("VERSION:3.0", true);
        sbCard.appendLine("N:Smith;Alex;;;", true);
        sbCard.appendLine("FN:Alex Smith", true);
        sbCard.appendLine("ORG:Example Company", true);
        sbCard.appendLine("TITLE:Sales Manager", true);
        sbCard.appendLine("TEL;TYPE=CELL:+1-555-0100", true);
        sbCard.appendLine("EMAIL:alex.smith@example.com", true);
        sbCard.appendLine("URL:https://www.example.com", true);
        sbCard.appendLine("END:VCARD", true);

        JsonObject json = new JsonObject();
        json.updateSb("payload.text", sbCard);
        json.updateInt("render.moduleSize", 6);
        json.updateString("output.path", "qa_output/qr_vcard.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("vCard length: " + sbCard.getLength());
        System.out.println("QR version: " + jsonOut.intOf("symbols[0].selectedEncoding.version"));
        System.out.println("modules: " + jsonOut.intOf("symbols[0].dimensions.modulesWide") + " x " + jsonOut.intOf("symbols[0].dimensions.modulesHigh"));
        System.out.println("image: " + jsonOut.intOf("outputs[0].width") + " x " + jsonOut.intOf("outputs[0].height") + " pixels");

        // The image written to qa_output/qr_vcard.png:

        // image

        // Sample output:

        // vCard length: 197
        // QR version: 10
        // modules: 57 x 57
        // image: 390 x 390 pixels
    }
}