首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在停止使用QuaggaJS后启动摄像机

如何在停止使用QuaggaJS后启动摄像机
EN

Stack Overflow用户
提问于 2020-10-16 16:34:51
回答 1查看 1.3K关注 0票数 2

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

类型错误无法读取未定义的属性“数据”

如果我重新初始化函数,那么它可以工作,但是移动浏览器闪烁3-4秒,然后稳定下来。

这是我的密码

代码语言:javascript
复制
$(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);
  });
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-13 06:23:20

只需在Quagga.init之后调用所需的参数Quagga.stop即可。

例如,在您的情况下,您应该调用:

代码语言:javascript
复制
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;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64393120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档