Skip to content

Instantly share code, notes, and snippets.

@kakha13
Last active September 15, 2021 20:24
Show Gist options
  • Save kakha13/37c4356d1a28d5c59489f8c616345f64 to your computer and use it in GitHub Desktop.
Save kakha13/37c4356d1a28d5c59489f8c616345f64 to your computer and use it in GitHub Desktop.
Read Barcode in browser from image element
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