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

QR Codes for Email, Phone, SMS and Location

See more QR Code Examples

Demonstrates QR codes that start an action on a phone. A QR code only holds text, and what happens on scanning depends on the form of that text: mailto: starts an email, tel: dials a number, SMSTO: starts a text message, and geo: opens a map at a latitude and longitude. A web address opens in the browser.

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.

    const fmt = try chilkat.Formats.init();
    defer fmt.deinit();
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    json.updateInt("render.moduleSize", 6) catch {};
    json.updateBool("output.overwrite", true) catch {};
    json.updateString("output.return", "none") catch {};

    // An email, with the address and optionally a subject.  Spaces in the subject are written %20.
    json.updateString("payload.text", "mailto:support@example.com?subject=Order%2012345") catch {};
    json.updateString("output.path", "qa_output/qr_email.png") catch {};
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("email: version {d}, {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "outputs[0].path") });

    // image

    // A phone number to dial, in international form.
    json.updateString("payload.text", "tel:+15555550100") catch {};
    json.updateString("output.path", "qa_output/qr_phone.png") catch {};
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("phone: version {d}, {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "outputs[0].path") });

    // image

    // A text message: the number, then the message.
    json.updateString("payload.text", "SMSTO:+15555550100:Please call me back") catch {};
    json.updateString("output.path", "qa_output/qr_sms.png") catch {};
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("sms: version {d}, {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "outputs[0].path") });

    // image

    // A location: latitude, then longitude, in degrees.
    json.updateString("payload.text", "geo:41.8781,-87.6298") catch {};
    json.updateString("output.path", "qa_output/qr_location.png") catch {};
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("location: version {d}, {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "outputs[0].path") });

    // image

    // Sample output:

    // email: version 4, qa_output/qr_email.png
    // phone: version 2, qa_output/qr_phone.png
    // sms: version 3, qa_output/qr_sms.png
    // location: version 2, qa_output/qr_location.png
}