cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <button id="detect" onclick="detect()"  class="btn btn-lg btn-primary">Detect barcodes</button>
<div>
<img hidden id="image" crossorigin="anonymous" src="https://upload.wikimedia.org/wikipedia/en/f/f4/LiveBarcodeApp.jpg"> <br>
<canvas id="canvas" width=320 height=320>
</div>
<footer></footer>


            
          
!
            
              var image = document.getElementById('image');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 
              0, 0, image.width, image.height,
              0, 0, canvas.width, canvas.height);

const scale = canvas.width / image.width;

function detect() {
  if (window.BarcodeDetector == undefined) {
    var footer = document.getElementsByTagName('footer')[0];
    footer.innerHTML = "Barcode Detection not supported";
    console.error('Barcode Detection not supported');
    return;
  }
  
  var barcodeDetector = new BarcodeDetector();
  barcodeDetector.detect(image)
    .then(barcodes => {
      // Draw the boxes on the <canvas>.
      var ctx = document.getElementById('canvas').getContext("2d");
      ctx.beginPath();
      ctx.lineWidth = 2;
      ctx.strokeStyle = "red";
      for(var i = 0; i < barcodes.length; i++) {
        ctx.rect(Math.floor(barcodes[i].boundingBox.x * scale), 
                 Math.floor(barcodes[i].boundingBox.y * scale),
                 Math.floor(barcodes[i].boundingBox.width * scale), 
                 Math.floor(barcodes[i].boundingBox.height * scale));
        ctx.stroke();
      }
      ctx.closePath();

      ctx.beginPath();
      ctx.strokeStyle = "yellow";
      for(var i = 0; i < barcodes.length; i++) {
        ctx.moveTo(Math.floor(barcodes[i].cornerPoints[0].x * scale), 
                   Math.floor(barcodes[i].cornerPoints[0].y * scale));
        ctx.lineTo(Math.floor(barcodes[i].cornerPoints[1].x * scale), 
                   Math.floor(barcodes[i].cornerPoints[1].y * scale));
        ctx.lineTo(Math.floor(barcodes[i].cornerPoints[2].x * scale), 
                   Math.floor(barcodes[i].cornerPoints[2].y * scale));
        ctx.lineTo(Math.floor(barcodes[i].cornerPoints[3].x * scale), 
                   Math.floor(barcodes[i].cornerPoints[3].y * scale));
        ctx.lineTo(Math.floor(barcodes[i].cornerPoints[0].x * scale), 
                   Math.floor(barcodes[i].cornerPoints[0].y * scale));
        ctx.stroke();
      }
      ctx.closePath();
    
      // Add the barcodes as strings to the <footer>
      var footer = document.getElementsByTagName('footer')[0];
      footer.innerHTML = 
          '<p>Detected ' + barcodes.length + ' barcodes</p><ul>';
      for(var i = 0; i < barcodes.length; i++) {
        footer.innerHTML += 
            '<li>@ (' + barcodes[i].boundingBox.x + ',' + 
            barcodes[i].boundingBox.y + '), size (' + 
            barcodes[i].boundingBox.width + 'x' + 
            barcodes[i].boundingBox.height + ')' + 
            '; rawValue=' + barcodes[i].rawValue + '</li>';
      }
      footer.innerHTML += '</ul>';
  
    }).catch((e) => {
      var footer = document.getElementsByTagName('footer')[0];
      footer.innerHTML = "Boo, Barcode Detection failed: " + e;
      console.error("Boo, Barcode Detection failed: " + e);
    })
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console