react-qr-reader icon indicating copy to clipboard operation
react-qr-reader copied to clipboard

Unable to stop the camera

Open Blesha843 opened this issue 3 years ago • 9 comments
trafficstars

after getting the result , not camera will not stop running. When i change my route/path to another the camera is still running in background. how can i stop running it in background ? any idea ? thanks

Blesha843 avatar Jul 15 '22 02:07 Blesha843

Hi @Blesha843 , I din't find a way to stop the camera on the version 3.0-beta (check the package.json), so I just downgrade to 2.2.0 (https://github.com/react-qr-reader/react-qr-reader/tree/v2.2.0) and worked normally.

adrianowy avatar Jul 28 '22 02:07 adrianowy

If you modify this bit of code, it should work. I won't make a PR since the maintainer seems to be MIA. What I think is happening is the codeReader continues to run after the useEffect cleanup executes IF the cleanup runs while the Promise is pending

      codeReader
        .decodeFromConstraints({ video }, videoId, (result, error) => {
          if (isValidType(onResult, 'onResult', 'function')) {
            if (controlsRef.current === null) {
              throw new Error('Component is unmounted');
            }
            onResult(result, error, codeReader);
          }
        })

culda avatar Aug 15 '22 09:08 culda

@adrianowy Reverting back to 2.2.0 has a dependency error on Windows, are there any other workarounds?

t-lamp11 avatar Aug 15 '22 23:08 t-lamp11

@t-lamp11 I haven't this issue because I'm using Linux. But you can try to use Docker with WSL.

adrianowy avatar Aug 15 '22 23:08 adrianowy

hi, My solution was:

-Create a varible:

//500 is the default delay const [delayScan , setDelayScan] = useState(500);

and in QrReader component:

<QrReader
  `scanDelay={delayScan}`
style={{ width: '100%' }}
constraints={{
facingMode: 'environment'
 }}
onResult={(result, error) => {
 if (!!result) {
console.log(result, "res");
setDelayScan(false)
}
if (!!error) {
 console.log("QrReader:", error);
  }
 }}

I set the "delayScan" to false and that stop the camera when the reader find a QR.

I hope that works for you guys.

Digonz avatar Aug 19 '22 22:08 Digonz

raised a PR https://github.com/react-qr-reader/react-qr-reader/pull/279 This has fixed the issue for us

culda avatar Aug 20 '22 06:08 culda

i fix this removing StricMode of react

abel1711 avatar Apr 07 '23 17:04 abel1711

Make the ref of the QRReader and stop it after getting the result.

const qrReader = React.createRef();

make function:

   const handleScan = (result, error) => {
        console.log("HAHHAHA");
        
          console.log("Result",result);
        if (!!result) {
          setQrData(result?.text);
          qrReader.current.stop();
        }

        if (!!error) {
          console.info(error);
        }
      };

Here is my Div:

 <div style={{ width: "40%" }}>
                        {qrData == "" ? (
                          <QrReader
                            scanDelay={delayScan}
                            ref={qrReader}
                            onResult={(result, error) =>
                              handleScan(result, error)
                            }
                            style={{ width: "100%" }}
                          />
                        ) : null}
                        <p>{qrData}</p>
                      </div>

ORODeveloper avatar Jun 06 '23 07:06 ORODeveloper

Hi @Blesha843 , I din't find a way to stop the camera on the version 3.0-beta (check the package.json), so I just downgrade to 2.2.0 (https://github.com/react-qr-reader/react-qr-reader/tree/v2.2.0) and worked normally.

Thanks a lot for sharing @Blesha843. I was facing this issue to stop using camera as in some Android devices majorly Samsung devices I was unable to access the camera for other components until I stopped using it I tried all ways like revoking the camera access but nothing worked. And the issue was solved by downgrading to 2.2.0

Abhiram010 avatar Jan 06 '24 10:01 Abhiram010