-
Notifications
You must be signed in to change notification settings - Fork 1
/
barcode.js
60 lines (53 loc) · 1.47 KB
/
barcode.js
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
var video;
var canvas;
var detectedBarcodes;
var errorDiv;
var barcodeDetector;
function setup() {
noCanvas();
errorDiv = document.getElementById('error');
canvas = document.getElementById('canvas');
video = document.getElementById('video');
var constraints = {video: {facingMode: 'environment'}};
navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
video.srcObject = mediaStream;
video.onloadedmetadata = function(e) {
video.play();
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
};
})
.catch(function(err) { });
}
function draw() {
if(typeof detectedBarcodes !== 'undefined'){
for(barcode of detectedBarcodes){
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.lineWidth='6';
ctx.strokeStyle='red';
ctx.rect(barcode.boundingBox.x,barcode.boundingBox.y,barcode.boundingBox.width,barcode.boundingBox.height);
ctx.stroke();
ctx.font='20px Arial';
ctx.fillStyle = 'red';
ctx.fillText(barcode.rawValue,barcode.boundingBox.x-5,(barcode.boundingBox.y+barcode.boundingBox.height+20));
}
}
}
async function getBarcodes(){
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
if(!video.paused){
try{
barcodeDetector = new BarcodeDetector();
const barcodes = await barcodeDetector.detect(canvas);
detectedBarcodes = barcodes;
}
catch(exception) {
errorDiv.innerText = exception;
};
}
}
setInterval(function(){
getBarcodes()
}, 200);