-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BarcodeDetector and refactors around it (#238)
* Add support for BarcodeDetector and refactors around it TODOs - [ ] Check support / performance on Android / IOS device - [ ] Add additional documentation (Github / article) for experimental features - [ ] Link this to readme - [ ] Update demo page Notes: - On Chrome `ZXing` based decoder takes `20-25` ms on my Mac book pro 16. - On Chrome `BarcodeDetector` based decoder takes `8.6-11 ms` on my Mac book pro 16. * Fixes for codacy reported issues * Add more documentation * Link experimental read and fix Codacy issues * Fix how support is checked for BarcodeDetector Fix added for Safari. Also, fixed some Codacy styling issues on readme * fix codacy issues and update performance report * fix table in markdown
- Loading branch information
Showing
12 changed files
with
513 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Generated code, do not modify manually. | ||
|
||
- All files in this directory are minified from source code in the project hosted at [src](../src). | ||
- Generated files should be supported in all major browsers. | ||
- All files in this directory are minified from source code in the project hosted at [src](../src). | ||
- Generated files should be supported in all major browsers. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Experimental features | ||
Some of the features that are supported by the library but not officially | ||
recommended to be used across devices. If you find any of the features useful, | ||
please feel free to use them at your own risk, they are configurable in the | ||
library. In future, based on the support level and compatibility, some of | ||
these features will get upgraded to general feature list. | ||
|
||
## Using experimental native BarcodeDetector API | ||
Turning on this flag allows using native [BarcodeDetector](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector) | ||
api now being introduced in web browsers for code scanning instead of `ZXing` | ||
library we use officially. | ||
|
||
### How to turn this on | ||
It can be turned on using new config called `useBarCodeDetectorIfSupported` | ||
added to `experimentalFeatures` config group. It's off (`value = false`) by | ||
default. If set to on (`value = true`) and the `BarcodeDetector` is supported | ||
by the browser, it'll be used for scanning all the kind of 1d and 2d codes. | ||
|
||
#### Html5Qrcode class | ||
|
||
```js | ||
function onScanSuccess(decodedText, decodedResult) { | ||
/** Handle success condition. */ | ||
} | ||
|
||
let html5qrcode = new Html5Qrcode("reader", { | ||
// Use this flag to turn on the feature. | ||
experimentalFeatures: { | ||
useBarCodeDetectorIfSupported: false | ||
} | ||
}); | ||
|
||
const scanConfig = { fps: 10, qrbox: 250 }; | ||
// If you want to prefer front camera | ||
html5qrcode.start({ facingMode: "user" }, scanConfig, onScanSuccess); | ||
``` | ||
|
||
#### Html5QrcodeScanner class | ||
|
||
```js | ||
function onScanSuccess(decodedText, decodedResult) { | ||
/** Handle success condition. */ | ||
} | ||
|
||
let html5QrcodeScanner = new Html5QrcodeScanner( | ||
"reader", | ||
{ | ||
fps: 10, | ||
qrbox: 250, | ||
// Use this flag to turn on the feature. | ||
experimentalFeatures: { | ||
useBarCodeDetectorIfSupported: false | ||
} | ||
}); | ||
html5QrcodeScanner.render(onScanSuccess); | ||
``` | ||
|
||
### performance | ||
|
||
Native scanning library seems to be giving better performance for scanning QR | ||
codes than with Zxing library. | ||
|
||
| Device | With ZXing | With BarcodeDecoder | | ||
|--|--|--| | ||
| Macbook Pro 16, Google Chrome | 21 ms | 10 ms| | ||
| Pixel 4 Google Chrome | 56 ms | 23 ms | | ||
| Pixel 4a Google Chrome | 92 ms| 47.3 ms | | ||
| (low end) Android Device Google Chrome | 373 ms | 77.5 ms| | ||
|
||
Not supported on: | ||
- Macbook Pro 16, Safari (macOS Big Sur) | ||
|
||
### More references | ||
- [https://web.dev/shape-detection/#barcodedetector](https://web.dev/shape-detection/#barcodedetector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @fileoverview | ||
* Core library for experimental features. | ||
* | ||
* @author mebjas <[email protected]> | ||
* | ||
* Experimental features are those which have limited browser compatibility and | ||
* hidden from official documentations. These features are not recommended by | ||
* the author to be used in production unless explictly tested. | ||
* | ||
* Subset of the features are expected to upgrade to official feature list from | ||
* time to time. | ||
* | ||
* The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED | ||
* http://www.denso-wave.com/qrcode/faqpatent-e.html | ||
*/ | ||
|
||
|
||
/** | ||
* Configuration for enabling or disabling experimental features in the library. | ||
* | ||
* These features will eventually upgrade as fully supported features in the | ||
* library. | ||
*/ | ||
export interface ExperimentalFeaturesConfig { | ||
/** | ||
* {@class BarcodeDetector} is being implemented by browsers at the moment. | ||
* It has very limited browser support but as it gets available it could | ||
* enable faster native code scanning experience. | ||
* | ||
* Set this flag to true, to enable using {@class BarcodeDetector} if | ||
* supported. This is false by default. | ||
* | ||
* Documentations: | ||
* - https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector | ||
* - https://web.dev/shape-detection/#barcodedetector | ||
*/ | ||
useBarCodeDetectorIfSupported?: boolean | undefined; | ||
} | ||
|
||
/** Factory for {@interface ExperimentalFeaturesConfig}. */ | ||
export class ExperimentalFeaturesConfigFactory { | ||
|
||
/** | ||
* Creates fully filled experimental config. | ||
*/ | ||
public static createExperimentalFeaturesConfig( | ||
config?: ExperimentalFeaturesConfig | undefined) | ||
: ExperimentalFeaturesConfig { | ||
if (!config) { | ||
return { | ||
useBarCodeDetectorIfSupported: false | ||
}; | ||
} | ||
|
||
if (config.useBarCodeDetectorIfSupported !== true) { | ||
config.useBarCodeDetectorIfSupported = false; | ||
} | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.