~fabrixxm/zint-vapi

zint-vapi/test/example_3.vala -rw-r--r-- 1.4 KiB
281ee64c — fabrixxm Add COPYING a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Encoding and Saving to Buffer

void main(string[] args) {
    if (args.length != 2) {
        error("Missing 'value' param.\n");
    }

    var my_symbol = new Zint.Symbol();

    int err;
    err = my_symbol.encode(args[1].to_utf8());
    err = my_symbol.buffer(0);
    if (err != 0) {
        error("Error saving to buffer");
    }

    var bitmap = my_symbol.get_bitmap();

    print("bitmap: %u bytes, %dx%d\n",
        bitmap.length,
        my_symbol.bitmap_width,
        my_symbol.bitmap_height
    );
    
    if (bitmap.length == 0) return;
    
    var pixbuf = new Gdk.Pixbuf.from_data(
        bitmap,
        Gdk.Colorspace.RGB,
        false,
        8,
        my_symbol.bitmap_width,
        my_symbol.bitmap_height,
        my_symbol.bitmap_width*3
    );
     
    if (pixbuf == null) {
        error("Error creating pixbuf\n");
    }
    
    print("pixbuf\n");
    print("\tbits_per_sample: %i\n", pixbuf.get_bits_per_sample());
    print("\tchannels: %i\n", pixbuf.get_n_channels());
    print("\tbyte_length: %u\n", (uint)pixbuf.get_byte_length());
    print("\tpix size: %ix%i\n", pixbuf.get_width(), pixbuf.get_height());
    
    File file = File.new_for_path("out.jpg");
    try {
        FileOutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
        pixbuf.save_to_stream(os, "jpeg");
    } catch(Error e) {
        error("Error: %s\n", e.message);
    }
}