File

src/lib/scandit-sdk.service.ts

Description

Scandit SDK Service handling interaction with the Scandit SDK library, including configuration, initialization and barcode picker creation

Index

Methods

Constructor

constructor(config: ScanditSdkServiceConfig)

Initialize the service and start loading the library based on the passed config options

Parameters :
Name Type Optional Description
config ScanditSdkServiceConfig No

Configuration options

Methods

Public createPicker
createPicker(element: HTMLElement, params?: any)

Create a new picker in a given HTML element

See https://docs.scandit.com/stable/web/classes/barcodepicker.html#create

Parameters :
Name Type Optional Description
element HTMLElement No

The HTML element that should contain the picker

params any Yes

The parameters used for creating the picker

Returns : Promise<BarcodePicker>

A promise resolving to the created BarcodePicker

Public onEngineLoadError
onEngineLoadError(handler: (error: Error) => void)

Add a handler in the event of an error while loading the engine

Parameters :
Name Type Optional Description
handler function No

Function to handle an error while loading the engine

Returns : void
import { Injectable } from "@angular/core";
import { BarcodePicker, configure, Scanner } from "scandit-sdk";

/**
 * Configuration object for library setup
 */
export class ScanditSdkServiceConfig {
  /**
   * The license key to be used for configuration.
   *
   * @see https://docs.scandit.com/stable/web/index.html#configuration
   */
  licenseKey: string;

  /**
   * Configuration options with the following signature:
   * {
   *    engineLocation: string = "/",
   *    preloadEngine?: boolean = true,
   *    preloadBlurryRecognition?: boolean = true
   * }
   *
   * `options.engineLocation`: The location of the Scandit Engine `scandit-engine-sdk.min.js` and
   * `scandit-engine-sdk.wasm` (external WebAssembly files). You must copy the relevant files
   * to a place where they can be served from, e.g. `assets`.
   *
   * `options.preloadEngine`: _optional_. Whether to preload (download if needed, compile/instantiate
   * WebAssembly code and initialize) the external Scandit Engine library, used by BarcodePicker and Scanner
   * objects to perform scan operations.
   *
   * `options.preloadBlurryRecognition`: _optional_. Whether to preload (load or generate if needed) data needed for
   * blurry recognition as soon as possible via a separate asynchronous WebWorker thread running the Scandit Engine
   * library. Data for all symbologies is generated over time.
   *
   * @see https://docs.scandit.com/stable/web/index.html#configuration
   */
  options: {
    engineLocation: string;
    preloadEngine?: boolean;
    preloadBlurryRecognition?: boolean;
  };
}

/**
 * Scandit SDK Service handling interaction with the Scandit SDK library, including configuration,
 * initialization and barcode picker creation
 */
@Injectable({
  providedIn: "root"
})
export class ScanditSdkService {
  /**
   * The handler to call in case of an engine load error
   */
  private engineLoadErrorHandler: (error: Error) => any;

  private configurationPromise: Promise<void | Scanner> = Promise.resolve();

  /**
   * Initialize the service and start loading the library based on the passed config options
   *
   * @param config Configuration options
   */
  constructor(private readonly config: ScanditSdkServiceConfig) {
    console.debug("Loading Scandit library...");
    this.loadLibrary(config.licenseKey, config.options);
  }

  /**
   * Create a new picker in a given HTML element
   *
   * See https://docs.scandit.com/stable/web/classes/barcodepicker.html#create
   *
   * @param element The HTML element that should contain the picker
   * @param params The parameters used for creating the picker
   * @returns A promise resolving to the created BarcodePicker
   */
  public createPicker(element: HTMLElement, params?: any): Promise<BarcodePicker> {
    if (params == null) {
      params = {};
    }

    return this.configurationPromise.then(() => {
      return BarcodePicker.create(element, params);
    });
  }

  /**
   * Add a handler in the event of an error while loading the engine
   *
   * @param handler Function to handle an error while loading the engine
   */
  public onEngineLoadError(handler: (error: Error) => any): void {
    this.engineLoadErrorHandler = handler;
  }

  /**
   * Configure the library and delegate error handling if needed.
   *
   * @param licenseKey The license key used for the library
   * @param options Configuration options
   * @param engineLocation The engine location
   * @param preloadEngine Whether to preload the external Scandit Engine library
   * @param preloadBlurryRecognition Whether to preload data needed for blurry recognition
   */
  private loadLibrary(
    licenseKey: string,
    options: { engineLocation: string, preloadEngine?: boolean, preloadBlurryRecognition?: boolean }
  ): void {
    this.configurationPromise = configure(licenseKey, options).catch(error => {
      if (this.engineLoadErrorHandler != null) {
        this.engineLoadErrorHandler(error);
      } else {
        alert(error);
      }
      throw error;
    });
  }
}

result-matching ""

    No results matching ""