Library: Can't use the torch to scan (torch from mobile top bar)

Created on 18 Jan 2019  ·  14Comments  ·  Source: zxing-js/library

If I want to turn on the torch I get "can't open because the camera is using it"
Any idea how to solve this? maybe some config
Thanks in advance

question

Most helpful comment

I've managed to find a way to turn the torch on/off without touching the library code. The trick is that we can get the stream through the

All 14 comments

Yes, tlfr: implement your own BrowserCodeReader.

We already implemented torch control, but not here, unfortunately. You can find some code and examples on our @zxing-js/ngx-scanner repository, we implemented a custom BrowserCodeReader there that can control the device torch.

I wish I could implement it for you in a short period of time, but I can't 🙁, so all I can give are instructions and some help.

Are you interested in implementing a custom BrowserCodeReader or even take our code from the other repo and migrate it to here and making a PR?

Hi,
I don't want torch control. I asked why I can't use the mobile flashlight while using your js scan.
But It was a Samsung tab problem. Works fine on Xiaomi.
Best regards

Well, in that case I can't explain better than the error message, hehe. Feeel free to ask any questions.

Hi @odahcam , could you give some instructions on how to implement my own torch with a multi format reader? I am using the library.

You can copy the functionality from our Angular component, it's basically the same BrowserCodeReader but with two methods that makes use of RxJS. Are you interested?

Yes i am interested! First i will need to implement the MultiFormatReader.

I saw the Angular scanner, and it looks straight forward.. do you think that it's possible without RxJS? Kind of a performance budget here...
The idea is to add advanced: { torch: true } to constraints to enable it right?

I also tested the ngx-scanner on https://zxing-ngx-scanner.stackblitz.io/, and i can't access the torch in any way? There is no frontend implementation yet in the app?

There's no UI implementation there, just in the example app which is not running at the moment, but you can clone and ng serve the project, it will work.

You won't need yourown Multi Code Reader because there's already a implementation here compatible with any readers.

About the code, I think that this is what you need:

https://github.com/zxing-js/ngx-scanner/blob/742574321b3d284557d1f0bcc00dc08a87904432/src/app/modules/zxing-scanner/browser-code-reader.ts#L215-L230

It is possible to do that without RxJS, just use a boolean or add a callback after setting torchCompatible value and you will be fine.

I've managed to find a way to turn the torch on/off without touching the library code. The trick is that we can get the stream through the

@bezo97 Very nice, exactly what I needed as a workaround for now.

The only problem I got is that it's not working on the iPhone. I guess this is a webkit/safari bug...

@bezo97 Where to put this code? this is relative to which element?

I'm using:

        .decodeFromInputVideoDevice(undefined, 'video')
        .then((result) => { console.log(result.text); });

setTimeout(() => { document.getElementById('video')
    .srcObject.getVideoTracks()[0].applyConstraints({ advanced: [{ torch: true }] }); }, 2000);

and it works and starts the torch 2 seconds later, but how to start it just after the stream has started?

@josephernest Forget the this, the videoElement in my snippet is the object you get by calling getElementById.

Also I don't have a solution for detecting the stream start, I just used a button to turn it on/off. I suggest you open a separate issue for this.

Thank you for your answer, it works indeed.

I didn't detect the stream start either, I used setTimeout(..., 2000) and
it's OK.

Something else @bezo97: did you find how to force a camera focus /
autofocus with a similar method than the torch ?

This would be super useful as often an unsuccessful detection just comes
from the lack of camera focus.

On Thursday, 13 February 2020, Zoltán Dócs notifications@github.com wrote:

@josephernest https://github.com/josephernest Forget the this, the
videoElement in my snippet is the object you get by calling getElementById
.

Also I don't have a solution for detecting the stream start, I just used a
button to turn it on/off. I suggest you open a separate issue for this.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/zxing-js/library/issues/128?email_source=notifications&email_token=ABPB4E2Q36NGI53YRDSVZUDRCSBYZA5CNFSM4GRAFZ52YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELSZXTI#issuecomment-585472973,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABPB4E3KJMT33FG7EZW4S3TRCSBYZANCNFSM4GRAFZ5Q
.

did you find how to force a camera focus / autofocus with a similar method than the torch ?

That would be awesome. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

majestic84 picture majestic84  ·  8Comments

sabasayer picture sabasayer  ·  3Comments

kenlyon picture kenlyon  ·  6Comments

lenny76 picture lenny76  ·  10Comments

boruchsiper picture boruchsiper  ·  7Comments