serratus / quaggaJS

An advanced barcode-scanner written in JavaScript
https://serratus.github.io/quaggaJS/
MIT License
5.06k stars 978 forks source link

Please provide simple example #118

Closed csaleman closed 8 years ago

csaleman commented 8 years ago

First thank you very much, this library is impressive.

I spent many hours playing with this library and finally got it to work but could find the right way to start it and stop it. I'm initializing each time I need to read a barcode and onDectected is being called multiple times as result.

I found the only example capturing streaming media too difficult to read and not intuitive. Can you please provide another example explaining how to initialize , set event handlers, start and stop Quagga with the minimum amount of code?

update: I might be able to fix the problem that I'm facing with Quagga.offDetected(handler), but I haven't been able to make it work. Quagga.offProcessed(handler) did work for me.

update: Never mind here is example for dummies.

https://codetidy.com/8589/

camp0sfer commented 4 years ago

Can someone post some example?

ericblade commented 4 years ago

Here's a recently updated pretty basic React example

https://github.com/ericblade/quagga2-react-example

ausievich commented 4 years ago

Same problem. Can't understand offDetected(handler) method. Maybe just silly. But would be great to have a simple example.

ericblade commented 4 years ago

Same problem. Can't understand offDetected(handler) method. Maybe just silly. But would be great to have a simple example.

onDetected(handler) provides a callback that is called when a barcode is detected. offDetected(handler) removes the given handler from the list of callbacks. It's basically addEventListener() and removeEventListener() for Quagga instead of for the document. It's useful if you're using LiveStream to detect, mostly.

ausievich commented 4 years ago

Thanks, Eric! I actually made it work yesterday. My code looks like this:

onDetected(function hello(code){ offDetected(hello); //Would you like to add "code" to the list? //Accepting user choice and turning it on again: onDetected(hello); });

It looks a bit strange to me, because of stopping and calling handler "hello" function inside itself, but its behavior is proper. Thanks again for the answer.

P.S. Yes, I'm using LiveStream.

ericblade commented 4 years ago

That seems pretty reasonable, assuming you're getting some sort of asynchronous response from the user then turning it back on. In the app I have, I don't expect the user to repeatedly scan items, so I call .stop after each successful scan.

For your purposes, you might also want to make sure that the detected code isn't in your list of codes that you've scanned, or wasn't the most recent one, in case the user doesn't move the barcode out of view before adding it to the list.