cordova-plugin-qrscanner icon indicating copy to clipboard operation
cordova-plugin-qrscanner copied to clipboard

[IONIC 4] the camera is still enabled on the background

Open lim-enhui opened this issue 5 years ago • 21 comments

anyone have same issue noticing the camera is still running visible on the background despite switching to another component? I suppose angular would have destroyed it...

lim-enhui avatar Jun 07 '19 12:06 lim-enhui

Did you use the destroy() function?

From the documentation:

QRScanner.destroy(function(status){
  console.log(status);
});

Besides that, there are options to hide or cancel the scan. You can do this too, whenever you switch to a different component, or whenever the scan was successful.

DerkJanSpeelman avatar Jun 18 '19 08:06 DerkJanSpeelman

I dont want the intention to destroy but rather hide... cause if it is destroy i am not able to reinitiate when back to the page to initiate again. however hide, still the camera function is running on background although qrcode is not scanning...

lim-enhui avatar Jul 01 '19 06:07 lim-enhui

Did you manage to solve it?

The same as my camera shows on my route.

AlexisNarvaez97 avatar Jul 10 '19 16:07 AlexisNarvaez97

If you're talking about pages, you can use the Ionic lifecycle hooks. If you're really talking about components, you can use the lifecycle hooks of the framework you're using. A little search on the Ionic docs should help.

If you got a working solution you can post it here for the others.

RafaelKr avatar Jul 15 '19 21:07 RafaelKr

With the latest version of ionic 3 too, when I do a destroy, on a page dedicated to the scan, I have the impression that the plugin he stopped scanning, the camera is disabled, "webview" becomes white, but when we change pages with "setRoot", then we push to another page, then we dream in the previous page manually in ios, we can see that the camera remains active.

The solution is to inspect the source code of the plugin, to find the bug. I'm using the latest version of IOS 12.3, not IOS 13.

GomesGokuBR avatar Jul 16 '19 08:07 GomesGokuBR

I think I have found a solution, but I think the documentation does not necessarily explain. First we have to do an "unsubscribe" of the scan, returned by the scan.subscribe () function, then a destroy, then normally, the camera does not turn in the background anymore.

Can you confirm that?

thank you in advance.

GomesGokuBR avatar Jul 16 '19 08:07 GomesGokuBR

I'm having this issue as well. Even after going trough:

QRScanner.disableLight(function (err, status) {err && console.error(err); onsole.log(status);});
QRScanner.cancelScan(function (status) {console.log(status);});
QRScanner.hide(function (status) {console.log(status);});
QRScanner.destroy(function (status) {console.log(status);});

My camera is still on and recording in the background. Here's the Scanner status logged from the last destroy call:

{"authorized": false,
"canChangeCamera": false,
"canEnableLight": false,
"canOpenSettings": false,
"currentCamera": 0,
"denied": false,
"lightEnabled": false,
"prepared": false,
"previewing": true,
"restricted": false,
"scanning": false,
"showing": false,
"__proto__":{}
}

This is done on a cordova app using the plugin and running on the browser platform (chrome, last stable release, if it matters.). While it's not that big of a deal, it bothers me to know the camera is still on when I'm not using it. Any news would be appreciated, thanks! ^^

Darkxell avatar Jul 18 '19 13:07 Darkxell

@Darkxell I also had the same problem, the camera is still on and recording in the background.Do you have a solution?

HvanTao avatar Aug 04 '19 10:08 HvanTao

Sadly no, sorry.

Darkxell avatar Aug 08 '19 16:08 Darkxell

this.qrScanner.pausePreview();
this.qrScanner.hide();

lim-enhui avatar Aug 08 '19 16:08 lim-enhui

Having the same issue. Looks like scan not finishing properly. If I do not call scan (only prepare and show) - hide disables camera. If I start scan - all hooks work, but camera is still active in the background after hide and even destroy. Somebody had managed to solve this problem?

PetrovskYYY avatar Sep 25 '19 15:09 PetrovskYYY

Up, stuck with the same problem!

DydjyZ avatar Dec 18 '19 17:12 DydjyZ

I still have this issue where QRScanner is still active in the background even when this.qrScanner.destroy() is called. Is this repo still being maintained?

Is there any fix to make sure the scanner is not active?

madsthom avatar Feb 05 '20 12:02 madsthom

I have no problems, I even commented above. Me it works in very well with Android and IOS.

GomesGokuBR avatar Feb 05 '20 13:02 GomesGokuBR

I have no problems, I even commented above. Me it works in very well with Android and IOS.

Actually, it seems to work with your solution, but I have no way of verifying if the scanner/camera is still active. This would drain the battery quick if this is the case.

Anyway, thank you for your reply @TekinGomes37.

madsthom avatar Feb 05 '20 14:02 madsthom

I have no problems, I even commented above. Me it works in very well with Android and IOS.

Actually, it seems to work with your solution, but I have no way of verifying if the scanner/camera is still active. This would drain the battery quick if this is the case.

Anyway, thank you for your reply @TekinGomes37.

I checked if the scan and the camera remained active, just before the solution I proposed, my camera remained active, to the point that when a qrcode passed, I had feedback from the plug-in. Before writing this solution, "which I personally find not clean", I checked with Android Studio, as well as rated Xcode. I did a long test. I had no problems with battery discharges. But before we are done, I had glaring battery discharge problems, for example, the laptop was heating up, due to the fact that the scan remained active.

Sorry for my level of English.

GomesGokuBR avatar Feb 06 '20 08:02 GomesGokuBR

I think I have found a solution, but I think the documentation does not necessarily explain. First we have to do an "unsubscribe" of the scan, returned by the scan.subscribe () function, then a destroy, then normally, the camera does not turn in the background anymore.

Can you confirm that?

thank you in advance.

Unfortunately, this solution blocks the screen.

I'm still with this problem, anyone found a solution?

almogyehuda avatar Mar 08 '20 07:03 almogyehuda

Its the 8th of April 2020, 4.8 billion people are in lockdown due to Covid-19. I really hope the developers of this plugin who are sitting and getting bored at home end up finding a solution for this issue.

mahen23 avatar Apr 08 '20 06:04 mahen23

I too have this issue where Camera is still active in the background even if this.qrScanner.destroy() is called. Any fix for this?

akshatjoshii avatar Apr 13 '20 14:04 akshatjoshii

Just a follow up on this, I have found that as long as all you call is destroy(), then camera is no longer active. hide/pausepreview, does still keep camera active. But destroy definitely works for me.

Also after this.qrScanner.destroy() I can restart the scan no problem. Looking at the swift code, destroy method does free the camera resources.

sjdrew avatar Feb 22 '21 15:02 sjdrew

I encountered the same issue and it was fixed by keeping track of all of the stream instances created and stopping them manually once you're done. Please see steps and sample code below:

Create a variable that will use to store the streams videoStreams: any[] = []

Create a function that would store created instance


  saveCurrentQRStream() {
      this.qrScannerVideo = document.querySelector('video')
      this.videoStreams.push(this.qrScannerVideo.srcObject)
    }
  }

Create a function that would destroy all stream instances


  destroyQRStreams = async () => {
    if (
      this.videoStreams
      && this.videoStreams.length === 0
    ) {
      return
    }

    let videoTracks = []
    this.qrScannerVideo.srcObject = null

    await this.videoStreams.forEach(stream => {
      videoTracks.push(stream.getVideoTracks()[0])
    })

    await videoTracks.forEach((track) => {
      track.enabled = false
      track.stop()
    })

    return 
  }

Invoke the save function right after calling the show method

  await this.qrScanner.show()
  // camera permission was granted

  // Save stream instance
  await this.saveCurrentQRStream()

Add call stream destroy function if you are done with the scanner

The possible cause of the issue is that maybe the QR scanner plugin unconsciously created another instance of a stream and lost track of the previously created one. That's why when we call the destroy method, it only stops the current instance of a stream that is tracked by the plugin.

ewojjowe avatar Mar 01 '22 02:03 ewojjowe