Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quagga.stop not working #529

Closed
usamahassankhan opened this issue Dec 22, 2023 · 8 comments
Closed

Quagga.stop not working #529

usamahassankhan opened this issue Dec 22, 2023 · 8 comments

Comments

@usamahassankhan
Copy link

Hi Eric could you please help why quagga.stop() not stopping the camera ,it starts fine but after start we can't stop it ,need your prompt support

Copy link

Thank you for filing an issue! Please be patient. :-)

@ericblade
Copy link
Owner

When I've encountered this it's always been some code outside of Quagga that is doing something that prevents the browser from detaching the camera. What is your code doing when you stop?

@usamahassankhan
Copy link
Author

usamahassankhan commented Dec 22, 2023 via email

@ericblade
Copy link
Owner

ericblade commented Dec 22, 2023

I don't know, you could try it, it's a drop-in replacement. If you show your code, that might help figure out what you're doing.

In Q2, I did add a promise return to the stop function, so that there's something to await on to know that the browser has completed the stop.

@ericblade
Copy link
Owner

ericblade commented Dec 22, 2023

When I personally had this issue, it was calling stop in React unmount, but then the component was being immediately remounted again then unmounted, due to some weird animation interaction. Difficult to say what yours is doing without seeing code. The other example you commented on, the stop call is never even being hit, because their code just errors and doesn't run.

@usamahassankhan
Copy link
Author

usamahassankhan commented Dec 22, 2023 via email

@ericblade
Copy link
Owner

If you re-format your code so it's indented properly

import React, { useEffect } from "react";

import Quagga from "quagga";

import config from "./../../config.json";

const Scanner = (props) => {
  const { onDetected } = props;

  useEffect(() => {
    Quagga.init(config, (err) => {
      if (err) {
        console.log(err, "error msg");
      }

      Quagga.start();

      return () => {
        Quagga.stop();
      };
    });

    //detecting boxes on stream

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

      if (result) {
        if (result.boxes) {
          drawingCtx.clearRect(
            0,

            0,

            Number(drawingCanvas.getAttribute("width")),

            Number(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(detected);
  }, []);

  const detected = (result) => {
    onDetected(result.codeResult.code);
  };

  return (
    // If you do not specify a target,

    // QuaggaJS would look for an element that matches

    // the CSS selector #interactive.viewport

    <div id="interactive" className="viewport" />
  );
};

export default Scanner;

You can see on line 18, you are returning the closure that calls Quagga.stop() from Quagga.init() but you need to be returning it from useEffect(). Probably that whole remainder of that block is in the wrong scope, too.

Your Quagga.stop is never being hit.

You should work to improve your code formatting, so errors like that are more visible when looking at it. Also, if you used a debugger breakpointed at the stop() call, you would see that it was never being hit .. or just using a console.log() before it.

@usamahassankhan
Copy link
Author

usamahassankhan commented Dec 24, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants