eisberg-labs / ngx-barcode-scanner

Angular Barcode scanner using Quagga

Home Page:https://ngx-barcode-scanner.amarjanica.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add interface start stop functions (feature request)

FedericoAndreoli opened this issue · comments

Really good plug and play package.

It would be great to have some way to programmatically use the start and stop functions, so I can choose when to start and stop the scanning process (for example pressing a button or when a certain event occurs)

Thanks for the feature request. Currently ngx-barcode-scanner ties the service start onInit and stop is onDestroy. You can access the service by importing it directly:

import {NgxBarcodeScannerService} from "@eisberg-labs/ngx-barcode-scanner";

@Component({
  selector: 'app-root',
  template: `<ngx-barcode-scanner [(value)]="value"
                     [codes]="['code_128', 'ean', 'upc', 'upc_e', 'ean_8']" [errorThreshold]="0.1" (exception)="onError($event)"></ngx-barcode-scanner>`
})
export class AppComponent {
  value: string = '';
  isError = false;

  constructor(
    service: NgxBarcodeScannerService
  ) {
 // register on value change, call service.stop() ?
  }
}

Would this solution help?

That's perfect.

It would be great if you could update the documentation highlighting the presence of this service. I suggest a code snippet or something like this:

import {NgxBarcodeScannerService} from "@eisberg-labs/ngx-barcode-scanner";

//...

@Component({
  selector: 'app-root',
  template: `<ngx-barcode-scanner [(value)]="value"
                     [codes]="['code_128', 'ean', 'upc', 'upc_e', 'ean_8']" [errorThreshold]="0.1" (exception)="onError($event)"></ngx-barcode-scanner>`
    <div style="display: flex; justify-content: space-between; margin: 5% auto">
        <button (click)="onStartButtonPress()">Start</button>
        <button (click)="onStopButtonPress()">Stop</button>
    </div>
})

  constructor(
     service: NgxBarcodeScannerService
  ) {
    //Do constructor things...
  }

  onStartButtonPress() {
    this.service.start(this.quaggaConfig, 0.1)
  }

  onValueChanges(detectedValue: string) {
    console.log("Found this: " + detectedValue)
  }
  
  onStopButtonPress() {
    this.service.stop()
  }

Thank you, I will