Dev Center
Table of contents

JavaScript Hello World Sample - Next.js

Next.js is an open-source development framework built on top of Node.js enabling React based web applications functionalities such as server-side rendering and generating static websites. Follow this guide to learn how to implement Dynamsoft Barcode Reader JavaScript SDK (hereafter called “the library”) into a Next.js application. Note that in this sample we will use TypeScript.

Official Sample

Preparation

Make sure you have node installed. node 14.21.3 and next 13.2.4 are used in the example below.

Create the sample project

Create a Bootstrapped Raw Next.js Application

npx create-next-app@latest --typescript

CD to the root directory of the application and install the dependencies

npm install dynamsoft-javascript-barcode

Start to implement

Add a file dbr.ts at the root of the application to configure the library

import { BarcodeReader } from 'dynamsoft-javascript-barcode';
BarcodeReader.license = 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9';
BarcodeReader.engineResourcePath = "https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.40/dist/";

Note:

  • license specify a license key to use the library. You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=sample&product=dbr&package=js to get your own trial license good for 30 days.
  • engineResourcePath tells the library where to get the necessary resources at runtime.

Create a directory “components” and create the following files inside it to represent three components

  • VideoDecode.tsx
  • ImgDecode.tsx
  • HelloWorld.tsx

Edit the VideoDecode component

  • In VideoDecode.tsx, add code for initializing and destroying the BarcodeScanner instance. The VideoDecode component uses BarcodeScanner class of the library to help decode barcodes via camera.
import { BarcodeScanner } from "dynamsoft-javascript-barcode";
import React from 'react';
import styles from '@/styles/VideoDecode.module.css'

class VideoDecode extends React.Component {
    pScanner: Promise<BarcodeScanner>|null = null;
    elRef: React.RefObject<HTMLDivElement> = React.createRef();
    async componentDidMount() {
        try {
            const scanner = await (this.pScanner = BarcodeScanner.createInstance());
            // Should judge if scanner is destroyed after 'await', as in development React runs setup and cleanup one extra time before the actual setup in Strict Mode. 
            if(scanner.isContextDestroyed()) return;
            await scanner.setUIElement(this.elRef.current!);
            // Should judge if scanner is destroyed after 'await', as in development React runs setup and cleanup one extra time before the actual setup in Strict Mode. 
            if(scanner.isContextDestroyed()) return;
            scanner.onFrameRead = results => {
                for (let result of results) {
                    console.log(result.barcodeText);
                }
            };
            scanner.onUniqueRead = (txt, result) => {
                alert(txt);
            }
            await scanner.open();
        } catch (ex: any) {
            if (ex.message.indexOf("network connection error")) {
                let customMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
                console.log(customMsg);
                alert(customMsg);
            }
            throw ex;
        }
    }
    async componentWillUnmount() {
        if (this.pScanner) {
            (await this.pScanner).destroyContext();
            console.log('BarcodeScanner Component Unmount');
        }
    }
    shouldComponentUpdate() {
        // Never update UI after mount, dbrjs sdk use native way to bind event, update will remove it.
        return false;
    }
    render() {
        return (
            <div ref={this.elRef} className={styles["component-barcode-scanner"]}>
                <svg className={styles["dce-bg-loading"]+" "+"dce-bg-loading"} viewBox="0 0 1792 1792"><path d="M1760 896q0 176-68.5 336t-184 275.5-275.5 184-336 68.5-336-68.5-275.5-184-184-275.5-68.5-336q0-213 97-398.5t265-305.5 374-151v228q-221 45-366.5 221t-145.5 406q0 130 51 248.5t136.5 204 204 136.5 248.5 51 248.5-51 204-136.5 136.5-204 51-248.5q0-230-145.5-406t-366.5-221v-228q206 31 374 151t265 305.5 97 398.5z"></path></svg>
                <svg className={styles["dce-bg-camera"]+" "+"dce-bg-camera"} viewBox="0 0 2048 1792"><path d="M1024 672q119 0 203.5 84.5t84.5 203.5-84.5 203.5-203.5 84.5-203.5-84.5-84.5-203.5 84.5-203.5 203.5-84.5zm704-416q106 0 181 75t75 181v896q0 106-75 181t-181 75h-1408q-106 0-181-75t-75-181v-896q0-106 75-181t181-75h224l51-136q19-49 69.5-84.5t103.5-35.5h512q53 0 103.5 35.5t69.5 84.5l51 136h224zm-704 1152q185 0 316.5-131.5t131.5-316.5-131.5-316.5-316.5-131.5-316.5 131.5-131.5 316.5 131.5 316.5 316.5 131.5z"></path></svg>
                <div className={styles["dce-video-container"]+" "+"dce-video-container"}></div>
                <div className={styles["dce-scanarea"]+" "+"dce-scanarea"}>
                    <div className={styles["dce-scanlight"]+" "+"dce-scanlight"}></div>
                </div>
                <div className={styles["div-select-container"]+" "+"div-select-container"}>
                    <select className={styles["dce-sel-camera"]+" "+"dce-sel-camera"}></select>
                    <select className={styles["dce-sel-resolution"]+" "+"dce-sel-resolution"}></select>
                </div>
                <div className={styles["dbr-msg-poweredby"]}><svg viewBox="0 0 94 17"><g><path d="M0.9,14V4.3h2.3c0.6,0,1,0.1,1.4,0.3c0.3,0.2,0.6,0.5,0.7,0.9s0.2,0.8,0.2,1.4c0,0.5-0.1,0.9-0.2,1.3 C5.1,8.5,4.9,8.8,4.5,9.1C4.2,9.3,3.7,9.4,3.2,9.4H1.8V14H0.9z M1.8,8.7h1.2c0.4,0,0.7-0.1,1-0.2S4.3,8.2,4.4,8 c0.1-0.3,0.2-0.6,0.2-1.1c0-0.5,0-0.9-0.1-1.2C4.3,5.4,4.2,5.2,3.9,5.1S3.4,5,2.9,5H1.8V8.7z" /><path d="M8.1,14.1c-0.5,0-0.8-0.1-1.1-0.3s-0.5-0.5-0.6-0.9s-0.2-0.9-0.2-1.4V9.6c0-0.6,0.1-1,0.2-1.4 C6.5,7.8,6.7,7.5,7,7.3S7.6,7,8.1,7C8.6,7,9,7.1,9.2,7.3s0.5,0.5,0.6,0.9C9.9,8.5,9.9,9,9.9,9.6v1.9c0,0.6-0.1,1-0.2,1.4 c-0.1,0.4-0.3,0.7-0.6,0.9S8.6,14.1,8.1,14.1z M8.1,13.4c0.3,0,0.5-0.1,0.7-0.2C8.9,13,9,12.8,9,12.5c0-0.3,0-0.6,0-1v-2 c0-0.4,0-0.7,0-1C9,8.2,8.9,8,8.8,7.9C8.6,7.7,8.4,7.6,8.1,7.6c-0.3,0-0.5,0.1-0.7,0.2C7.3,8,7.2,8.2,7.2,8.5c0,0.3-0.1,0.6-0.1,1 v2c0,0.4,0,0.7,0.1,1c0,0.3,0.1,0.5,0.3,0.7C7.6,13.4,7.8,13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,0.3,0,0.6,0.1,0.7c0.1,0.1,0.2,0.2,0.5,0.2c0.1,0,0.2,0,0.2,0s0.2,0,0.3,0V14c-0.1,0-0.2,0-0.4,0.1 C92.9,14.1,92.8,14.1,92.7,14.1z" /></g></svg>
                </div>
            </div>
        );
    }
}

export default VideoDecode;

Note:

  • The html code in render() and the following code builds the UI for the library.

    await scanner.setUIElement(this.elRef.current!);
    
  • To release resources timely, the BarcodeScanner instance is destroyed with the component in the callback componentWillUnmount.
  • The component should never update (check the code for shouldComponentUpdate()) so that events bound to the UI stay valid.
  • Create ‘VideoDecode.module.css’ under ‘/styles/’ and define the style of the component in it
.component-barcode-scanner{width:100%;height:100%;/* min-width:640px; */min-height:480px;background:#eee;position:relative;resize:both;}
.dce-bg-loading{display:none;animation:1s linear infinite dce-rotate;width:40%;height:40%;position:absolute;margin:auto;left:0;top:0;right:0;bottom:0;fill:#aaa;}
.dce-bg-camera{display:none;width:40%;height:40%;position:absolute;margin:auto;left:0;top:0;right:0;bottom:0;fill:#aaa;}
.dce-video-container{position:absolute;left:0;top:0;width:100%;height:100%;}
.dce-scanarea{width:100%;height:100%;position:absolute;left:0;top:0;}
.dce-scanlight{display:none;width:100%;height:3%;position:absolute;animation:3s infinite dce-scanlight;border-radius:50%;box-shadow:0px 0px 2vw 1px #00e5ff;background:#fff;}
.div-select-container{position:absolute;left:0;top:0;}
.dce-sel-camera{display:block;}
.dce-sel-resolution{display:block;margin-top:5px;}
.dbr-msg-poweredby{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);}
.dbr-msg-poweredby svg {height:max(3vmin,17px);fill:#FFFFFF;}
@keyframes dce-rotate{from{transform:rotate(0turn);}to{transform:rotate(1turn);}}
@keyframes dce-scanlight{from{top:0;}to{top:97%;}}

Edit the ImgDecode component

  • In ImgDecode.tsx, add code for initializing and destroying the BarcodeReader instance. The ImgDecode component uses BarcodeReader class of the library to help decode barcodes in an image.
import React, { ChangeEvent, Component } from 'react'
import { BarcodeReader } from "dynamsoft-javascript-barcode";
import styles from '@/styles/ImgDecode.module.css'

export default class ImgDecode extends Component {
  pReader: Promise<BarcodeReader> = BarcodeReader.createInstance();

  decodeImg = async (e: ChangeEvent<HTMLInputElement>) => {
    try {
      const reader = await this.pReader;
      let results = await reader.decode(e.target.files![0]);
      for (let result of results) {
        alert(result.barcodeText);
      }
      if (!results.length) { alert('No barcode found'); }
    } catch (ex: any) {
      if (ex.message.indexOf("network connection error")) {
        let customMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
        console.log(customMsg);
        alert(customMsg);
      }
      throw ex;
    }
    e.target.value = '';
  }

  async componentWillUnmount() {
    if (this.pReader) {
      (await this.pReader).destroyContext();
      console.log('ImgDecode Component Unmount');
    }
  }

  render() {
    return (
      <div className={styles.ImgDecode}><input type="file" onChange={this.decodeImg} /></div>
    )
  }
}
  • Create ‘ImgDecode.module.css’ under ‘/styles/’ and define the style of the component in it
.ImgDecode {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid black
}

Edit the HelloWorld component

  • Add VideoDecode and ImgDecode components in HelloWorld.tsx
import React from 'react';
import VideoDecode from './VideoDecode';
import ImgDecode from './ImgDecode';
import "../dbr"; // import side effects. The license, engineResourcePath, so on.
import { BarcodeReader } from "dynamsoft-javascript-barcode";
import styles from '@/styles/HelloWorld.module.css'

interface isState {
    bShowScanner: boolean,
    bShowImgDecode: boolean
}

class HelloWorld extends React.Component<any, isState> {
    state = {
        bShowScanner: true,
        bShowImgDecode: false
    };
    
    async componentDidMount() {
        try {
            await BarcodeReader.loadWasm();
        } catch (ex: any) {
            if (ex.message.indexOf("network connection error")) {
                let customMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
                console.log(customMsg);
                alert(customMsg);
            }
            throw ex;
        }
    }

    showScanner = () => {
        this.setState({
            bShowScanner: true,
            bShowImgDecode: false
        });
    }

    showImgDecode = () => {
        this.setState({
            bShowScanner: false,
            bShowImgDecode: true
        });
    }

    render() {
        return (
            <div className={styles.helloWorld}>
                <h1 className={styles.header}>Hello World for Next.js</h1>
                <div>
                    <button className={styles.button} style= onClick={this.showScanner}>Video Decode</button>
                    <button className={styles.button} style= onClick={this.showImgDecode}>Image Decode</button>
                </div>
                <div className={styles.container}>
                    {this.state.bShowScanner ? (<VideoDecode></VideoDecode>) : ""}
                    {this.state.bShowImgDecode ? (<ImgDecode></ImgDecode>) : ""}
                </div>
            </div>
        );
    }
}
export default HelloWorld;
  • Create ‘HelloWorld.module.css’ under ‘/styles/’ and define the style of the component in it
.helloWorld {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: #455A64;
}

.header {
  font-size: 1.5em;
}

.button {
  font-size: 1.5rem;
  margin: 1.5vh 0;
  border: 1px solid black;
  background-color: white;
  color: black;
}

.container {
  margin: 2vmin auto;
  width: 100%;
}

Add the HelloWorld component to /pages/index.tsx

In index.tsx, import the HelloWorld component:

import HelloWorld from '../components/HelloWorld'

Change the <main> tag like this:

<main className={styles.main}>
  <div className={styles.UIElement}>
    <HelloWorld />
  </div>
</main>
  • Define the style of the “App” element in styles/Home.module.css
.UIElement {
  margin: 2vmin auto;
  text-align: center;
  font-size: medium;
  width: 80vw;
}
  • Try running the project.
npm run dev

If you have followed all the steps correctly, you should now have a functioning page that allows you to scan barcodes from a webcam or a built-in camera. Additionally, if you want to decode a local image, you can click the Image Decode button and select the image you want to decode. Any barcodes that are detected will be displayed in a dialog.

Project Setup

npm install

Compile and Hot-Reload for Development

npm run dev

Type-Check, Compile and Minify for Production

npm run build

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

    • Latest version(10.2.10)
    • Version 10.x
      • Version 10.0.21
      • Version 10.0.20
    • Version 9.x
      • Version 9.6.40
      • Version 9.6.33
      • Version 9.6.32
      • Version 9.6.31
      • Version 9.6.30
      • Version 9.6.21
      • Version 9.6.20
      • Version 9.6.11
      • Version 9.6.10
      • Version 9.6.2
      • Version 9.6.1
      • Version 9.6.0
      • Version 9.3.1
      • Version 9.3.0
      • Version 9.2.13
      • Version 9.2.12
      • Version 9.2.11
      • Version 9.0.2
      • Version 9.0.1
      • Version 9.0.0
    • Version 8.x
      • Version 8.8.7
      • Version 8.8.5
      • Version 8.8.3
      • Version 8.8.0
      • Version 8.6.3
      • Version 8.6.0
      • Version 8.4.0
      • Version 8.2.5
      • Version 8.2.3
      • Version 8.2.1
      • Version 8.2.0
      • Version 8.1.3
      • Version 8.1.2
      • Version 8.1.0
      • Version 8.0.0
    • Version 7.x
      • Version 7.6.0
      • Version 7.5.0
    Change +