QuaggaJS Barcode scanner - live stream
Live Stream - decodes code 39,128
by vrmtm
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/quagga/0.11.6/quagga.js"></script>
<section id="container" class="container">
<div class="controls">
<fieldset class="input-group">
<button class="stop">Stop</button>
</fieldset>
<fieldset class="reader-config-group">
<label>
<span>Camera</span>
<select name="input-stream_constraints" id="deviceSelection">
</select>
</label>
</fieldset>
</div>
<div id="result_strip">
<ul class="thumbnails"></ul>
</div>
<div id="interactive" class="viewport"></div>
</section>
CSS
@charset "UTF-8";
.collapsable-source pre {
font-size: small;
}
.input-field {
display: flex;
align-items: center;
width: 260px;
}
.input-field label {
flex: 0 0 auto;
padding-right: 0.5rem;
}
.input-field input {
flex: 1 1 auto;
height: 20px;
}
.input-field button {
flex: 0 0 auto;
height: 28px;
font-size: 20px;
width: 40px;
}
.icon-barcode {
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZpZXdCb3g9IjAgMCAzMiAzMiI+PHBhdGggZD0iTTAgNGg0djIwaC00ek02IDRoMnYyMGgtMnpNMTAgNGgydjIwaC0yek0xNiA0aDJ2MjBoLTJ6TTI0IDRoMnYyMGgtMnpNMzAgNGgydjIwaC0yek0yMCA0aDF2MjBoLTF6TTE0IDRoMXYyMGgtMXpNMjcgNGgxdjIwaC0xek0wIDI2aDJ2MmgtMnpNNiAyNmgydjJoLTJ6TTEwIDI2aDJ2MmgtMnpNMjAgMjZoMnYyaC0yek0zMCAyNmgydjJoLTJ6TTI0IDI2aDR2MmgtNHpNMTQgMjZoNHYyaC00eiI+PC9wYXRoPjwvc3ZnPg==);
}
.overlay {
overflow: hidden;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.3);
}
.overlay__content {
top: 50%;
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
width: 90%;
max-height: 90%;
max-width: 800px;
}
.overlay__close {
position: absolute;
right: 0;
padding: 0.5rem;
width: 2rem;
height: 2rem;
line-height: 2rem;
text-align: center;
background-color: white;
cursor: pointer;
border: 3px solid black;
font-size: 1.5rem;
margin: -1rem;
border-radius: 2rem;
z-index: 100;
box-sizing: content-box;
}
.overlay__content video {
width:...
JavaScript
/**Quagga initialiser starts here*/
$(function() {
var value;
var App = {
init : function() {
Quagga.init(this.state, function(err) {
if (err) {
console.log(err);
return;
}
App.attachListeners();
Quagga.start();
});
},
initCameraSelection: function(){
var streamLabel = Quagga.CameraAccess.getActiveStreamLabel();
return Quagga.CameraAccess.enumerateVideoDevices()
.then(function(devices) {
function pruneText(text) {
return text.length > 30 ? text.substr(0, 30) : text;
}
var $deviceSelection = document.getElementById("deviceSelection");
while ($deviceSelection.firstChild) {
$deviceSelection.removeChild($deviceSelection.firstChild);
}
devices.forEach(function(device) {
var $option = document.createElement("option");
$option.value = device.deviceId || device.id;
$option.appendChild(document.createTextNode(pruneText(device.label || device.deviceId || device.id)));
$option.selected = streamLabel === device.label;
$deviceSelection.appendChild($option);
});
});
},
querySelectedReaders: function() {
return Array.prototype.slice.call(document.querySelectorAll('.readers input[type=checkbox]'))
.filter(function(element) {
return !!element.checked;
})
.map(function(element) {
return element.getAttribute("name");
});
},
attachListeners: function() {
var self = this;
self.initCameraSelection();
$(".controls").on("click", "button.stop", function(e) {
...