Created
August 14, 2017 20:32
-
-
Save asolove/109b17078efbc614290cb3586ba58a53 to your computer and use it in GitHub Desktop.
Scan Barcodes for Project Cure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var searchInput = document.getElementById("SearchBox"); | |
var scanInput = document.createElement("input"); | |
scanInput.type = "file"; | |
scanInput.accept = "image/*"; | |
scanInput.addEventListener("change", onFileChange); | |
searchInput.parentElement.insertBefore(scanInput, searchInput); | |
function onFileChange() { | |
readImageData(scanInput.files[0], function(error, imageData) { | |
if(error) return console.error(error); | |
findBarcode(imageData, function(error, barcode) { | |
if(error) return console.error(error); | |
searchInput.value = barcode.code; | |
searchInput.form.submit(); | |
}); | |
}) | |
} | |
function readImageData(file, callback) { | |
var reader = new FileReader(); | |
reader.addEventListener("load", function () { | |
callback(null, reader.result); | |
}, false); | |
if (file) { | |
reader.readAsDataURL(file); | |
} | |
} | |
function findBarcode(imageData, callback) { | |
Quagga.decodeSingle({ | |
decoder: { | |
readers: ["code_128_reader"] // List of active readers, may need additions | |
}, | |
locate: true, | |
src: imageData | |
}, function(result){ | |
if(result.codeResult) { | |
callback(null, result.codeResult); | |
} else { | |
callback("Found no bar code!") | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment