-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarcoder-element.html
More file actions
350 lines (318 loc) · 9.95 KB
/
barcoder-element.html
File metadata and controls
350 lines (318 loc) · 9.95 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<link rel="import" href="../polymer/polymer.html">
<dom-module id="barcoder-element">
<template>
<style>
:host {
display: block;
}
</style>
<video hidden=true id="video" width="{{width}}" height="{{height}}" autoplay></video>
<canvas id="canvas" width="{{width}}" height="{{height}}"></canvas>
<input value="{{digits}}"></input>
<button on-tap=_onRestart>ReScan</button>
</template>
<script>
Polymer({
is: 'barcoder-element',
properties: {
/**
* Set width for video capture width
*
* @attribute width
*/
width: {
type: Number,
value: 320,
},
/**
* Set height for video capture height
*
* @attribute height
*/
height: {
type: Number,
value: 240,
},
/**
* Quality reflects the reliability of the detected barcode
*
* @attribute quality
*/
quality: {
type: Number,
value: 0.60,
},
/**
* B/W threshold for bar detection
*
* @attribute threshold
*/
threshold: {
type: Number,
value: 150,
},
/**
* recognized barcode
*
* @attribute digits
*/
digits: {
type: Number,
notify: true,
},
timer: {
type: Object,
},
},
_onRestart: function() {
this.timer = setInterval(this.processImage.bind(this) , 50);
},
ready: function() {
var canvas = this.$.canvas,
context = canvas.getContext("2d"),
video = this.$.video,
videoObj = { "video": true },
errBack = function(error) {
console.log("Video capture error: ", error.code);
};
this.timer = setInterval(this.processImage.bind(this) , 50);
// Put video listeners into place
if(navigator.getUserMedia) { // Standard
navigator.getUserMedia(videoObj, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
else if(navigator.mozGetUserMedia) { // Firefox-prefixed
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
},
processImage: function() {
this.digits = null;
var canvas = this.$.canvas;
var result = false;
context = canvas.getContext("2d");
video = this.$.video;
videoObj = { "video": true };
context.drawImage(video, 0, 0, this.width, this.height);
result = this.convertToGrayScale(context);
this.drawOverlay(context, result);
},
drawOverlay: function(context, success) {
context.strokeStyle = 'rgba(256,0,0,0.7)';
if(success == true) {
clearInterval(this.timer);
context.strokeStyle = 'rgba(0,256,0,0.7)';
}
context.lineWidth = 5;
context.beginPath();
context.moveTo(this.width/10, this.height/2);
context.lineTo(this.width/1.1, this.height/2);
context.stroke();
},
convertToGrayScale: function(context) {
var bars = [];
var pixels = [];
var binary = [];
var pixelBars = [];
// read one line of pixels right of the middle
var imgd = context.getImageData(this.width/10, this.height/2, this.width/1.1, 1);
var rgbpixels = imgd.data;
var imgd1 = context.getImageData(this.width/10, (this.height/2)-1, this.width/1.1, 1);
var rgbpixels1 = imgd1.data;
var imgd2 = context.getImageData(this.width/10, (this.height/2)-2, this.width/1.1, 1);
var rgbpixels2 = imgd1.data;
for (var i = 0, ii = rgbpixels.length; i < ii; i++) {
rgbpixels[i] = (rgbpixels[i] + rgbpixels1[i] + rgbpixels2[i])/3
}
// convert to grayscale respecting luminance
for (var i = 0, ii = rgbpixels.length; i < ii; i = i + 4) {
pixels.push(Math.round(rgbpixels[i] * 0.2126 + rgbpixels[i + 1] * 0.7152 + rgbpixels[ i + 2] * 0.0722));
}
// normalize
var min = Math.min.apply(null, pixels);
var max = Math.max.apply(null, pixels);
// convert to binary
for (var i = 0, ii = pixels.length; i < ii; i++) {
if (Math.round((pixels[i] - min) / (max - min) * 255) > this.threshold) {
binary.push(1);
} else {
binary.push(0);
}
}
// determine bar widths
var current = binary[0];
var count = 0;
for (var i = 0, ii = binary.length; i < ii; i++) {
if (binary[i] == current) {
count++;
} else {
pixelBars.push(count);
count = 1;
current = binary[i]
}
}
pixelBars.push(count);
// quality check
if (pixelBars.length < (3 + 24 + 5 + 24 + 3 + 1)) {
return;
}
// find starting sequence
var startIndex = 0;
var minFactor = 0.5;
var maxFactor = 1.5;
for (var i = 3, ii = pixelBars.length; i < ii; i++) {
var refLength = (pixelBars[i] + pixelBars[i-1] + pixelBars[i-2]) / 3;
if ((pixelBars[i] > (minFactor * refLength) || pixelBars[i] < (maxFactor * refLength))
&& (pixelBars[i-1] > (minFactor * refLength) || pixelBars[i-1] < (maxFactor * refLength))
&& (pixelBars[i-2] > (minFactor * refLength) || pixelBars[i-2] < (maxFactor * refLength))
&& (pixelBars[i-3] > 3 * refLength)
) {
startIndex = i - 2;
break;
}
}
// return if no starting sequence found
if (startIndex == 0) {
return;
}
// discard leading and trailing patterns
pixelBars = pixelBars.slice(startIndex, startIndex + 3 + 24 + 5 + 24 + 3);
// calculate relative widths
var ref = (pixelBars[0] + pixelBars[1] + pixelBars[2]) / 3;
for (var i = 0, ii = pixelBars.length; i < ii; i++) {
bars.push(Math.round(pixelBars[i] / ref * 100) / 100);
}
return this.decode(bars);
},
decode: function(bars) {
var upc = {
'0': [3, 2, 1, 1],
'1': [2, 2, 2, 1],
'2': [2, 1, 2, 2],
'3': [1, 4, 1, 1],
'4': [1, 1, 3, 2],
'5': [1, 2, 3, 1],
'6': [1, 1, 1, 4],
'7': [1, 3, 1, 2],
'8': [1, 2, 1, 3],
'9': [3, 1, 1, 2]
};
var check = {
'oooooo': '0',
'ooeoee': '1',
'ooeeoe': '2',
'ooeeeo': '3',
'oeooee': '4',
'oeeooe': '5',
'oeeeoo': '6',
'oeoeoe': '7',
'oeoeeo': '8',
'oeeoeo': '9'
}
// determine parity first digit and reverse sequence if necessary
var first = this.normalize(bars.slice(3, 3 + 4), 7);
if (!this.isOdd(Math.round(first[1] + first[3]))) {
bars = bars.reverse();
}
// split into digits
var digits = [
this.normalize(bars.slice(3, 3 + 4), 7),
this.normalize(bars.slice(7, 7 + 4), 7),
this.normalize(bars.slice(11, 11 + 4), 7),
this.normalize(bars.slice(15, 15 + 4), 7),
this.normalize(bars.slice(19, 19 + 4), 7),
this.normalize(bars.slice(23, 23 + 4), 7),
this.normalize(bars.slice(32, 32 + 4), 7),
this.normalize(bars.slice(36, 36 + 4), 7),
this.normalize(bars.slice(40, 40 + 4), 7),
this.normalize(bars.slice(44, 44 + 4), 7),
this.normalize(bars.slice(48, 48 + 4), 7),
this.normalize(bars.slice(52, 52 + 4), 7)
]
// determine parity and reverse if necessary
var parities = [];
for (var i = 0; i < 6; i++) {
if (this.parity(digits[i])) {
parities.push('o');
} else {
parities.push('e');
digits[i] = digits[i].reverse();
}
}
// identify digits
var result = [];
var quality = 0;
for (var i = 0, ii = digits.length; i < ii; i++) {
var distance = 9;
var bestKey = '';
for (key in upc) {
if (this.maxDistance(digits[i], upc[key]) < distance) {
distance = this.maxDistance(digits[i], upc[key]);
bestKey = key;
}
}
result.push(bestKey);
if (distance > quality) {
quality = distance;
}
}
// check digit
var checkDigit = check[parities.join('')];
// output
if((quality < this.quality) && (checkDigit != null)) {
this.digits = (checkDigit + result.join(''));
return true;
}
},
normalize: function(input, total) {
var sum = 0;
var result = [];
for (var i = 0, ii = input.length; i < ii; i++) {
sum = sum + input[i];
}
for (var i = 0, ii = input.length; i < ii; i++) {
result.push(input[i] / sum * total);
}
return result;
},
isOdd: function(num) {
return num % 2;
},
maxDistance: function(a, b) {
var distance = 0;
for (var i = 0, ii = a.length; i < ii; i++) {
if (Math.abs(a[i] - b[i]) > distance) {
distance = Math.abs(a[i] - b[i]);
}
}
return distance;
},
parity: function(digit) {
return this.isOdd(Math.round(digit[1] + digit[3]));
},
drawBars: function(context, binary) {
for (var i = 0, ii = binary.length; i < ii; i++) {
if (binary[i] == 1) {
context.strokeStyle = '#fff';
} else {
context.strokeStyle = '#000';
}
context.lineWidth = 3;
context.beginPath();
context.moveTo(start + i, height * 0.5);
context.lineTo(start + i + 1, height * 0.5);
context.stroke();
}
},
});
</script>
</dom-module>