bitpay / cordova-plugin-qrscanner

A fast, energy efficient, highly-configurable QR code scanner for Cordova apps and the browser.
MIT License
568 stars 772 forks source link

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

Open lim-enhui opened 4 years ago

lim-enhui commented 4 years ago

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...

DerkJanSpeelman commented 4 years ago

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.

lim-enhui commented 4 years ago

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...

AlexisNarvaez97 commented 4 years ago

Did you manage to solve it?

The same as my camera shows on my route.

RafaelKr commented 4 years ago

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.

GomesGokuBR commented 4 years ago

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 commented 4 years ago

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.

Darkxell commented 4 years ago

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! ^^

HvanTao commented 4 years ago

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

Darkxell commented 4 years ago

Sadly no, sorry.

lim-enhui commented 4 years ago
this.qrScanner.pausePreview();
this.qrScanner.hide();
PetrovskYYY commented 4 years ago

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?

DydjyZ commented 4 years ago

Up, stuck with the same problem!

madsthom commented 4 years ago

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?

GomesGokuBR commented 4 years ago

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

madsthom commented 4 years ago

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.

GomesGokuBR commented 4 years ago

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.

almogyehuda commented 4 years ago

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?

mahen23 commented 4 years ago

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.

akshatjoshii commented 4 years ago

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

sjdrew commented 3 years ago

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.

ewojjowe commented 2 years ago

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.