Skip to content

Commit 3e1183c

Browse files
feat: update electron sample and docs (#186)
* feat: update electron sample and docs * fix: reversed globally defined vars --------- Co-authored-by: felixindrawan <indrawan.felix123@gmail.com>
1 parent 841727c commit 3e1183c

File tree

5 files changed

+141
-117
lines changed

5 files changed

+141
-117
lines changed

hello-world/electron/README.md

Lines changed: 96 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Hello World Sample for Electron
22

3-
[Electron](https://www.electronjs.org/) is a framework for creating native applications with web technologies. Follow this guide to learn how to implement Dynamsoft Barcode Reader JavaScript SDK (hereafter called "the library") into an Electron application.
3+
[Electron](https://www.electronjs.org/) is a framework for creating native applications with web technologies. Follow this guide to learn how to implement [Dynamsoft Barcode Reader JavaScript SDK](https://www.dynamsoft.com/barcode-reader/sdk-javascript/) (hereafter called "the library") into a Next.js application. Note that in this sample, `TypeScript` is used.
4+
5+
In this guide, we will be using [`dynamsoft-barcode-reader-bundle 10.2.1000`](https://www.npmjs.com/package/dynamsoft-barcode-reader-bundle/v/10.2.1000).
6+
7+
> Note:
8+
>
9+
> If you’re looking to integrate DBR-JS into a framework that we don't yet have a sample, don't worry! We have a [comprehensive guide](https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/use-in-framework.html) that provides detailed instruction and best practices for a seamless integration into any frameworks!
10+
>
11+
> Additionally, we're here to help! Please don't hesitate to [contact us](#Support) for any support or questions you might have.
412
513
## Official Sample
614

@@ -10,7 +18,23 @@
1018

1119
Make sure you have [node](https://nodejs.org/) installed. `node 16.20.1` and `electron 26.4.1` are used in this article.
1220

13-
## Initialize project
21+
## Quick Start
22+
23+
```cmd
24+
npm install
25+
npm start
26+
```
27+
A window should open to view the sample application
28+
29+
## Creating the sample project
30+
31+
In this section, we will be creating an Electron application utilizing the Dynamsoft Barcode Reader bundle sdk.
32+
33+
We'll be exploring how you could create a page that not only enables barcode scanning via a webcam or a built-in camera, but also decode barcodes from local images.
34+
35+
By the end of this guide, you'll have a good understanding of the SDK and be ready to discover more ways to use it!
36+
37+
### Initialize project
1438

1539
```cmd
1640
mkdir my-app && cd my-app
@@ -19,27 +43,25 @@ npm init
1943

2044
`npm init` will prompt you to configure some fields in your `package.json`. Note that the `entry point` should be `main.js` (it will be created later).
2145

22-
## install necessary libraries
46+
### Install the necessary libraries
2347

2448
```cmd
2549
npm install electron --save-dev
26-
npm install dynamsoft-capture-vision-std
27-
npm install dynamsoft-image-processing
28-
npm install dynamsoft-core
29-
npm install dynamsoft-license
30-
npm install dynamsoft-utility
31-
npm install dynamsoft-barcode-reader
32-
npm install dynamsoft-capture-vision-router
33-
npm install dynamsoft-camera-enhancer
50+
npm install dynamsoft-capture-vision-std -E
51+
npm install dynamsoft-image-processing -E
52+
npm install dynamsoft-barcode-reader-bundle -E
3453
```
3554

3655
## Start to implement
3756

3857
### Create a main.js file
3958

40-
As defined in the `package.json` file, `main.js` is the entry point of the application, we define it like this:
59+
As defined in the `package.json` file, `main.js` is the entry point of the application.
60+
61+
Create a `main.js` file at the root folder, and define it like this:
4162

4263
```javascript
64+
/* /main.js */
4365
const { app, BrowserWindow } = require("electron");
4466

4567
function createWindow() {
@@ -80,47 +102,60 @@ The code basically opens `index.html` in a window. For more information, check o
80102

81103
### Create an `index.html` file
82104

83-
Create the page to be loaded in the created window.
105+
As defined above, `index.html` is the file that will be loaded into the crated window.
106+
107+
Create an `index.html` file at the root folder, and define it like this:
84108

85109
```html
110+
<!-- /index.html -->
86111
<!DOCTYPE html>
87112
<html>
88113
<head>
89114
<meta charset="UTF-8" />
90-
<meta name="description" content="Read barcodes from camera with Dynamsoft Barcode Reader in an Electron Application.">
91-
<meta name="keywords" content="barcode, camera, Electron">
115+
<meta
116+
name="description"
117+
content="Read barcodes from camera with Dynamsoft Barcode Reader in an Electron Application."
118+
/>
119+
<meta name="keywords" content="barcode, camera, Electron" />
92120
<title>Dynamsoft Barcode Reader Sample - Electron</title>
93-
<link href="style.css" rel="stylesheet">
94-
<script src="./node_modules/dynamsoft-core/dist/core.js"></script>
95-
<script src="./node_modules/dynamsoft-license/dist/license.js"></script>
96-
<script src="./node_modules/dynamsoft-utility/dist/utility.js"></script>
97-
<script src="./node_modules/dynamsoft-barcode-reader/dist/dbr.js"></script>
98-
<script src="./node_modules/dynamsoft-capture-vision-router/dist/cvr.js"></script>
99-
<script src="./node_modules/dynamsoft-camera-enhancer/dist/dce.js"></script>
121+
<link href="style.css" rel="stylesheet" />
122+
<script src="./node_modules/dynamsoft-barcode-reader-bundle/dist/dbr.bundle.js"></script>
100123
</head>
101124
<body>
102125
<h1>Hello World for Electron</h1>
103-
<div id="div-ui-container"></div>
126+
<div id="camera-view-container"></div>
127+
<br />
104128
Results:
105-
<br>
106-
<div id="div-results-container"></div>
129+
<div id="results"></div>
107130
<script src="action.js"></script>
108131
</body>
109132
</html>
110133
```
111134

112135
### Create an `action.js` file
113136

114-
`index.html` will loads `action.js`, which makes use of libraries to read barcodes from a video input:
137+
`index.html` will load `action.js`, which makes use of libraries to read barcodes from a video input.
138+
139+
Create the `action.js` file at the root folder, and define it like this:
115140

116141
```javascript
142+
/* /action.js */
143+
// Configures the paths where the .wasm files and other necessary resources for modules are located.
144+
Dynamsoft.Core.CoreModule.engineResourcePaths = {
145+
std: "./node_modules/dynamsoft-capture-vision-std/dist/",
146+
dip: "./node_modules/dynamsoft-image-processing/dist/",
147+
core: "./node_modules/dynamsoft-core/dist/",
148+
license: "./node_modules/dynamsoft-license/dist/",
149+
cvr: "./node_modules/dynamsoft-capture-vision-router/dist/",
150+
dbr: "./node_modules/dynamsoft-barcode-reader/dist/",
151+
dce: "./node_modules/dynamsoft-camera-enhancer/dist/"
152+
};
153+
117154
/** LICENSE ALERT - README
118155
* To use the library, you need to first specify a license key using the API "initLicense()" as shown below.
119156
*/
120157

121-
Dynamsoft.License.LicenseManager.initLicense(
122-
"DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
123-
);
158+
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", true);
124159

125160
/**
126161
* You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days.
@@ -129,63 +164,41 @@ Dynamsoft.License.LicenseManager.initLicense(
129164
* LICENSE ALERT - THE END
130165
*/
131166

132-
Dynamsoft.Core.CoreModule.engineResourcePaths = {
133-
std: "./node_modules/dynamsoft-capture-vision-std/dist/",
134-
dip: "./node_modules/dynamsoft-image-processing/dist/",
135-
core: "./node_modules/dynamsoft-core/dist/",
136-
license: "./node_modules/dynamsoft-license/dist/",
137-
cvr: "./node_modules/dynamsoft-capture-vision-router/dist/",
138-
dbr: "./node_modules/dynamsoft-barcode-reader/dist/",
139-
dce: "./node_modules/dynamsoft-camera-enhancer/dist/"
140-
};
141-
(async function () {
167+
// Optional. Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading.
168+
Dynamsoft.Core.CoreModule.loadWasm(["DBR"]);
169+
170+
(async () => {
142171
try {
143172
// Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
144173
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
145-
const cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(
146-
cameraView
147-
);
148-
document
149-
.querySelector("#div-ui-container")
150-
.append(cameraView.getUIElement()); // Get default UI and append it to DOM.
174+
const cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
175+
// Get default UI and append it to DOM.
176+
document.querySelector("#camera-view-container").append(cameraView.getUIElement());
151177

152178
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
153179
const cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
154180
cvRouter.setInput(cameraEnhancer);
155181

156182
// Define a callback for results.
157-
const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver();
158-
resultReceiver.onDecodedBarcodesReceived = (result) => {
159-
if (!result.barcodeResultItems.length) return;
160-
161-
const resultsContainer = document.querySelector("#div-results-container");
162-
resultsContainer.textContent = '';
163-
console.log(result);
164-
for (let item of result.barcodeResultItems) {
165-
resultsContainer.append(
166-
`${item.formatString}: ${item.text}`,
167-
document.createElement('br'),
168-
document.createElement('hr'),
169-
);
183+
cvRouter.addResultReceiver({
184+
onDecodedBarcodesReceived: (result) => {
185+
if (!result.barcodeResultItems.length) return;
186+
187+
const resultsContainer = document.querySelector("#results");
188+
resultsContainer.textContent = '';
189+
console.log(result);
190+
for (let item of result.barcodeResultItems) {
191+
resultsContainer.textContent += `${item.formatString}: ${item.text}\n\n`;
192+
}
170193
}
171-
};
172-
cvRouter.addResultReceiver(resultReceiver);
194+
});
173195

174196
// Filter out unchecked and duplicate results.
175197
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
176-
filter.enableResultCrossVerification(
177-
"barcode",
178-
true
179-
); // Filter out unchecked barcodes.
198+
// Filter out unchecked barcodes.
199+
filter.enableResultCrossVerification("barcode", true);
180200
// Filter out duplicate barcodes within 3 seconds.
181-
filter.enableResultDeduplication(
182-
"barcode",
183-
true
184-
);
185-
filter.setDuplicateForgetTime(
186-
"barcode",
187-
3000
188-
);
201+
filter.enableResultDeduplication("barcode", true);
189202
await cvRouter.addResultFilter(filter);
190203

191204
// Open camera and start scanning single barcode.
@@ -201,19 +214,27 @@ Dynamsoft.Core.CoreModule.engineResourcePaths = {
201214

202215
### Create an `style.css` file
203216

204-
`index.html` will loads `style.css`, which defines the styles for the UI
217+
`index.html` will load `style.css`, which defines the styles for the UI.
218+
219+
Create the `style.css` file at the root folder. Note that this is customizable!
205220

206221
```css
207-
#div-ui-container {
222+
body {
223+
text-align: center;
224+
}
225+
226+
#camera-view-container {
208227
width: 100%;
209228
height: 80vh;
210229
}
211230

212-
#div-results-container {
231+
#results {
213232
width: 100%;
214233
height: 10vh;
215234
overflow: auto;
235+
white-space: pre-wrap;
216236
}
237+
217238
```
218239

219240
## Run the application

hello-world/electron/action.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
// Configures the paths where the .wasm files and other necessary resources for modules are located.
2+
Dynamsoft.Core.CoreModule.engineResourcePaths = {
3+
std: "./node_modules/dynamsoft-capture-vision-std/dist/",
4+
dip: "./node_modules/dynamsoft-image-processing/dist/",
5+
core: "./node_modules/dynamsoft-core/dist/",
6+
license: "./node_modules/dynamsoft-license/dist/",
7+
cvr: "./node_modules/dynamsoft-capture-vision-router/dist/",
8+
dbr: "./node_modules/dynamsoft-barcode-reader/dist/",
9+
dce: "./node_modules/dynamsoft-camera-enhancer/dist/"
10+
};
11+
112
/** LICENSE ALERT - README
213
* To use the library, you need to first specify a license key using the API "initLicense()" as shown below.
314
*/
415

5-
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
16+
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", true);
617

718
/**
819
* You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days.
@@ -11,48 +22,37 @@ Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMD
1122
* LICENSE ALERT - THE END
1223
*/
1324

14-
Dynamsoft.Core.CoreModule.engineResourcePaths = {
15-
std: "./node_modules/dynamsoft-capture-vision-std/dist/",
16-
dip: "./node_modules/dynamsoft-image-processing/dist/",
17-
core: "./node_modules/dynamsoft-core/dist/",
18-
license: "./node_modules/dynamsoft-license/dist/",
19-
cvr: "./node_modules/dynamsoft-capture-vision-router/dist/",
20-
dbr: "./node_modules/dynamsoft-barcode-reader/dist/",
21-
dce: "./node_modules/dynamsoft-camera-enhancer/dist/",
22-
};
23-
24-
// Optional. Used to load wasm resources in advance, reducing latency between video playing and barcode decoding.
25+
// Optional. Preload "BarcodeReader" module for reading barcodes. It will save time on the initial decoding by skipping the module loading.
2526
Dynamsoft.Core.CoreModule.loadWasm(["DBR"]);
26-
// Defined globally for easy debugging.
27-
let cameraEnhancer, cvRouter;
2827

2928
(async () => {
29+
// Defined globally for easy debugging.
30+
let cameraEnhancer, cvRouter;
31+
3032
try {
3133
// Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
3234
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
3335
cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
3436
// Get default UI and append it to DOM.
35-
document.querySelector("#div-ui-container").append(cameraView.getUIElement());
37+
document.querySelector("#camera-view-container").append(cameraView.getUIElement());
3638

3739
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
3840
cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
3941
cvRouter.setInput(cameraEnhancer);
4042

4143
// Define a callback for results.
42-
cvRouter.addResultReceiver({ onDecodedBarcodesReceived: (result) => {
43-
if (!result.barcodeResultItems.length) return;
44+
cvRouter.addResultReceiver({
45+
onDecodedBarcodesReceived: (result) => {
46+
if (!result.barcodeResultItems.length) return;
4447

45-
const resultsContainer = document.querySelector("#div-results-container");
46-
resultsContainer.textContent = '';
47-
console.log(result);
48-
for (let item of result.barcodeResultItems) {
49-
resultsContainer.append(
50-
`${item.formatString}: ${item.text}`,
51-
document.createElement('br'),
52-
document.createElement('hr'),
53-
);
48+
const resultsContainer = document.querySelector("#results");
49+
resultsContainer.textContent = '';
50+
console.log(result);
51+
for (let item of result.barcodeResultItems) {
52+
resultsContainer.textContent += `${item.formatString}: ${item.text}\n\n`;
53+
}
5454
}
55-
}});
55+
});
5656

5757
// Filter out unchecked and duplicate results.
5858
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();

hello-world/electron/index.html

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
<html>
33
<head>
44
<meta charset="UTF-8" />
5-
<meta name="description" content="Read barcodes from camera with Dynamsoft Barcode Reader in an Electron Application.">
6-
<meta name="keywords" content="barcode, camera, Electron">
5+
<meta
6+
name="description"
7+
content="Read barcodes from camera with Dynamsoft Barcode Reader in an Electron Application."
8+
/>
9+
<meta name="keywords" content="barcode, camera, Electron" />
710
<title>Dynamsoft Barcode Reader Sample - Electron</title>
8-
<link href="style.css" rel="stylesheet">
9-
<script src="./node_modules/dynamsoft-core/dist/core.js"></script>
10-
<script src="./node_modules/dynamsoft-license/dist/license.js"></script>
11-
<script src="./node_modules/dynamsoft-utility/dist/utility.js"></script>
12-
<script src="./node_modules/dynamsoft-barcode-reader/dist/dbr.js"></script>
13-
<script src="./node_modules/dynamsoft-capture-vision-router/dist/cvr.js"></script>
14-
<script src="./node_modules/dynamsoft-camera-enhancer/dist/dce.js"></script>
11+
<link href="style.css" rel="stylesheet" />
12+
<script src="./node_modules/dynamsoft-barcode-reader-bundle/dist/dbr.bundle.js"></script>
1513
</head>
1614
<body>
1715
<h1>Hello World for Electron</h1>
18-
<div id="div-ui-container"></div>
16+
<div id="camera-view-container"></div>
17+
<br />
1918
Results:
20-
<br>
21-
<div id="div-results-container"></div>
19+
<div id="results"></div>
2220
<script src="action.js"></script>
2321
</body>
2422
</html>

hello-world/electron/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
"dynamsoft-capture-vision-std": "1.2.10",
2626
"dynamsoft-image-processing": "2.2.30"
2727
}
28-
}
28+
}

0 commit comments

Comments
 (0)