serratus / quaggaJS

An advanced barcode-scanner written in JavaScript
https://serratus.github.io/quaggaJS/
MIT License
5.03k stars 978 forks source link

Returning different results for same barcode? #436

Open kunal3210 opened 4 years ago

kunal3210 commented 4 years ago

Why is it returning different results for same barcode.I really don't understand the reason behind it.

Here is an image of different results i have only scanned one barcode and have got many different values.

barcode console

And here's my code

var jquery = document.createElement('script');
jquery.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js';
document.head.appendChild(jquery);
$.getScript("dist/quagga.js",function(){
$(document).ready(function(){
if (navigator.mediaDevices && typeof navigator.mediaDevices.getUserMedia === 'function') {
  Quagga.init({
    inputStream : {
      name : "Live",
      type : "LiveStream",
      target: document.querySelector('#barcode_scanner')    // Or '#yourElement' (optional)
    },
    decoder : {
      readers : ["ean_reader","ean_8_reader","code_39_reader","code_39_vin_reader","codabar_reader","upc_reader","upc_e_reader","i2of5_reader","2of5_reader","code_93_reader"]
    }, 
  },
     function(err) {
      if (err) {
          console.log(err);
          return
      }
      console.log("Initialization finished. Ready to start");
      Quagga.start();
  });

  /*
   Quagga.onProcessed(function(result) {
        var drawingCtx = Quagga.canvas.ctx.overlay,
            drawingCanvas = Quagga.canvas.dom.overlay;

        if (result) {
            if (result.boxes) {
                drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
                result.boxes.filter(function (box) {
                    return box !== result.box;
                }).forEach(function (box) {
                    Quagga.ImageDebug.drawPath(box, {x: 0, y: 1}, drawingCtx, {color: "green", lineWidth: 2});
                });
            }

            if (result.box) {
                Quagga.ImageDebug.drawPath(result.box, {x: 0, y: 1}, drawingCtx, {color: "#00F", lineWidth: 2});
            }

            if (result.codeResult && result.codeResult.code) {
                Quagga.ImageDebug.drawPath(result.line, {x: 'x', y: 'y'}, drawingCtx, {color: 'red', lineWidth: 3});
            }
        }
    });
*/

  Quagga.onDetected(function(data){
    var result = data.codeResult.code;
    console.log(result);
        $.ajax({
        url : 'quagga.php',
        type : 'post',
        data :{result : result},
    });
  });
 }
});
});

Please tell me what to do next.

FFParf commented 4 years ago

I have the same problem

ericblade commented 4 years ago

Check the errors result, and/or validate that the barcode scanned is valid, before passing it on. Especially from a LiveStream, your input image may have significant differences between scans, maybe your autofocus hardware/software is figuring it out at the same time the reader is scanning, the position is different, etc. It's going to be a lot more stable if you feed it the identical image repeatedly.

kunal3210 commented 4 years ago

@ericblade Thanks for your reply, i solved it by mentioning all the possible parameters for readers as

 decoder : {
      readers : ["code_128_reader","ean_reader","ean_8_reader","code_39_reader","code_39_vin_reader","codabar_reader","upc_reader","upc_e_reader","i2of5_reader","2of5_reader","code_93_reader"]
    }, 
ericblade commented 4 years ago

Probably not a good idea to have ALL of the barcode readers, but hey, if it works, go for it