undecaf / barcode-detector-polyfill

A WebAssembly polyfill for the Barcode Detection API
MIT License
118 stars 14 forks source link

Scanner goes infinitely / doesn't stop after reading a barcode #10

Closed abdulwahabalm closed 1 year ago

abdulwahabalm commented 1 year ago

The scanner is really good and is of high quality but it doesn't stop even after it detects a barcode.

can there be a way for the scanner to stop after scanning a barcode ?

undecaf commented 1 year ago

Could you please provide a minimal working example that demonstrates this behavior?

I am asking this because the polyfill does not behave like that in the unit tests and in the examples.

abdulwahabalm commented 1 year ago

I attempted to write a minimal working example of you code and it didn't really work

but my attempt was that

function detect(source){ return detector .detect(source) .then(symbols => {

/ rest of code /

detectVideo(false) // my assumption is it would have worked the same way the camera disappears when you trigger detectImg

})

undecaf commented 1 year ago

I assume that you are referring to the code in barcode-detector-polyfill/example-bundled/main.js

In order to stop scanning as soon as a symbol has been detected, I would suggest to do something like this:

function detect(source) {
    return detector
        .detect(source)
        .then(symbols => {
           // ...
           // Add this after the code block:
           if (symbols.length > 0) {
               detectVideo(false)
           }
        })
}

To start scanning the video that is playing in the <video> element, call detectVideo() without arguments (as in the videoBtn click listener) . This will clear the previous bounding box.

abdulwahabalm commented 1 year ago

Thank you! it worked