Bonjour j'ai effectué un lecteur de code barre classique qui peut lire les ean13 par exemple mais j'aurais aussi besoin qu'il puisse lire et décoder les data matrix (2d) mais le problème c'est que je n'arrive pas a modifier ceci .
voici mon js :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// déclaration d'un objet barcode on lui passe 3 paramètre
var Barcode = function (context, width, height) {
    this.context = context;
    this.width = width;
    this.height = height;
    this.debug = false;
};
 
Barcode.MAX_VARIANCE_SEPARATOR = 0.9;
Barcode.MAX_VARIANCE = 0.27;
 
Barcode.prototype.scan = function () {
 
    var horizontal = 2;
 
    while (horizontal--) {
 
        var length = horizontal ? this.width : this.height;
        var scan = 10;
        var step = length / scan;
 
        while (scan--) {
 
            var xy = (step * scan * 1.5) % length;
 
            if (horizontal) {
                var x = 0;
                var y = xy;
                var width = length;
                var height = 1;
            } else {
                var x = xy;
                var y = 0;
                var width = 1;
                var height = length;
            }
 
            var data = this.context.getImageData(x, y, width, height).data;
            var grey = Barcode.grey(data);
 
            for (var contrast = 10; contrast <= 750; contrast += 10) {
 
                var bits = Barcode.convert(grey, contrast);
 
                var reverse = 2;
                while (reverse--) {
 
                    var line = new Barcode.Line(bits, x, y, width, height, horizontal);
 
                    if (line.parse()) {
                        return line;
                    }
 
                    bits.reverse();
                }
            }
        }
    }
 
    return false;
};
 
Barcode.prototype.print = function (line) {
 
    for (var i = 0; i < line.bits.length; i++) {
 
        this.context.fillStyle = 'rgb(' + line.bits[i] + ', ' + line.bits[i] + ', ' + line.bits[i] + ')';
 
        if (line.horizontal) {
            this.context.fillRect(i, line.y, 1, 100);
        } else {
            this.context.fillRect(line.x, i, 100, 1);
        }
    }
 
    this.context.fillStyle = 'rgba(255, 0, 0, 0.5)';
    if (line.horizontal) {
        this.context.fillRect(line.x, line.y, line.width, 5);
    } else {
        this.context.fillRect(line.x, line.y, 5, line.height);
    }
};
 
Barcode.grey = function (data) {
 
    var grey = [];
 
    for (var i = 0, n = data.length; i < n; i += 4) {
        grey[grey.length] = data[i] + data[i + 1] + data[i + 2];
    }
 
    return grey;
};
 
Barcode.convert = function (grey, contrast) {
 
    var bits = [];
 
    for (var i = 0, n = grey.length; i < n; i++) {
        bits[i] = grey[i] < contrast ? 0 : 255;
    }
 
    return bits;
};
 
Barcode.runlength = function (bits) {
 
    var lines = [];
    var current = bits[0];
    var count = 0;
    for (var col = 0; col < bits.length; col++) {
        if (bits[col] == current) {
            count++;
        } else {
            lines.push(count);
            count = 1;
            current = bits[col];
        }
    }
    lines.push(count);
 
    return lines;
};
 
Barcode.Line = function (bits, x, y, width, height, horizontal) {
    this.bits = bits;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.horizontal = horizontal;
 
    this.start = 0;
    this.bar = 0;
    this.digits = [];
 
    this.isbn = '';
};
 
Barcode.Line.prototype.parse = function () {
 
    var lines = Barcode.runlength(this.bits);
 
    // find start
    var bar = 0, start = 0, end = 0;
    for (var i = 0; i < (lines.length - 3); i++) {
 
        var all = lines.slice(i, i + 59);
 
        var total = 0;
        for (var j = 0; j < all.length; j++) {
            total += all[j];
        }
        var bar = total / 95;
 
        var variance = (lines[i] / bar) * (lines[i + 1] / bar) * (lines[i + 2] / bar);
 
        if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {
 
            // check middle
            var variance = (lines[i + 27] / bar) * (lines[i + 28] / bar) * (lines[i + 29] / bar) * (lines[i + 30] / bar) * (lines[i + 31] / bar);
 
            if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {
 
                // check end
                var variance = (lines[i + 56] / bar) * (lines[i + 57] / bar) * (lines[i + 58] / bar);
 
                if (Math.abs(1 - variance) < Barcode.MAX_VARIANCE_SEPARATOR) {
 
                    end = i + 59;
                    start = i + 3;
 
                    // start, middle and end found
                    break;
                }
            }
        }
    }
 
    if (end == 0) {
 
        // no end found
        return false;
    }
 
    // decode barcode
    var GROUP = 6;
 
    var isbn = '';
    var sum = '';
 
    var bars = lines.slice(start, start + 4 * GROUP);
    bars = bars.concat(lines.slice(start + 4 * GROUP + 5, start + 4 * 2 * GROUP + 5));
    for (var i = 0; i < 2 * GROUP; i++) {
 
        var digits = [
            bars[i * 4],
            bars[i * 4 + 1],
            bars[i * 4 + 2],
            bars[i * 4 + 3]
        ];
        this.digits.push(digits);
 
        var pattern = Barcode.EAN13.match(digits, bar);
        if (pattern) {
            sum += 'L';
        } else {
            sum += 'G';
            pattern = Barcode.EAN13.match(digits.reverse(), bar);
        }
 
        if (!pattern) {
            // no pattern match found
            return false;
        }
 
        isbn += pattern;
    }
 
    var first = Barcode.EAN13.FIRST_DIGITS[sum.substr(0, 6)] || false;
 
    if (!first) {
        // no first pattern found
        return false;
    }
 
    this.isbn = first + isbn;
 
    return Barcode.EAN13.checksum(this.isbn);
};
 
Barcode.EAN13 = {
    PATTERNS: [
        [3, 2, 1, 1], // 0
        [2, 2, 2, 1], // 1
        [2, 1, 2, 2], // 2
        [1, 4, 1, 1], // 3
        [1, 1, 3, 2], // 4
        [1, 2, 3, 1], // 5
        [1, 1, 1, 4], // 6
        [1, 3, 1, 2], // 7
        [1, 2, 1, 3], // 8
        [3, 1, 1, 2]  // 9
    ],
    FIRST_DIGITS: {
        'LLLLLL': '0',
        'LLGLGG': '1',
        'LLGGLG': '2',
        'LLGGGL': '3',
        'LGLLGG': '4',
        'LGGLLG': '5',
        'LGGGLL': '6',
        'LGLGLG': '7',
        'LGLGGL': '8',
        'LGGLGL': '9'
    },
    checksum: function (isbn) {
 
        var length = isbn.length;
        var sum = 0;
 
        for (var i = 0; i < length; i++) {
 
            if (i % 2 == 0) {
                sum += parseInt(isbn[i]);
            } else {
                sum += parseInt(isbn[i]) * 3;
            }
        }
 
        return sum % 10 == 0;
    },
    match: function (digits, bar) {
 
        var best = Barcode.MAX_VARIANCE;
        var variance = 0;
        var match = false;
 
        for (var j = 0; j < this.PATTERNS.length; j++) {
 
            variance = this.variance(digits, this.PATTERNS[j], bar);
 
            if (variance < best) {
                best = variance;
                match = "" + j;
            }
        }
 
        return match;
    },
    variance: function (digits, pattern, bar) {
 
        var sum = digits[0] + digits[1] + digits[2] + digits[3];
        if (isNaN(sum)) {
            return 9999;
        }
        var total = 0;
 
        total += Math.abs(digits[0] - pattern[0] * bar);
        total += Math.abs(digits[1] - pattern[1] * bar);
        total += Math.abs(digits[2] - pattern[2] * bar);
        total += Math.abs(digits[3] - pattern[3] * bar);
 
        return total / sum;
    }
};
et maintenant mon html :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Giphar</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="../Public/bootstrap.min.css">
        <link rel="stylesheet" href="../Public/style.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="../Public/jquery.min.js" charset="utf-8"></script>
    </head>
    <body>
 
        <!-- appel le menu -->
        <?php require('../Modele/m_menu.php'); ?><br>
 
        <form style="text-align:center" action="v_DataMatrix.php" method="post">
            <input type="submit" name="" value="DataMatrix" class="btn btn-info">
        </form>
        <form id="form"style="">
            <div style="border-bottom: 1px solid #ddd;">
                <input type="file" id="file" style="width: 280px; line-height: 18px; padding: 20px;" />
            </div><br>
            <p id="isbn" style="">
                s'il vous plait selectionner une photo d'un code barre (Ean13 ...)
            </p>
 
            <canvas id="canvas"></canvas>
        </form>
        <script src="../Controleur/c_scan.js"></script>
        <script type="text/javascript" >
            try {
                document.getElementById('file').onchange = function() {
                    document.getElementById('isbn').innerHTML = 'Chargement…';
                    var image = new Image();
                    image.onload = function () {
                        var canvas = document.getElementById('canvas');
                        var width = canvas.width;
                        var height = canvas.height;
                        var context = canvas.getContext('2d');
                        context.drawImage(image, 0, 0, width, height);
                        var barcode = new Barcode(context, width, height);
                        var line = barcode.scan();
 
                        if (line) {
                            document.getElementById('isbn').innerHTML = "produit trouvé...";
                             barcode.print(line);
                             document.getElementById('codeB').value = line.isbn;
 
                        } else {
                            document.getElementById('isbn').innerHTML = 'Désolé, ce code barre est introuvable ...';
                        }
                    };
                    image.src = window.webkitURL.createObjectURL(this.files[0]);
                };
            } catch (e) {
                alert(e);
            }
      </script>
      <form action="../Controleur/c_fichePcodeB.php" method="post">
         <input type="text" name="codeB" id="codeB" hidden="">
         <input style='margin-top:30%;margin-left:35%' type="submit" name="btnCodeB" value="fiche produit" class="btn btn-success">
       </form>
    </body>
</html>
merci d'avance de votre aide !