Build a Web-Based Barcode Scanner Using Just a Few Lines of JavaScript
+ +-
+
- Build a Web-Based Barcode Scanner Using Just a Few Lines of JavaScript +
- License +
- Quick Start: Hello World Example +
- Next Steps +
This user guide provides a step-by-step walkthrough of a "Hello World" web application using the BarcodeScanner JavaScript Edition.
The BarcodeScanner class offers the following features:
-
@@ -249,30 +259,25 @@
Please refer to system requirements for more details.
License
-
When getting started with Barcode Scanner, we recommend getting your own 30-day trial license
- ---[!IMPORTANT]
-
- The trial license can be renewed via the customer portal twice, each time for another 15 days, giving you a total of 60 days to develop your own application using the solution. Please contact the Dynamsoft Support Team if you need more time for a full evaluation.
Full License
-If you are fully satisfied with the solution and would like to move forward with a full license, please contact the Dynamsoft Sales Team.
+Quick Start: Hello World Example
<!DOCTYPE html>
-<html lang="en">
+<html lang="en">
<body>
- <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
<script>
// Initialize the Dynamsoft Barcode Scanner
const barcodeScanner = new Dynamsoft.BarcodeScanner({
- // Please don't forget to replace YOUR_LICENSE_KEY_HERE
- license: "YOUR_LICENSE_KEY_HERE",
+ // Please don't forget to replace YOUR_LICENSE_KEY_HERE
+ license: "YOUR_LICENSE_KEY_HERE",
});
- (async () => {
+ (async () => {
// Launch the scanner and wait for the result
const result = await barcodeScanner.launch();
- // Display the first detected barcode's text in an alert
+ // Display the first detected barcode's text in an alert
if (result.barcodeResults.length) {
alert(result.barcodeResults[0].text);
}
@@ -282,7 +287,7 @@ Quick Start: Hello World Example
</html>
-
+
@@ -290,21 +295,21 @@
Quick Start: Hello World Example
Step 1: Setting up the HTML and Including the Barcode Scanner
-As outlined earlier, this guide will help you create a simple Hello World barcode scanning application using vanilla JavaScript. The full sample code is also available in the GitHub repository.
+As outlined earlier, this guide will help you create a simple Hello World barcode scanning application using vanilla JavaScript. The full sample code is also available in the GitHub repository.
The first step before writing the code is to include the SDK in your application. You can simply include the SDK by using the precompiled script.
<!DOCTYPE html>
-<html lang="en">
+<html lang="en">
<body>
- <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
</body>
</html>
-In this example, we include the precompiled Barcode Scanner SDK script via public CDN in the header.
+In this example, we include the precompiled Barcode Scanner SDK script via public CDN in the body.
Step 1: Se
- jsDelivr
- <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
- UNPKG
- <script src="https://unpkg.com/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://unpkg.com/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
When using a framework such as React, Vue or Angular, we recommend adding the package as a dependency using a package manager such as npm or yarn:
- npm i dynamsoft-barcode-reader-bundle@11.0.6000
+ npm i dynamsoft-barcode-reader-bundle@11.2.2000
# or
- yarn add dynamsoft-barcode-reader-bundle@11.0.6000
+ yarn add dynamsoft-barcode-reader-bundle@11.2.2000
-As for package managers like npm or yarn, you likely need to specify the location of the engine files as a link to a CDN. Please see the BarcodeScannerConfig API for a code snippet on how to set the engineResourcePaths.
+When using package managers like npm or yarn, you likely need to specify the location of the engine files as a link to a CDN. Please see the BarcodeScannerConfig API for a code snippet on how to set the engineResourcePaths.
Host the SDK yourself
Alternatively, you may choose to download the SDK and host the files on your own server or preferred CDN. This approach provides better control over versioning and availability.
From the website
-Download Dynamsoft Barcode Reader JavaScript Package
+Download Dynamsoft Barcode Reader JavaScript Package
The resources are located in the ./dist/ directory.
From npm
- npm i dynamsoft-barcode-reader-bundle@11.0.6000
+ npm i dynamsoft-barcode-reader-bundle@11.2.2000
The resources are located at the path node_modules/<pkg>, without @<version>. You can copy it elsewhere and add @<version> tag.
[!IMPORTANT]
Since "nodemodules" is reserved for Node.js dependencies, and in our case the package is used only as static resources, we recommend either renaming the "nodemodules" folder or moving the "dynamsoft-" packages to a dedicated folder for static resources in your project to facilitate self-hosting.
-You can typically include SDK like this:
-<script src="path/to/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+You can typically include the SDK like this:
+<script src="path/to/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
Barcode Scanner comes with a Ready-to-Use UI. When the Barcode Scanner launches, it creates a container which it populates with the Ready-to-Use UI.
Step 2: Initializing the Barcode Scanner
// Initialize the Dynamsoft Barcode Scanner
-const barcodescanner = new Dynamsoft.BarcodeScanner({
- // Please don't forget to replace YOUR_LICENSE_KEY_HERE
- license: "YOUR_LICENSE_KEY_HERE",
+const barcodeScanner = new Dynamsoft.BarcodeScanner({
+ // Please don't forget to replace YOUR_LICENSE_KEY_HERE
+ license: "YOUR_LICENSE_KEY_HERE",
});
This is the simplest way to initialize the Barcode Scanner. The configuration object must include a valid license key. Without it, the scanner will fail to launch and display an error. For help obtaining a license, see the licensing section.
@@ -361,16 +366,16 @@ Step 2: Initializing the Barcod
By default, the BarcodeScanner scans a single barcode at a time. However, it also supports a MULTI_UNIQUE scanning mode, which continuously scans and accumulates unique results in real time. You can enable this mode by modifying the BarcodeScannerConfig as follows:
// Initialize the Dynamsoft Barcode Scanner in MULTI_UNIQUE mode
-const barcodescanner = new Dynamsoft.BarcodeScanner({
- license: "YOUR_LICENSE_KEY_HERE",
+const barcodeScanner = new Dynamsoft.BarcodeScanner({
+ license: "YOUR_LICENSE_KEY_HERE",
scanMode: Dynamsoft.EnumScanMode.SM_MULTI_UNIQUE,
});
Step 3: Launching the Barcode Scanner
(async () => {
// Launch the scanner and wait for the result
- const result = await barcodescanner.launch();
- // Display the first detected barcode's text in an alert
+ const result = await barcodeScanner.launch();
+ // Display the first detected barcode's text in an alert
if (result.barcodeResults.length) {
alert(result.barcodeResults[0].text);
}
@@ -379,14 +384,16 @@ Step 3: Launching the Barcode Scan
Now that the Barcode Scanner has been initialized and configured, it is ready to be launched! Upon launch, the Barcode Scanner presents the main BarcodeScannerView UI in its container on the page, and is ready to start scanning. By default, we use the SINGLE scanning mode, which means only one decoding result will be included in the final result. In the code above, we directly alerted the successfully decoded barcode text on the page.
[!NOTE]
- In the Hello World sample, after a successfully decoding process, the scanner closes and the user is met with an empty page. In order to open the scanner again, the user must refresh the page. You may choose to implement a more user-friendly behavior in a production environment, such as presenting the user with an option to re-open the Barcode Scanner upon closing it.
+ In the Hello World sample, after a successful decoding process, the scanner closes and the user is met with an empty page. In order to open the scanner again, the user must refresh the page. You may choose to implement a more user-friendly behavior in a production environment, such as presenting the user with an option to re-open the Barcode Scanner upon closing it.
+ [!TIP]
+ When running performance benchmarks, make sure to disable the console, avoid using console.log(), and run in non-debug mode whenever possible. This ensures that your benchmark results reflect true performance without interference from logging or debugging overhead.
+ Next Steps
-Next Steps
-Now that you've implemented the basic functionality, here are some recommended next steps to further explore the capabilities of the Barcode Scanner
+Now that you've implemented the basic functionality, here are some recommended next steps to further explore the capabilities of the Barcode Scanner:
- Learn how to Customize the Barcode Scanner
-- Check out the Official Samples and Demo
-- Learn about the APIs of BarcodeScanner
+- Check out the Official Samples and Demo
+- Learn about the APIs of BarcodeScanner
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
- UNPKG
- <script src="https://unpkg.com/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+ <script src="https://unpkg.com/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
When using a framework such as React, Vue or Angular, we recommend adding the package as a dependency using a package manager such as npm or yarn:
- npm i dynamsoft-barcode-reader-bundle@11.0.6000
+ npm i dynamsoft-barcode-reader-bundle@11.2.2000
# or
- yarn add dynamsoft-barcode-reader-bundle@11.0.6000
+ yarn add dynamsoft-barcode-reader-bundle@11.2.2000
-As for package managers like npm or yarn, you likely need to specify the location of the engine files as a link to a CDN. Please see the BarcodeScannerConfig API for a code snippet on how to set the engineResourcePaths.
+When using package managers like npm or yarn, you likely need to specify the location of the engine files as a link to a CDN. Please see the BarcodeScannerConfig API for a code snippet on how to set the engineResourcePaths.
Host the SDK yourself
Alternatively, you may choose to download the SDK and host the files on your own server or preferred CDN. This approach provides better control over versioning and availability.
From the website
-Download Dynamsoft Barcode Reader JavaScript Package
+Download Dynamsoft Barcode Reader JavaScript Package
The resources are located in the ./dist/ directory.
From npm
- npm i dynamsoft-barcode-reader-bundle@11.0.6000
+ npm i dynamsoft-barcode-reader-bundle@11.2.2000
The resources are located at the path node_modules/<pkg>, without @<version>. You can copy it elsewhere and add @<version> tag.
[!IMPORTANT]
Since "nodemodules" is reserved for Node.js dependencies, and in our case the package is used only as static resources, we recommend either renaming the "nodemodules" folder or moving the "dynamsoft-" packages to a dedicated folder for static resources in your project to facilitate self-hosting.
-You can typically include SDK like this:
-<script src="path/to/dynamsoft-barcode-reader-bundle@11.0.6000/dist/dbr.bundle.js"></script>
+You can typically include the SDK like this:
+<script src="path/to/dynamsoft-barcode-reader-bundle@11.2.2000/dist/dbr.bundle.js"></script>
Barcode Scanner comes with a Ready-to-Use UI. When the Barcode Scanner launches, it creates a container which it populates with the Ready-to-Use UI.
Step 2: Initializing the Barcode Scanner
// Initialize the Dynamsoft Barcode Scanner
-const barcodescanner = new Dynamsoft.BarcodeScanner({
- // Please don't forget to replace YOUR_LICENSE_KEY_HERE
- license: "YOUR_LICENSE_KEY_HERE",
+const barcodeScanner = new Dynamsoft.BarcodeScanner({
+ // Please don't forget to replace YOUR_LICENSE_KEY_HERE
+ license: "YOUR_LICENSE_KEY_HERE",
});
This is the simplest way to initialize the Barcode Scanner. The configuration object must include a valid license key. Without it, the scanner will fail to launch and display an error. For help obtaining a license, see the licensing section.
@@ -361,16 +366,16 @@ Step 2: Initializing the Barcod
By default, the BarcodeScanner scans a single barcode at a time. However, it also supports a MULTI_UNIQUE scanning mode, which continuously scans and accumulates unique results in real time. You can enable this mode by modifying the BarcodeScannerConfig as follows:
// Initialize the Dynamsoft Barcode Scanner in MULTI_UNIQUE mode
-const barcodescanner = new Dynamsoft.BarcodeScanner({
- license: "YOUR_LICENSE_KEY_HERE",
+const barcodeScanner = new Dynamsoft.BarcodeScanner({
+ license: "YOUR_LICENSE_KEY_HERE",
scanMode: Dynamsoft.EnumScanMode.SM_MULTI_UNIQUE,
});
Step 3: Launching the Barcode Scanner
(async () => {
// Launch the scanner and wait for the result
- const result = await barcodescanner.launch();
- // Display the first detected barcode's text in an alert
+ const result = await barcodeScanner.launch();
+ // Display the first detected barcode's text in an alert
if (result.barcodeResults.length) {
alert(result.barcodeResults[0].text);
}
@@ -379,14 +384,16 @@ Step 3: Launching the Barcode Scan
Now that the Barcode Scanner has been initialized and configured, it is ready to be launched! Upon launch, the Barcode Scanner presents the main BarcodeScannerView UI in its container on the page, and is ready to start scanning. By default, we use the SINGLE scanning mode, which means only one decoding result will be included in the final result. In the code above, we directly alerted the successfully decoded barcode text on the page.
[!NOTE]
- In the Hello World sample, after a successfully decoding process, the scanner closes and the user is met with an empty page. In order to open the scanner again, the user must refresh the page. You may choose to implement a more user-friendly behavior in a production environment, such as presenting the user with an option to re-open the Barcode Scanner upon closing it.
+ In the Hello World sample, after a successful decoding process, the scanner closes and the user is met with an empty page. In order to open the scanner again, the user must refresh the page. You may choose to implement a more user-friendly behavior in a production environment, such as presenting the user with an option to re-open the Barcode Scanner upon closing it.
+ [!TIP]
+ When running performance benchmarks, make sure to disable the console, avoid using console.log(), and run in non-debug mode whenever possible. This ensures that your benchmark results reflect true performance without interference from logging or debugging overhead.
+ Next Steps
-Next Steps
-Now that you've implemented the basic functionality, here are some recommended next steps to further explore the capabilities of the Barcode Scanner
+Now that you've implemented the basic functionality, here are some recommended next steps to further explore the capabilities of the Barcode Scanner:
- Learn how to Customize the Barcode Scanner
-- Check out the Official Samples and Demo
-- Learn about the APIs of BarcodeScanner
+- Check out the Official Samples and Demo
+- Learn about the APIs of BarcodeScanner