SDK Documentation Help

Getting Started

A step-by-step guide on how to integrate the STRICH SDK into your app and get started with barcode scanning.

Obtaining the Library

Installing from NPM

The STRICH SDK is published as an NPM package. It can be added to your NPM project like any other package:

npm install @pixelverse/strichjs-sdk

By default, npm will add a version requirement of ^x.y.z, which will automatically update the dependency to the latest minor version every time you run npm install.

Loading STRICH from a CDN

Alternatively, you can load STRICH from a CDN like jsDeliver as an ES6 module using an import statement:

import {StrichSDK, BarcodeReader} from "https://cdn.jsdelivr.net/npm/@pixelverse/strichjs-sdk@latest";

Loading With a <script> Tag

If you can not use ES6 modules and would like to load STRICH with a <script> tag, we also provide a non-modular build flavor.

Creating a license key

To initialize the SDK, you need to obtain a license key first. License keys can be created in the Customer Portal after creating an account and starting the free 30-day trial.

License keys are restricted to one or more application URLs (the scope of the license). So if your scanning app will be available at https://app.example.com and you have staging and development environments, you need to add those URLs to the license key when creating it. You can also create separate license keys if you already have a mechanism for injecting per-environment configurations.

Creating a License Key

Please note that HTTPS is required. Web browsers only allow access to the camera feed for secure origins. For more information on how to serve from a secure origin during development, please refer to the Deployment Guide.

Initializing the SDK

The SDK needs to be initialized once with the license key created previously by calling StrichSDK.initialize(). When the Promise resolves successfully, the SDK is ready to use.

StrichSDK.initialize('eyJhbGciOiJIUzI1NiIsInR...') .then(() => console.log('STRICH SDK initialized'));

We recommend doing SDK initialization early, before the user expects to see the barcode scanning screen, for instance during the rest of your app initialization. SDK initialization is asynchronous and requires internet connectivity, except for Enterprise licenses which support offline operation.

Defining the Host Element

We need to provide the BarcodeReader with a place to live. We do so by defining a host element – a DOM element that will contain the UI, i.e. the camera feed, view finder, camera controls, etc.

Example: Vertical Layout with BarcodeReader on Top

A typical scanning app has a vertical layout with the scanner on top, results below and an action bar at the bottom.

<div style="display: flex; flex-direction: column"> <div id="scanner" style="position: relative; height: 240px"> <!-- BarcodeReader will be instantiated here - position:relative is required! --> </div> <div class="results"> <!-- Display scanned results --> </div> <div class="actions"> <!-- Action buttons, e.g. Save, Send, Cancel, etc. --> </div> </div>

Here, the host element is a div with ID scanner, so we can refer to it via the CSS selector #scanner.

Creating a Configuration

Before we can create the BarcodeReader, we need to create a configuration – an object that specifies the functional and visual characteristics of the BarcodeReader.

For a good scanning experience, it is important to restrict the types of barcodes to be detected by the BarcodeReader to only the ones you need. STRICH will automatically set the region of interest (the area where barcodes are detected) to something appropriate for selected barcode types.

Example: Configuration for Code 128 Barcodes

In the example below, we only look for Code 128 barcodes. We also set a duplicateInterval of 2.5 seconds, avoiding repeated scans of the same code over a short time frame.

let config = { // the CSS selector identifying the host element selector: '#scanner', engine: { // restrict to the required symbologies symbologies: ['code128'], // filter duplicate results duplicateInterval: 2500 } };

Initializing the BarcodeReader

Initializing the BarcodeReader is a two-step process: the configuration is passed to the constructor, then the initialize() method is called, which will request access to the camera.

To receive barcode detections in your app code, provide a callback using the detected property. The BarcodeReader will invoke this callback whenever barcodes are detected.

Example: Initializing a BarcodeReader Instance

new BarcodeReader(config).initialize() .then(result => { // initialization successful, store BarcodeReader for later this.barcodeReader = result; // register handler for code detections this.barcodeReader.detected = (detections) => { // .. do something with the scanned codes }; // start reading barcodes! return this.barcodeReader.start(); }) .catch(error => { // initialization failed (error message displayed in BarcodeReader) });

Camera Access

Initialization requires camera access, which is gated through a browser prompt. It is usually a good idea to make the user aware of this prior to initializing the BarcodeReader, for example by displaying a camera access will be required in the next step notice.

The SDK provides utility functions for determining the current camera permission state and whether the device actually has a camera.

Testing on a Smartphone

STRICH is optimized for barcode scanning in web apps running on smartphones. It is very important to always test your app on actual smartphones.

During development, you are probably running your app locally on your desktop or laptop. When accessing your app from a smartphone on the same network, you might have to do some extra work to set up SSL, as camera access only works if the app is hosted via HTTPS or is accessed via localhost, a secure context.

Please refer to the Deployment Guide for more information about how to expose your app via HTTPS during development.

Destroying the BarcodeReader

The BarcodeReader you created earlier needs to be stored somewhere, as you need to destroy it when it is no longer needed, or pause/resume scanning between user interactions. It is also possible to temporarily hide and then show the scanner using setVisible, without having to re-acquire camera access.

Next Steps

You are now ready to scan barcodes in your web app. For further advice, check out these additional resources.

Last modified: 08 October 2024