Sample code for 30+ languages & platforms
Zig 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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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.
    const sb_card = try chilkat.StringBuilder.init();
    defer sb_card.deinit();
    sb_card.appendLine("BEGIN:VCARD", true) catch {};
    sb_card.appendLine("VERSION:3.0", true) catch {};
    sb_card.appendLine("N:Smith;Alex;;;", true) catch {};
    sb_card.appendLine("FN:Alex Smith", true) catch {};
    sb_card.appendLine("ORG:Example Company", true) catch {};
    sb_card.appendLine("TITLE:Sales Manager", true) catch {};
    sb_card.appendLine("TEL;TYPE=CELL:+1-555-0100", true) catch {};
    sb_card.appendLine("EMAIL:alex.smith@example.com", true) catch {};
    sb_card.appendLine("URL:https://www.example.com", true) catch {};
    sb_card.appendLine("END:VCARD", true) catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateSb("payload.text", sb_card) catch {};
    json.updateInt("render.moduleSize", 6) catch {};
    json.updateString("output.path", "qa_output/qr_vcard.png") catch {};
    json.updateBool("output.overwrite", true) catch {};
    json.updateString("output.return", "none") catch {};

    const fmt = try chilkat.Formats.init();
    defer fmt.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("vCard length: {d}\n", .{sb_card.getLength()});
    std.debug.print("QR version: {d}\n", .{json_out.intOf("symbols[0].selectedEncoding.version")});
    std.debug.print("modules: {d} x {d}\n", .{ json_out.intOf("symbols[0].dimensions.modulesWide"), json_out.intOf("symbols[0].dimensions.modulesHigh") });
    std.debug.print("image: {d} x {d} pixels\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height") });

    // 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
}