Skip to content

Commit 1894d20

Browse files
author
Cube
authored
Merge pull request #105 from Dynamsoft/9.6.20
9.6.20
2 parents 194a49b + 3da026a commit 1894d20

File tree

209 files changed

+5718
-2708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+5718
-2708
lines changed

1.hello-world/1.hello-world.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<body>
1414
Loading...
15-
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.11/dist/dbr.js"></script>
15+
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.20/dist/dbr.js"></script>
1616
<script>
1717
if(location.protocol === "file:") {
1818
const message = `The page is opened via file:// and "BarcodeScanner" may not work properly. Please open the page via https:// or host it on "http://localhost/".`;
@@ -30,7 +30,7 @@
3030
/**
3131
* 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.
3232
* Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license.
33-
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.11&utm_source=github#specify-the-license or contact support@dynamsoft.com.
33+
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.20&utm_source=github#specify-the-license or contact support@dynamsoft.com.
3434
* LICENSE ALERT - THE END
3535
*/
3636

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# JavaScript Hello World Sample - PWA
2+
3+
[PWA](https://web.dev/progressive-web-apps/) is short for Progressive Web Apps which stand for web applications that have been designed to behave like platform-specific (native) applications. Check out the following on how to implement Dynamsoft Barcode Reader JavaScript SDK (hereafter called "the library") into a PWA application.
4+
5+
## Official Sample
6+
7+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/1.hello-world/10.read-video-pwa/helloworld-pwa.html">Hello World in PWA - Demo</a>
8+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/main/1.hello-world/10.read-video-pwa">Hello World in PWA - Source Code</a>
9+
10+
## Preparation
11+
12+
We will try to turn our most basic hello world sample into a PWA.
13+
14+
First, create a file with the name "helloworld-pwa.html" and fill it with the following code:
15+
16+
```html
17+
<!DOCTYPE html>
18+
<html lang="en">
19+
20+
<head>
21+
<meta charset="utf-8">
22+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
23+
<title>Dynamsoft Barcode Reader PWA Sample - Hello World (Decoding via Camera)</title>
24+
</head>
25+
26+
<body>
27+
<h1 style="font-size: 1.5em;">Hello World for PWA</h1>
28+
Loading...
29+
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.20/dist/dbr.js"></script>
30+
<script>
31+
Dynamsoft.DBR.BarcodeReader.license = 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9';
32+
(async function() {
33+
try {
34+
const scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
35+
scanner.onFrameRead = results => {
36+
console.log("Barcodes on one frame:");
37+
for (let result of results) {
38+
const format = result.barcodeFormatString;
39+
console.log(format + ": " + result.barcodeText);
40+
}
41+
};
42+
scanner.onUniqueRead = (txt, result) => {
43+
alert(txt);
44+
console.log("Unique Code Found: ", result);
45+
}
46+
await scanner.show();
47+
} catch (ex) {
48+
let errMsg = ex.message||ex;
49+
console.error(errMsg);
50+
alert(errMsg);
51+
}
52+
})();
53+
54+
if ('serviceWorker' in navigator) {
55+
navigator.serviceWorker.register('./service-worker.js');
56+
};
57+
</script>
58+
</body>
59+
60+
</html>
61+
```
62+
63+
Next, set up a secure environment (HTTPs) to run the page "helloworld-pwa.html". This is required because PWAs only run in secure environments.
64+
65+
In our case, we use IIS to set up a secure site at "https://localhost" and the page is put at the root so that it can be accessed at "https://localhost/helloworld-pwa.html".
66+
67+
## Make the app progressive
68+
69+
### Register a service worker for offline support
70+
71+
As the basis for PWAs, Service Workers are a virtual proxy between the browser and the network. A service worker can serve content offline, handle notifications and perform heavy calculations, etc. all on a separate thread.
72+
73+
To use a service worker, we first need to register it. In the helloworld-pwa.html file, add the following at the end of the script:
74+
75+
```javascript
76+
if ('serviceWorker' in navigator) {
77+
navigator.serviceWorker.register('./service-worker.js');
78+
};
79+
```
80+
81+
Create the service-worker.js file with the following content:
82+
83+
```javascript
84+
// Files to cache
85+
const cacheName = 'helloworld-pwa';
86+
const appShellFiles = [
87+
'./helloworld-pwa.html',
88+
];
89+
90+
// Installing Service Worker
91+
self.addEventListener('install', (e) => {
92+
console.log('[Service Worker] Install');
93+
e.waitUntil((async () => {
94+
const cache = await caches.open(cacheName);
95+
console.log('[Service Worker] Caching all: app shell and content');
96+
await cache.addAll(appShellFiles);
97+
})());
98+
});
99+
100+
self.addEventListener('fetch', (e) => {
101+
e.respondWith((async () => {
102+
const r = await caches.match(e.request);
103+
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
104+
if (r) { return r; }
105+
const response = await fetch(e.request);
106+
const cache = await caches.open(cacheName);
107+
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
108+
if (e.request.method !== "POST")
109+
cache.put(e.request, response.clone());
110+
return response;
111+
})());
112+
});
113+
```
114+
115+
With the above code, the application can now work offline because the service worker will cache the page helloworld-pwa.html and its related resources.
116+
117+
For more information, refer to [Making PWAs work offline with Service workers](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers).
118+
119+
> NOTE
120+
>
121+
> Since the files are being cached, changes we make in later steps may not be reflected. Therefore, don't forget to clear the cache after a change is made. To do so, you can run the following in the browser console.
122+
>
123+
> ```javascript
124+
> const cacheName = 'helloworld-pwa';
125+
> const cache = await caches.delete(cacheName);
126+
> ```
127+
128+
### Use a web manifest file to make the application installable
129+
130+
A web manifest file contains a list of information about a website in a JSON format. This information is used to present the web app correctly for installation on a device.
131+
132+
In our example, we first create a file "helloworld-pwa.webmanifest" with the following content:
133+
134+
```json
135+
{
136+
"name": "Dynamsoft Barcode Reader Progressive Web App",
137+
"short_name": "DBR-PWA",
138+
"description": "Progressive Web App that reads barcodes from a video input with Dynamsoft Barcode Reader.",
139+
"start_url": "./helloworld-pwa.html",
140+
"scope": ".",
141+
"display": "standalone",
142+
"theme_color": "#B12A34",
143+
"background_color": "#B12A34",
144+
"icons": [
145+
{
146+
"src": "./dynamsoft-512x512.png",
147+
"sizes": "512x512",
148+
"type": "image/png"
149+
},
150+
{
151+
"src": "./dynamsoft-192x192.png",
152+
"sizes": "192x192",
153+
"type": "image/png"
154+
}
155+
]
156+
}
157+
```
158+
159+
> The icon files can be found in the github repository.
160+
161+
Then we include the file in the &lt;head&gt; block of the helloworld-pwa.html file:
162+
163+
```html
164+
<link rel="manifest" href="helloworld-pwa.webmanifest">
165+
```
166+
167+
For compatibility on safari, we need add some `meta` in `<head>`:
168+
169+
```html
170+
<meta name="theme-color" content="#B12A34">
171+
<meta name="mobile-web-app-capable" content="yes">
172+
<meta name="apple-mobile-web-app-title" content="sample for ios">
173+
<meta name="apple-mobile-web-app-capable" content="yes">
174+
<meta name="apple-mobile-web-app-status-bar-style" content="default">
175+
<link rel="apple-touch-icon" sizes="192x192" href="./dynamsoft-192x192.png" />
176+
<link rel="apple-touch-icon" sizes="512x512" href="./dynamsoft-512x512.png" />
177+
```
178+
179+
Now, if you open the application again in your browser, you will notice an install icon appear on the right side of the address bar. When you click on it, a pop-up will appear asking if you want to install the app.
180+
181+
Once installed, you can use it like a native app.
182+
183+
For offline use, you need to cache more files.
184+
185+
service-worker.js
186+
```javascript
187+
const dbrVersion = "9.6.20";
188+
const dbrCdn = `https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@${dbrVersion}/dist/`;
189+
190+
const appShellFiles = [
191+
'./helloworld-pwa.html',
192+
'./dynamsoft-192x192.png',
193+
'./dynamsoft-512x512.png',
194+
'./helloworld-pwa.json',
195+
`${dbrCdn}dbr.js`,
196+
`${dbrCdn}dbr-${dbrVersion}.full.wasm`,
197+
`${dbrCdn}dbr-${dbrVersion}.full.wasm.js`,
198+
`${dbrCdn}dbr-${dbrVersion}.browser.worker.js`,
199+
];
200+
```
201+
202+
## Summary
203+
204+
In this article we took a look at how you can turn a simple barcode reading page into a PWA that is installable, re-engageable and capable of working offline. To learn more about Progressive web apps, you can click [here](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps).
-1.69 KB
Binary file not shown.
-18.7 KB
Binary file not shown.
6.48 KB
Loading
9.71 KB
Loading

1.hello-world/10.read-video-pwa/helloworld-pwa.html

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
<title>Dynamsoft Barcode Reader PWA Sample - Hello World (Decoding via Camera)</title>
1010
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/1.hello-world/10.read-video-pwa/helloworld-pwa.html">
1111
<link rel="manifest" href="./helloworld-pwa.json">
12+
<meta name="theme-color" content="#B12A34">
13+
<meta name="mobile-web-app-capable" content="yes">
14+
<meta name="apple-mobile-web-app-title" content="sample for ios">
15+
<meta name="apple-mobile-web-app-capable" content="yes">
16+
<meta name="apple-mobile-web-app-status-bar-style" content="default">
17+
<link rel="apple-touch-icon" sizes="192x192" href="./dynamsoft-192x192.png" />
18+
<link rel="apple-touch-icon" sizes="512x512" href="./dynamsoft-512x512.png" />
1219
</head>
1320

1421
<body>
1522
<h1 style="font-size: 1.5em;">Hello World for PWA</h1>
1623
Loading...
17-
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.11/dist/dbr.js"></script>
24+
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@9.6.20/dist/dbr.js"></script>
1825
<script>
1926
if(location.protocol === "file:") {
2027
const message = `The page is opened via file:// and "BarcodeScanner" may not work properly. Please open the page via https:// or host it on "http://localhost/".`;
@@ -32,54 +39,40 @@ <h1 style="font-size: 1.5em;">Hello World for PWA</h1>
3239
/**
3340
* 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.
3441
* Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license.
35-
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.11&utm_source=github#specify-the-license or contact support@dynamsoft.com.
42+
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.20&utm_source=github#specify-the-license or contact support@dynamsoft.com.
3643
* LICENSE ALERT - THE END
3744
*/
3845

39-
let pScanner = null;
40-
let latestResult = null;
41-
42-
function startNotificationLoop() {
43-
if (latestResult != null) {
44-
const title = "New Barcode Found!";
45-
const notifBody = `Barcode Text: ${latestResult}.`;
46-
const options = {
47-
body: notifBody,
48-
};
49-
new Notification(title, options);
50-
latestResult = null;
51-
}
52-
setTimeout(startNotificationLoop, 100);
53-
}
54-
5546
(async function() {
56-
if(window.Notification) {
57-
Notification.requestPermission().then((result) => {
58-
if (result === 'granted') {
59-
startNotificationLoop();
60-
}
61-
});
62-
}
6347
try {
64-
const scanner = await (pScanner = pScanner || Dynamsoft.DBR.BarcodeScanner.createInstance());
48+
const scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
49+
/**
50+
* 'onFrameRead' is triggered after the library finishes reading a frame image.
51+
* There can be multiple barcodes on one image.
52+
*/
6553
scanner.onFrameRead = results => {
6654
console.log("Barcodes on one frame:");
6755
for (let result of results) {
6856
const format = result.barcodeFormatString;
6957
console.log(format + ": " + result.barcodeText);
7058
}
7159
};
60+
/**
61+
* 'onUniqueRead' is triggered only when a 'new' barcode is found.
62+
* The amount of time that the library 'remembers' a barcode is defined by
63+
* "duplicateForgetTime" in "ScanSettings". By default it is set to 3000 ms.
64+
*/
7265
scanner.onUniqueRead = (txt, result) => {
73-
latestResult = txt;
66+
alert(txt);
67+
console.log("Unique Code Found: ", result);
7468
}
69+
/**
70+
* 'show()' opens the camera and shows the video stream on the page.
71+
* After that, the library starts to scan the frame images continuously.
72+
*/
7573
await scanner.show();
7674
} catch (ex) {
77-
let errMsg;
78-
if (ex.message.includes("network connection error")) {
79-
errMsg = "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.";
80-
} else {
81-
errMsg = ex.message||ex;
82-
}
75+
let errMsg = ex.message||ex;
8376
console.error(errMsg);
8477
alert(errMsg);
8578
}

1.hello-world/10.read-video-pwa/helloworld-pwa.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
"name": "Dynamsoft Barcode Reader Progressive Web App",
33
"short_name": "DBR-PWA",
44
"description": "Progressive Web App that reads barcodes from a video input with Dynamsoft Barcode Reader.",
5+
"start_url": "./helloworld-pwa.html",
6+
"scope": ".",
7+
"display": "standalone",
8+
"theme_color": "#B12A34",
9+
"background_color": "#B12A34",
510
"icons": [
611
{
7-
"src": "dbr-bigger.png",
8-
"sizes": "256x256",
9-
"type": "image/png"
12+
"src": "./dynamsoft-512x512.png",
13+
"sizes": "512x512",
14+
"type": "image/png"
1015
},
1116
{
12-
"src": "dbr-big.png",
13-
"sizes": "128x128",
14-
"type": "image/png"
17+
"src": "./dynamsoft-192x192.png",
18+
"sizes": "192x192",
19+
"type": "image/png"
1520
}
16-
],
17-
"start_url": "./helloworld-pwa.html",
18-
"display": "fullscreen",
19-
"theme_color": "#B12A34",
20-
"background_color": "#B12A34"
21-
}
21+
]
22+
}

1.hello-world/10.read-video-pwa/service-worker.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
const dbrVersion = "9.6.20";
2+
const dbrCdn = `https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@${dbrVersion}/dist/`;
3+
14
// Files to cache
25
const cacheName = 'helloworld-pwa';
36
const appShellFiles = [
47
'./helloworld-pwa.html',
5-
'./dbr-big.png',
8+
'./dynamsoft-192x192.png',
9+
'./dynamsoft-512x512.png',
610
'./helloworld-pwa.json',
11+
`${dbrCdn}dbr.js`,
12+
`${dbrCdn}dbr-${dbrVersion}.full.wasm`,
13+
`${dbrCdn}dbr-${dbrVersion}.full.wasm.js`,
14+
`${dbrCdn}dbr-${dbrVersion}.browser.worker.js`,
715
];
816

917
// Installing Service Worker
@@ -28,4 +36,8 @@ self.addEventListener('fetch', (e) => {
2836
cache.put(e.request, response.clone());
2937
return response;
3038
})());
31-
});
39+
});
40+
41+
42+
43+

0 commit comments

Comments
 (0)