barcode-detector-api-polyfill 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/browser/barcode-detector-polyfill.min.js +21 -0
- package/browser/barcode-detector-polyfill.min.js.map +7 -0
- package/cjs/BarcodeDetector.d.ts +13 -0
- package/cjs/BarcodeDetector.js +112 -0
- package/cjs/BarcodeDetector.js.map +1 -0
- package/cjs/browser.d.ts +1 -0
- package/cjs/browser.js +7 -0
- package/cjs/browser.js.map +1 -0
- package/cjs/index.d.ts +4 -0
- package/cjs/index.js +7 -0
- package/cjs/index.js.map +1 -0
- package/cjs/models/IBarcodeDetector.d.ts +4 -0
- package/cjs/models/IBarcodeDetector.js +3 -0
- package/cjs/models/IBarcodeDetector.js.map +1 -0
- package/cjs/models/ICornerPoint.d.ts +4 -0
- package/cjs/models/ICornerPoint.js +3 -0
- package/cjs/models/ICornerPoint.js.map +1 -0
- package/cjs/models/IDetectedBarcode.d.ts +7 -0
- package/cjs/models/IDetectedBarcode.js +3 -0
- package/cjs/models/IDetectedBarcode.js.map +1 -0
- package/cjs/models/WindowWithBarcodeDetector.d.ts +8 -0
- package/cjs/models/WindowWithBarcodeDetector.js +3 -0
- package/cjs/models/WindowWithBarcodeDetector.js.map +1 -0
- package/cjs/models/index.d.ts +4 -0
- package/cjs/models/index.js +21 -0
- package/cjs/models/index.js.map +1 -0
- package/es2022/BarcodeDetector.d.ts +13 -0
- package/es2022/BarcodeDetector.js +109 -0
- package/es2022/BarcodeDetector.js.map +1 -0
- package/es2022/browser.d.ts +1 -0
- package/es2022/browser.js +5 -0
- package/es2022/browser.js.map +1 -0
- package/es2022/index.d.ts +4 -0
- package/es2022/index.js +4 -0
- package/es2022/index.js.map +1 -0
- package/es2022/models/IBarcodeDetector.d.ts +4 -0
- package/es2022/models/IBarcodeDetector.js +2 -0
- package/es2022/models/IBarcodeDetector.js.map +1 -0
- package/es2022/models/ICornerPoint.d.ts +4 -0
- package/es2022/models/ICornerPoint.js +2 -0
- package/es2022/models/ICornerPoint.js.map +1 -0
- package/es2022/models/IDetectedBarcode.d.ts +7 -0
- package/es2022/models/IDetectedBarcode.js +2 -0
- package/es2022/models/IDetectedBarcode.js.map +1 -0
- package/es2022/models/WindowWithBarcodeDetector.d.ts +8 -0
- package/es2022/models/WindowWithBarcodeDetector.js +2 -0
- package/es2022/models/WindowWithBarcodeDetector.js.map +1 -0
- package/es2022/models/index.d.ts +4 -0
- package/es2022/models/index.js +5 -0
- package/es2022/models/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Stefan Nedelchev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# BarcodeDetector API Polyfill
|
|
2
|
+
|
|
3
|
+
## The problem
|
|
4
|
+
|
|
5
|
+
The web [BarcodeDetector API](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector) is amazing because it's very easy to use and it works surprizingly well even with detecting muliple barcodes in a single image. However there is a problem with this API - [its current browser support](https://caniuse.com/mdn-api_barcodedetector) (or the lack of such, to be more precise). The current browser support can be summarized like this:
|
|
6
|
+
|
|
7
|
+
- Mobile
|
|
8
|
+
- Android - only Chromium-based browsers
|
|
9
|
+
- iOS - N/A
|
|
10
|
+
- Desktop
|
|
11
|
+
- Windows - N/A
|
|
12
|
+
- Linux - N/A
|
|
13
|
+
- macOS - only Chromium-based browsers *(WTF?)*
|
|
14
|
+
|
|
15
|
+
As you can see there is a huge gap in the support across mobile and desktop platforms and this project is here to fill it.
|
|
16
|
+
|
|
17
|
+
## The solution
|
|
18
|
+
|
|
19
|
+
This project implements a polyfill for the BarcodeDetector API that follows the [W3C specification](https://wicg.github.io/shape-detection-api/#barcode-detection-api) with the help of the [ZXing library](https://github.com/zxing-js/library) and [its browser layer](https://github.com/zxing-js/browser). ZXing ("zebra crossing") is a popular open-source library that works with 1D and 2D barcode formats. It was originally written in Java but was eventually ported to many different languages including JavaScript and TypeScript.
|
|
20
|
+
|
|
21
|
+
## How to use?
|
|
22
|
+
|
|
23
|
+
### Standalone browser script
|
|
24
|
+
|
|
25
|
+
Download the latest build and use it as a regular JS file in a `<script>` tag or include it to your script imports if you use a bundler. This build takes care of adding the missing BarcodeDetector object to the global scope and you are able to use it the same way as the real BarcodeDetector API.
|
|
26
|
+
|
|
27
|
+
### Module import
|
|
28
|
+
|
|
29
|
+
If you just want to have the detector and handle the polyfilling by yourself you can simply import the `BarcodeDetector` class in your code. A valid use case for this could be if you use a bundler and you want to lazy load the polyfill only when its needed.
|
|
30
|
+
|
|
31
|
+
## Important notes and known drawbacks
|
|
32
|
+
|
|
33
|
+
### Consistency with the native API
|
|
34
|
+
|
|
35
|
+
The ZXing reader detects only a single barcode from the image source. Because of that, you can never have more than 1 barcode result.
|
|
36
|
+
|
|
37
|
+
Barcode results from ZXing don't always include 4 corner points. For example, for 1-dimensional barcodes it returns only 2 points so you end up with coordinates for a line rather than a quadrangle. This might be an issue if you want to use these coordinates in your app to draw an outline of the detected barcode on a canvas.
|
|
38
|
+
|
|
39
|
+
### Reliability
|
|
40
|
+
|
|
41
|
+
Although for the most part ZXing works well, it's not as reliable as the native BarcodeDetector API. It might struggle in situations like:
|
|
42
|
+
|
|
43
|
+
- the barcode is not focused well
|
|
44
|
+
- the barcode is at a very narrow angle
|
|
45
|
+
- the image is not clean enough
|
|
46
|
+
|
|
47
|
+
### Performance
|
|
48
|
+
|
|
49
|
+
Although it might be obvious, the image recognition logic is now handled entirely in the JavaScript code, therefore you can't expect performance as good as the native API. If you are going to use the detector with static images, you don't need to worry about that, but if you want to use the detector continuously on a streaming video, you can potentially expect performance issues.
|
|
50
|
+
|
|
51
|
+
### Bundle size
|
|
52
|
+
|
|
53
|
+
Due to the presence of all ZXing decoders, the minified JS build of the polyfill is ~430 kb (before gzip compression). This could be a deal breaker for low bandwidth connections and Core Web Vitals scores. You can potentially work your way around this by lazy loading the polyfill which involves additional effort.
|
|
54
|
+
|
|
55
|
+
## How it's done?
|
|
56
|
+
|
|
57
|
+
This section here is for the curious ones who are interested in the development itself (and maybe want to contribute too). The polyfill is basically a class that implements the specified BarcodeDetector interface, and [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/BarcodeDetector) has been very helpful during the development. The class uses a ZXing multi-format reader internally and initializes it with a specific list of formats (if such are passed to the constructor). The class has the public static method `getSupportedFormats()` and the instance method `detect()`, both of can be used the same way as the native API.
|
|
58
|
+
|
|
59
|
+
The implementation of the `detect()` method was a challenge because unlike the BarcodeDetector API, ZXing doesn't have this silver bullet detection method that accepts all kinds of image sources but rather has different methods that decode from different sources (canvas, video, image, stream, etc.). Because of this, the polyfill implementation conditionally calls different methods for one-time detection based on the type of the image source.
|
|
60
|
+
|
|
61
|
+
Another challange was to align the differences between the barcode formats of ZXing and the BarcodeDetector API. The barcode formats in ZXing are presented as an `enum` while the BarcodeDetector API accepts (and returns) strings. To make the methods of the polyfill work as expected, two special map-like objects had to be created to make it possible to map barcode formats back and forth.
|