Last active
September 15, 2021 20:24
-
-
Save kakha13/37c4356d1a28d5c59489f8c616345f64 to your computer and use it in GitHub Desktop.
Read Barcode in browser from image element
This file contains hidden or 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 imageEl = document.getElementById("test_image"); // image element | |
// check compatibility | |
if (!("BarcodeDetector" in window)) { | |
console.log("Barcode Detector is not supported by this browser."); | |
} else { | |
console.log("Barcode Detector supported!"); | |
// create new detector | |
var barcodeDetector = new BarcodeDetector(); // enable any supported barcode | |
// read code from image element | |
barcodeDetector | |
.detect(imageEl) | |
.then((barcodes) => { | |
barcodes.forEach((barcode) => console.log(barcode.rawValue)); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment