2

我可以使用 QuaggaJS 扫描条形码。但是,一旦检测到条形码,我就停止使用它Quagga.stop();,然后继续我的功能。如果我的函数返回 false,那么我必须再次启动相机并且我正在使用Quagga.start()它但它不工作。给我留下了错误信息

typeerror 无法读取未定义的属性“数据”

如果我重新初始化该功能,那么它可以工作,但随后移动浏览器 flickr 3-4 秒,然后变得稳定。

这是我的代码

$(document).ready(function(){
    if($(".scanner-box").length > 0){
        if (_scannerIsRunning) {
            Quagga.stop();
        } else {
            startScanner();
        }
    }
}

var _scannerIsRunning = false;
function startScanner() {
Quagga.init({
        inputStream: {
                name: "Live",
                type: "LiveStream",
                target: document.querySelector('#scanner-container'),
                constraints: {
                        width: "100%",
                        height: "100%",
                        facingMode: "environment"
                },
        },
        decoder: {
                readers: [
                        "ean_reader",
                        "ean_8_reader"
                ],
                debug: {
                        showCanvas: true,
                        showPatches: true,
                        showFoundPatches: true,
                        showSkeleton: true,
                        showLabels: true,
                        showPatchLabels: true,
                        showRemainingPatchLabels: true,
                        boxFromPatches: {
                                showTransformed: true,
                                showTransformedBox: true,
                                showBB: true
                        }
                }
        },
},
function (err) {
        if (err) {
                $("#error").text(err);
                return
        }
        console.log("Initialization finished. Ready to start");
        Quagga.start();
        _scannerIsRunning = true;
});
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 (result) {
        var barcodeResult = $("#result").text(result.codeResult.code);
        var barcode     = result.codeResult.code;
        if(barcode.toString().length < '13'){
                
        }else{
                if (_scannerIsRunning) {
                        Quagga.stop();
                }
                var checkCode = checkBarCode(barcode,canvasRatio,canvasHeight);
                if(!checkCode){
                      Quagga.start();
                      //startScanner(); //other option
                }   
        }
        console.log("Barcode detected and processed : [" + result.codeResult.code + "]", result);
  });
}
4

1 回答 1

0

您只需要在Quagga.stop之后使用所需的参数调用Quagga.init

例如,在您的情况下,您应该致电:

Quagga.init({
        inputStream: {
                name: "Live",
                type: "LiveStream",
                target: document.querySelector('#scanner-container'),
                constraints: {
                        width: "100%",
                        height: "100%",
                        facingMode: "environment"
                },
        },
        decoder: {
                readers: [
                        "ean_reader",
                        "ean_8_reader"
                ],
                debug: {
                        showCanvas: true,
                        showPatches: true,
                        showFoundPatches: true,
                        showSkeleton: true,
                        showLabels: true,
                        showPatchLabels: true,
                        showRemainingPatchLabels: true,
                        boxFromPatches: {
                                showTransformed: true,
                                showTransformedBox: true,
                                showBB: true
                        }
                }
        },
},
function (err) {
        if (err) {
                $("#error").text(err);
                return
        }
        console.log("Initialization finished. Ready to start");
        Quagga.start();
        _scannerIsRunning = true;
});
于 2021-11-13T06:23:20.433 回答