cozmo / jsQR

A pure javascript QR code reading library. This library takes in raw images and will locate, extract and parse any QR code found within.
https://cozmo.github.io/jsQR/
Apache License 2.0
3.72k stars 607 forks source link

How to toggle camera on and off conditionally? #87

Closed hoangnguyen208 closed 6 years ago

hoangnguyen208 commented 6 years ago

Hi, if I don't want the camera always shows, how can I toggle it off? Specifically, in my case, I have a form and decoding QR code is one of result I have to submit. QR code is the last field in the form I have to submit, so I want user to focus to fill all of the fields then toggle on the camera to get QR code before submitting the form. How can I achieve this feature?

chidindu-ogbonna commented 6 years ago

What you can do is open the web cam using a button and an event listener such as "click"

And you could delay creating the JSQR instance until the event is fired.

Or you could add an event listener to the last field such as "click or input" so the webcam would be open then.

cozmo commented 6 years ago

So this would fall under our note on webcams - Specifically, jsQR is not designed specifically to work with a specific webcam setup, rather it's designed to work independently so that you can adapt to whatever your situation is.

That said, generally it comes down to when you read data from the webcam and send it to jsQR. For example in our demo we immediately ask for camera permissions and start capturing frames with the tick function. it seems like in your case you'd want to do this once the user gets to that part of the form? This is what @chidonna is describing I believe.

If you have more questions about this I'd check out the resources mentioned in the note on webcams. I'm going to close this issue but if you believe that to be in error or have more questions feel free to re-open.