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

Stop the camera after closing

Open HimonRana opened this issue 4 years ago • 31 comments

Is there a way to stop the camera after closing it.

HimonRana avatar Apr 17 '20 11:04 HimonRana

You must should close it after scanning, for the moment we cannot close the stream output of camera. Is a good purpose

Jonathan64340 avatar Jun 25 '20 07:06 Jonathan64340

What i succeded with for stopping the camera was having this:

ReactDOM.unmountComponentAtNode(component)

So you have to unmount the component.

HimonRana avatar Jun 25 '20 08:06 HimonRana

can we control this using a prop?

sibelius avatar Jul 17 '20 17:07 sibelius

I was able to create a ref and call myRef.current.stopCamera();. I'm not quite sure how to restart it after it's been stopped though.

kevinhikaruevans avatar Aug 07 '20 18:08 kevinhikaruevans

I was able to create a ref and call myRef.current.stopCamera();. I'm not quite sure how to restart it after it's been stopped though.

I don't see any stopCamera in their documentation, May I know where did you get that?

avillarubia avatar Sep 15 '20 13:09 avillarubia

I was able to create a ref and call myRef.current.stopCamera();. I'm not quite sure how to restart it after it's been stopped though.

I don't see any stopCamera in their documentation, May I know where did you get that?

I added the ref and just console.log'd it to see if there were any available functions on the ref.

kevinhikaruevans avatar Sep 18 '20 18:09 kevinhikaruevans

any update on this? its really necessary for closing it when needed

WorarojS avatar Mar 07 '21 05:03 WorarojS

Even i have requirement to disengage the web cam after scanning the QR, let us know will this be fixed or not.

santro85 avatar Jun 30 '21 11:06 santro85

You have to unmount the component since the author will stop the camera and remove the onScan event listener when the component is unmounted. Change your code from:

<QrReader />

to

{!isUnmounted && <QrReader />}

This works for me.

eddiechok avatar Jul 15 '21 09:07 eddiechok

@eddiechok , it didn't work for me. my reader component gets unmounted, but the camera remains on, Guys, any other ideas on such an obvious problem?

yuriy-vasilyev avatar Feb 22 '22 16:02 yuriy-vasilyev

@yuriy-vasilyev which version of the library are you using?

JonatanSalas avatar Feb 23 '22 01:02 JonatanSalas

@JonatanSalas , the one that has been installed be default - "3.0.0-beta-1". I forgot to mention the issue was happening in Chrome on my laptop (which is my development environment).

yuriy-vasilyev avatar Feb 23 '22 15:02 yuriy-vasilyev

Replicated the same error on a modal. Modal destroys on close and unmounts but camera stays on.

franco-marzano avatar Mar 04 '22 19:03 franco-marzano

@JonatanSalas having the same issue.

joshperrin avatar Mar 10 '22 13:03 joshperrin

@JonatanSalas I'd be happy to help fix this if your open to PRs.

joshperrin avatar Mar 10 '22 17:03 joshperrin

Replicated the same error on a modal. Modal destroys on close and unmounts but camera stays on.

Is your problem solved? If solved please tell me how to solve because I also use modals. The flash still on and runs continuously untill refresh the page

vijaySai-22 avatar Apr 28 '22 10:04 vijaySai-22

Hello, is there any progression about this issue ?

berkaykpinar avatar May 08 '22 17:05 berkaykpinar

Hello, when you want to close the stream add the following.

try { navigator.getUserMedia({ audio: false, video: true }, function (stream) { var track = stream.getTracks()[0]; // if only one media track track.stop(); }, function (error) { console.log('getUserMedia() error', error); }); } catch (e) { }

gardevgranade avatar May 19 '22 07:05 gardevgranade

Hello, is there any progression about this issue ?

BrunoTeixeiraNDB avatar Jun 06 '22 16:06 BrunoTeixeiraNDB

Did someone solve this issue? Any ideas?

gellert1229 avatar Jun 20 '22 07:06 gellert1229

Yea, same issue, it keeps calling onResult forever, even with component unmounted. The listener is never removed.

maxgomes92 avatar Jun 30 '22 18:06 maxgomes92

I worked around it by adding a useRef to stop returning the result over and over.

// Wrapper component

function Wrapper () {
  const [shouldRender, setShouldRender] = useState(false)

  return (
    <>
      { shouldRender &&  <QRCodeReader />}
      <button onClick={() => setShouldRender(true)}>
        Render QR Reader
     </button>
    </>
  )
}

// QR Code Reader component

function QRCodeReader ({ onResult }) {
  const lastResult = useRef()

  const onReadResult = (result, error) => {
    if (!result) return;
    
    // This callback will keep existing even after 
    // this component is unmounted
    // So ignore it (only in this reference) if result keeps repeating
    if (lastResult.current === result.text) {
      return
    }
    
    lastResult.current = result.text;
    onResult(result.text);
  };

  return (
    <QrReader onResult={onReadResult} />    
  )
}

maxgomes92 avatar Jul 04 '22 15:07 maxgomes92

Any update on this? is this solved?

firstpersoncode avatar Jul 30 '22 11:07 firstpersoncode

In the previous version it used to work, you can go back to v2.2 as mentioned in: https://github.com/react-qr-reader/react-qr-reader/issues/271

joseglego avatar Aug 05 '22 23:08 joseglego

It didn't work for me, I used https://nimiq.github.io/qr-scanner/ instead and working fine.

lsancheztapia avatar Aug 25 '22 20:08 lsancheztapia

Any update on this? is this solved?

I've successfully close the scanner with this implementation:

NOTE: I removed React StrictMode. And if you need to host your app in the future, you need to be in "HTTPS".

import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { QrReader } from "react-qr-reader";

const ScanQR = () => {
    const [isShown, setShowQr] = useState(true);
    const [scanResult, setScanResult] = useState(null);
    const navigate = useNavigate();

    const closePage = () => {
        // IMPORTANT! This is the magic to stop the qr reader.
        setShowQr(false);
        // you can change the path
        navigate("/path_to_somewhere");
    }

    return (
        <>
             {/* If you wish to stop the qr reader when going back to the previous page.*/}
            <button onClick={() => closePage()}>Back</button>
            {/* Or, implement a switch mechanism. */}
            <button onClick={() => setShowQr(!showQr)}>{isShown ? 'Stop' : 'Start'}</button>
            {isShown && (
                <QrReader
                    onResult={(res, err) => {
                        if (!!res) {
                            // Fetch the result.
                            // Note: QR reader will also stop after scanning.
                            setScanResult(res?.text);
                            // you can add some logic here e.g., redirect to other pages after scanning etc.
                        }
                        if (!!err) {
                            console.info(err);
                        }
                    }}
                />
            )}
        </>
    );
};

export default ScanQR;

2203798 avatar Dec 16 '22 23:12 2203798

I got into this issue where the camera stream was not terminated, even if the component had been unmounted. My discovery is that this is probably due to React strictMode where the rendering happened twice. This issue goes away in production in my case. (Since strict mode only renders twice in dev mode)

jengkhaw95 avatar Dec 22 '22 14:12 jengkhaw95

@jengkhaw95 Thank you. I removed StrictMode and the camera turn off when the component unmount

danny-dang avatar Jan 10 '23 11:01 danny-dang

I've successfully close the scanner with this implementation:

NOTE: I removed React StrictMode. And if you need to host your app in the future, you need to be in "HTTPS".

import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { QrReader } from "react-qr-reader";

const ScanQR = () => {
    const [isShown, setShowQr] = useState(true);
    const [scanResult, setScanResult] = useState(null);
    const navigate = useNavigate();

    const closePage = () => {
        // IMPORTANT! This is the magic to stop the qr reader.
        setShowQr(false);
        // you can change the path
        navigate("/path_to_somewhere");
    }

    return (
        <>
             {/* If you wish to stop the qr reader when going back to the previous page.*/}
            <button onClick={() => closePage()}>Back</button>
            {/* Or, implement a switch mechanism. */}
            <button onClick={() => setShowQr(!showQr)}>{isShown ? 'Stop' : 'Start'}</button>
            {isShown && (
                <QrReader
                    onResult={(res, err) => {
                        if (!!res) {
                            // Fetch the result.
                            // Note: QR reader will also stop after scanning.
                            setScanResult(res?.text);
                            // you can add some logic here e.g., redirect to other pages after scanning etc.
                        }
                        if (!!err) {
                            console.info(err);
                        }
                    }}
                />
            )}
        </>
    );
};

export default ScanQR;

2203798 avatar Jan 10 '23 11:01 2203798

I got into this issue where the camera stream was not terminated, even if the component had been unmounted. My discovery is that this is probably due to React strictMode where the rendering happened twice. This issue goes away in production in my case. (Since strict mode only renders twice in dev mode)

Thank you so much, this was driving me insane.

HedgeWizardly avatar Apr 16 '23 19:04 HedgeWizardly