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

How to stop the reader and turn off the camera #21

Closed
DarrenWainwright opened this issue Dec 8, 2020 · 9 comments
Closed

How to stop the reader and turn off the camera #21

DarrenWainwright opened this issue Dec 8, 2020 · 9 comments

Comments

@DarrenWainwright
Copy link

DarrenWainwright commented Dec 8, 2020

Using: "@zxing/browser": "0.0.5"
Codebase: TypeScript project using lit-element

Hello,

I'm trying to implement this browser version of the library in a small UI to scan PDF417 barcodes.

I am having a hard time understanding how to stop listening/scanning/reading and turning the camera off.

In the /library version I see I can call stop() on the reader, though it doesn't appear to be available in this browser version for both types of VideoDevice decodes.

These pieces of code are lifted from the overall solution I'm trying to build.

In this first snippet I'm trying to decode once.

In this case, how can I stop the stream and turn off the camera - like a reset

  async decodeOnce(codeReader: BrowserPDF417Reader, selectedDeviceId: string){
    const videoElem = this.shadowRoot?.getElementById('video');    
    codeReader.decodeOnceFromVideoDevice(selectedDeviceId, videoElem).then((result) =>{
      console.log(result); 
    }).catch( (reason) => {
          console.log(reason);
    });
  }

In the DecodeContinously I can grab controls to stop the stream. I can now call this.controls.stop()

async decodeContinuously(codeReader: BrowserPDF417Reader, selectedDeviceId: string) {
   const videoElem = this.shadowRoot?.getElementById('video');
   this.controls =  await codeReader.decodeFromVideoDevice(selectedDeviceId, videoElem, (result, error, controls)=>{
     if (result){
       console.log(result);
     }
     // TODO - handle exceptions
   });
}

How to we properly turn off the camera and stop listening in the decodeOnce version?

@odahcam
Copy link
Member

odahcam commented Dec 10, 2020

image

BRUH

There isn't a way for doing that!

Why?

Because I designed it for being stopable by Prosime cancellation, but then I discovered that in JS there isn't such API. (...)

Here's how it's written by now:

public scanOneResult(
element: HTMLVisualMediaElement,
retryIfNotFound = true,
retryIfChecksumError = true,
retryIfFormatError = true,
): Promise<Result> {
return new Promise((resolve, reject) => {
// reuses the scan API, but returns at the first successful result
this.scan(element, (result, error, controls) => {
if (result) {
// good result, returning
resolve(result);
controls.stop();
return;
}
if (error) {
// checks if it should retry
if (error instanceof NotFoundException && retryIfNotFound) { return; }
if (error instanceof ChecksumException && retryIfChecksumError) { return; }
if (error instanceof FormatException && retryIfFormatError) { return; }
// not re-trying
controls.stop(); // stops scan loop
reject(error); // returns the error
}
});
});
}

So... I would say you should use the continuously decoding APIs for having full control over tha scanning proccess.

@odahcam
Copy link
Member

odahcam commented Dec 10, 2020

Related to #17

@DarrenWainwright
Copy link
Author

@odahcam - Thanks. I've since removed all async/await items.

Stopped the camera dying, but now being killed by the TypeError.

I've also since moved over to the BrowserMultiFormatReader to read PDF417 and it seems less brittle - no idea why..

@odahcam
Copy link
Member

odahcam commented Dec 11, 2020

Yeah, that is a PDF417 Reader error, I think we should move this conversation to #23 then.

@odahcam odahcam closed this as completed Jan 27, 2021
@thisconnect
Copy link

Because I designed it for being stopable by Prosime cancellation, but then I discovered that in JS there isn't such API. (...)

Now there is the AbortController, but might not work everywhere

@odahcam
Copy link
Member

odahcam commented Jan 31, 2021

Nice, I think we can do similar implementation in here.

@thisconnect
Copy link

Althoug I never liked the AbortController, just codeReader.reset() would be nice if it is possible.

@odahcam
Copy link
Member

odahcam commented Feb 1, 2021

It was a bad design, hard to maintain and father of many bugs, it won't come back. Instead we will create a simpler and elegant solution for this. When I have some free time, of course, or some kind soul sends a PR.

@piechoo
Copy link

piechoo commented Sep 28, 2023

Could we reopen this issue ? It's still a problem and I assume it can be implemented via abortController :)

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

4 participants