Skip to content

Commit d151de4

Browse files
committed
Use barcode-detector polyfill as secondary decoder, even if native lib is available
The polyfill has sometimes better recognition quality than the native lib, so additionally use it to improve detection quality.
1 parent ef1525f commit d151de4

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed

package-lock.json

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@part-db/html5-qrcode",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "A cross platform HTML5 QR Code & bar code scanner (forked for Part-DB)",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",
@@ -89,5 +89,8 @@
8989
},
9090
"publishConfig": {
9191
"access": "public"
92+
},
93+
"dependencies": {
94+
"barcode-detector": "^2.3.1"
9295
}
9396
}

src/code-decoder.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ export class Html5QrcodeShim implements RobustQrcodeDecoderAsync {
4040
logger: Logger) {
4141
this.verbose = verbose;
4242

43-
if (!BarcodeDetectorDelegate.isSupported()) {
44-
throw new Error("BarcodeDetector is not supported by this browser. Use Polyfill if required!");
45-
}
43+
//If native BarcodeDetector is supported, use it as primary decoder and polyfill as secondary.
44+
if (BarcodeDetectorDelegate.isNativeSupported()) {
45+
logger.log("Native BarcodeDetector is supported, using it as primary decoder.");
4646

47-
//Always use BarcodeDetector, as we removed the zxing-js library.
48-
this.primaryDecoder = new BarcodeDetectorDelegate(
49-
requestedFormats, verbose, logger);
47+
this.primaryDecoder = new BarcodeDetectorDelegate(
48+
requestedFormats, verbose, logger);
49+
this.secondaryDecoder = new BarcodeDetectorDelegate(
50+
requestedFormats, verbose, logger, true);
51+
} else { //Otherwise use polyfill as primary decoder.
52+
logger.log("Native BarcodeDetector is not supported, using polyfill as primary decoder.");
5053

54+
this.primaryDecoder = new BarcodeDetectorDelegate(
55+
requestedFormats, verbose, logger, true);
56+
}
5157
}
5258

5359
async decodeAsync(canvas: HTMLCanvasElement): Promise<QrcodeResult> {

src/native-bar-code-detector.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
Logger
2121
} from "./core";
2222

23+
//@ts-ignore
24+
import { BarcodeDetector as BarcodeDetectorPolyfill } from "barcode-detector/pure";
25+
2326
declare const BarcodeDetector: any;
2427

2528
/** Config for BarcodeDetector API. */
@@ -98,7 +101,7 @@ interface BarcodeDetectorResult {
98101
* getSupportedFormats() API.
99102
* @returns
100103
*/
101-
public static isSupported(): boolean {
104+
public static isNativeSupported(): boolean {
102105
if (!("BarcodeDetector" in window)) {
103106
return false;
104107
}
@@ -109,17 +112,22 @@ interface BarcodeDetectorResult {
109112
public constructor(
110113
requestedFormats: Array<Html5QrcodeSupportedFormats>,
111114
verbose: boolean,
112-
logger: Logger) {
113-
if (!BarcodeDetectorDelegate.isSupported()) {
114-
throw "Use html5qrcode.min.js without edit, Use "
115-
+ "BarcodeDetectorDelegate only if it isSupported();";
115+
logger: Logger,
116+
usePolyfill: boolean = false,
117+
) {
118+
if (!usePolyfill && !BarcodeDetectorDelegate.isNativeSupported()) {
119+
throw "BarcodeDetector is not supported natively by this browser. Use Polyfill by setting usePolyfill to true!";
116120
}
117121
this.verbose = verbose;
118122
this.logger = logger;
119123

120124
// create new detector
121125
const formats = this.createBarcodeDetectorFormats(requestedFormats);
122-
this.detector = new BarcodeDetector(formats);
126+
if (!usePolyfill) { //Use the native version
127+
this.detector = new BarcodeDetector(formats);
128+
} else { //Otherwise use the polyfill
129+
this.detector = new BarcodeDetectorPolyfill(formats);
130+
}
123131

124132
// check compatibility
125133
if (!this.detector) {

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"preserveConstEnums": true,
1818
"noImplicitAny": true,
1919

20+
"skipLibCheck": true,
21+
2022
// Allow javascript source to be built
2123
"allowJs": true
2224
},

0 commit comments

Comments
 (0)