barcode-detector-api-polyfill 1.0.0 → 1.0.3

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## The problem
4
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:
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 surprisingly well even with detecting multiple 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
6
 
7
7
  - Mobile
8
8
  - Android - only Chromium-based browsers
@@ -18,15 +18,36 @@ As you can see there is a huge gap in the support across mobile and desktop plat
18
18
 
19
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
20
 
21
- ## How to use?
21
+ ## How to install?
22
22
 
23
23
  ### Standalone browser script
24
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.
25
+ Download the latest build and use it as a regular JS file in a `<script>` tag. 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
26
 
27
- ### Module import
27
+ ```html
28
+ <script src="barcode-detector-polyfill.min.js"></script>
29
+ ```
28
30
 
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.
31
+
32
+ ### NPM module
33
+
34
+ ```shell
35
+ npm install barcode-detector-api-polyfill
36
+ ```
37
+
38
+ Include `node_modules/barcode-detector-api-polyfill/browser/barcode-detector-polyfill.min.js` to your script imports if you use a bundler.
39
+
40
+ If you just want to have the detector and handle the polyfilling by yourself you can simply import the `BarcodeDetector` class in your code.
41
+
42
+ ```ts
43
+ import { BarcodeDetector, WindowWithBarcodeDetector } from 'barcode-detector-api-polyfill';
44
+
45
+ if (!('BarcodeDetector' in window)) {
46
+ (window as WindowWithBarcodeDetector).BarcodeDetector = BarcodeDetector;
47
+ }
48
+ ```
49
+
50
+ A valid use case for this could be if you use a bundler and you want to lazy load the polyfill only when it's needed.
30
51
 
31
52
  ## Important notes and known drawbacks
32
53
 
@@ -34,7 +55,7 @@ If you just want to have the detector and handle the polyfilling by yourself you
34
55
 
35
56
  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
57
 
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.
58
+ 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
59
 
39
60
  ### Reliability
40
61
 
@@ -48,6 +69,8 @@ Although for the most part ZXing works well, it's not as reliable as the native
48
69
 
49
70
  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
71
 
72
+ *NOTE: I'm open for duscussions on weather we should use a web worker implementation to make detection process smoother!*
73
+
51
74
  ### Bundle size
52
75
 
53
76
  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.
@@ -58,4 +81,4 @@ This section here is for the curious ones who are interested in the development
58
81
 
59
82
  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
83
 
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.
84
+ Another challenge 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.