~stefan-schwarzburg/qreator/touch-scanner-automaticscan

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import QtQuick 2.0
import QtGraphicalEffects 1.0
import Ubuntu.Components 0.1
import "js/qrcode.js" as QrCode

Item {
    id: container
    height: parent.height
    width: parent.width
    property string text

    RectangularGlow {
        id: effect
        anchors.fill: qrcanvas
        glowRadius: 10
        spread: 0.2
        color: "#DD4814"
        cornerRadius: glowRadius
    }

    Canvas {
        id: qrcanvas
        width: container.width
        height: container.height
        antialiasing: true
            
        onPaint: drawQrCode(container.text)
    }
    
    function requestQrCodePaint(textme) {
        container.text = textme;
        qrcanvas.requestPaint();
    }

    function drawQrCode (textdata) {
        var ctx = qrcanvas.getContext('2d');
        
        var pixel_size = units.gu(1);
        // Determines the overall dimensions of the symbol (1..10)
        // We choose Version 4 (33×33).
        var QRCodeVersion = 4;
        var QRErrorCorrectLevel = 'M'
        var qr = QrCode.qrcode(QRCodeVersion, QRErrorCorrectLevel);
        
        qr.addData(textdata);
        qr.make();

        for (var row = 0; row < qr.getModuleCount(); row++) {
            for (var col = 0; col < qr.getModuleCount(); col++) {
                if (qr.isDark(row, col) ) {
                        ctx.fillStyle = "rgb(51,51,51)";
                } else {
                        ctx.fillStyle = "rgb(255,255,255)";  
                }
                drawPixelShape(ctx, 1, col*pixel_size, row*pixel_size, pixel_size, pixel_size);
            }
        }
    }

    function drawPixelShape(context, shape, x, y, width, height){
        // FIXME: need to add constants (enum) for shape. See
        // http://www.misfitgeek.com/2011/10/using-enum-in-javascript/
        if (shape == 1) {
            // Square-shaped pixels
            context.fillRect (x, y, width, height);
        }

        if (shape == 2) {
            // Round-shaped pixels
            context.beginPath();
            context.arc(x + width/2, y + height/2, width/2, 0, 2 * Math.PI, false);
            context.fill();
        }

    }
}