react-qr-barcode-scanner 2.0.4 → 2.1.1

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
@@ -18,15 +18,15 @@ Try a demo of the scanner [here](https://jamenamcinteer.github.io/react-qr-barco
18
18
  ## Usage in React:
19
19
 
20
20
  ```jsx
21
- import React from "react";
22
- import BarcodeScannerComponent from "react-qr-barcode-scanner";
21
+ import { useState } from "react";
22
+ import BarcodeScanner from "react-qr-barcode-scanner";
23
23
 
24
24
  function App() {
25
- const [data, setData] = React.useState("Not Found");
25
+ const [data, setData] = useState("Not Found");
26
26
 
27
27
  return (
28
28
  <>
29
- <BarcodeScannerComponent
29
+ <BarcodeScanner
30
30
  width={500}
31
31
  height={500}
32
32
  onUpdate={(err, result) => {
@@ -84,6 +84,26 @@ Type: `boolean`, Optional
84
84
 
85
85
  Turn the camera flashlight on or off.
86
86
 
87
+ Torch will only work if the correct `facingMode` is selected. For example, if "user" is selected but the flashlight corresponds to the "environment" camera, then it will not turn on.
88
+
89
+ Torch will not turn on if a static value of `true` is passed. The initial value must be `false`, and then switched to `true` after a timeout or after a user action.
90
+
91
+ Here is an example using a timeout:
92
+ ```
93
+ const [torchEnabled, setTorchEnabled] = useState<boolean>(false);
94
+
95
+ useEffect(() => {
96
+ setTimeout(() => {
97
+ setTorchEnabled(true);
98
+ }, 2000);
99
+ }, []);
100
+
101
+ <BarcodeScanner
102
+ torch={torchEnabled}
103
+ onUpdate={(err, result) => {}}
104
+ />
105
+ ```
106
+
87
107
  ### delay
88
108
 
89
109
  Type: `number`, Optional, Default: `500`
@@ -98,6 +118,12 @@ Type: `boolean`, Optional
98
118
 
99
119
  This prop is a workaround for a bug where the browser freezes if the webcam component is unmounted or removed. See known issues for more about this issue.
100
120
 
121
+ ### formats
122
+
123
+ Type: `BarcodeFormat[] | BarcodeStringFormat[]`, Optional
124
+
125
+ Array of barcode formats to decode. If not provided, all formats will be used. A smaller list may improve the speed of the scan.
126
+
101
127
  ## Supported Barcode Formats
102
128
 
103
129
  These formats are supported by ZXing:
@@ -0,0 +1,68 @@
1
+ import { BarcodeFormat, Result } from "@zxing/library";
2
+ import { BarcodeStringFormat } from "./BarcodeStringFormat";
3
+ declare global {
4
+ interface MediaTrackCapabilities {
5
+ torch?: boolean;
6
+ }
7
+ }
8
+ export declare const BarcodeScanner: ({ onUpdate, onError, width, height, facingMode, torch, delay, videoConstraints, stopStream, formats, }: {
9
+ /**
10
+ * Function that returns the result for every captured frame. Text from barcode can be accessed from result.getText() if there is a result.
11
+ * @param arg0 Error message or null
12
+ * @param arg1 result of the scan
13
+ * @returns
14
+ */
15
+ onUpdate: (arg0: unknown, arg1?: Result) => void;
16
+ /**
17
+ * If passed to the component, this function is called when there is an error with the camera (rather than with with reading the QR code, which would be passed to onUpdate). An example would be an error thrown when the user does not allow the required camera permission.
18
+ * @param arg0 Error message or DOMException
19
+ * @returns
20
+ */
21
+ onError?: (arg0: string | DOMException) => void;
22
+ /**
23
+ * The width of the video element. Default is "100%".
24
+ * Can be a number (in pixels) or a string (e.g. "100%").
25
+ * If a number is provided, it will be converted to a string with "px" appended.
26
+ * If a string is provided, it will be used as-is.
27
+ * @example
28
+ * width={500} // 500px
29
+ * width="100%" // 100% of the parent element
30
+ */
31
+ width?: number | string;
32
+ /**
33
+ * The height of the video element. Default is "100%".
34
+ * Can be a number (in pixels) or a string (e.g. "100%").
35
+ * If a number is provided, it will be converted to a string with "px" appended.
36
+ * If a string is provided, it will be used as-is.
37
+ * @example
38
+ * height={500} // 500px
39
+ * height="100%" // 100% of the parent element
40
+ */
41
+ height?: number | string;
42
+ /**
43
+ * The facing mode of the camera. Default is "environment".
44
+ * Can be "user" for front camera or "environment" for back camera.
45
+ */
46
+ facingMode?: "environment" | "user";
47
+ /**
48
+ * Whether to turn on the flashlight. Default is false.
49
+ */
50
+ torch?: boolean;
51
+ /**
52
+ * Delay between scans in milliseconds. Default is 500ms.
53
+ */
54
+ delay?: number;
55
+ /**
56
+ * Video constraints to pass to the webcam. If not provided, the default constraints will be used.
57
+ */
58
+ videoConstraints?: MediaTrackConstraints;
59
+ stopStream?: boolean;
60
+ /**
61
+ * Array of barcode formats to decode. If not provided, all formats will be used. A smaller list may improve the speed of the scan.
62
+ *
63
+ * @example
64
+ * formats={["QR_CODE", "DATA_MATRIX"]}
65
+ */
66
+ formats?: BarcodeFormat[] | BarcodeStringFormat[];
67
+ }) => React.ReactElement;
68
+ export default BarcodeScanner;
@@ -0,0 +1,63 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useRef } from "react";
3
+ import { BarcodeFormat, BrowserMultiFormatReader, DecodeHintType, } from "@zxing/library";
4
+ import Webcam from "react-webcam";
5
+ export const BarcodeScanner = ({ onUpdate, onError, width = "100%", height = "100%", facingMode = "environment", torch, delay = 500, videoConstraints, stopStream, formats, }) => {
6
+ const webcamRef = useRef(null);
7
+ const capture = useCallback(() => {
8
+ const codeReader = new BrowserMultiFormatReader(new Map([
9
+ [
10
+ DecodeHintType.POSSIBLE_FORMATS,
11
+ formats?.map((f) => (typeof f === "string" ? BarcodeFormat[f] : f)),
12
+ ],
13
+ ]));
14
+ const imageSrc = webcamRef?.current?.getScreenshot();
15
+ if (imageSrc) {
16
+ codeReader
17
+ .decodeFromImage(undefined, imageSrc)
18
+ .then((result) => {
19
+ onUpdate(null, result);
20
+ })
21
+ .catch((err) => {
22
+ onUpdate(err);
23
+ });
24
+ }
25
+ }, [onUpdate, formats]);
26
+ useEffect(() => {
27
+ // Turn on the flashlight if prop is defined and device has the capability
28
+ if (typeof torch === "boolean" &&
29
+ (navigator?.mediaDevices?.getSupportedConstraints()).torch) {
30
+ const stream = webcamRef?.current?.video?.srcObject;
31
+ const track = stream?.getVideoTracks()[0]; // get the active track of the stream
32
+ if (track && track.getCapabilities().torch) {
33
+ track
34
+ .applyConstraints({
35
+ advanced: [{ torch }],
36
+ })
37
+ .catch((err) => onUpdate(err));
38
+ }
39
+ }
40
+ }, [torch, onUpdate]);
41
+ useEffect(() => {
42
+ if (stopStream) {
43
+ let stream = webcamRef?.current?.video?.srcObject;
44
+ if (stream) {
45
+ stream.getTracks().forEach((track) => {
46
+ stream?.removeTrack(track);
47
+ track.stop();
48
+ });
49
+ stream = null;
50
+ }
51
+ }
52
+ }, [stopStream]);
53
+ useEffect(() => {
54
+ const interval = setInterval(capture, delay);
55
+ return () => {
56
+ clearInterval(interval);
57
+ };
58
+ }, [capture, delay]);
59
+ return (_jsx(Webcam, { width: width, height: height, ref: webcamRef, screenshotFormat: "image/jpeg", videoConstraints: videoConstraints || {
60
+ facingMode,
61
+ }, audio: false, onUserMediaError: onError }));
62
+ };
63
+ export default BarcodeScanner;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Enumerates barcode formats known to this package as strings. These need to be the same as BarcodeFormat from "@zing/library".
3
+ */
4
+ export declare enum BarcodeStringFormat {
5
+ /** Aztec 2D barcode format. */
6
+ AZTEC = "AZTEC",
7
+ /** CODABAR 1D format. */
8
+ CODABAR = "CODABAR",
9
+ /** Code 39 1D format. */
10
+ CODE_39 = "CODE_39",
11
+ /** Code 93 1D format. */
12
+ CODE_93 = "CODE_93",
13
+ /** Code 128 1D format. */
14
+ CODE_128 = "CODE_128",
15
+ /** Data Matrix 2D barcode format. */
16
+ DATA_MATRIX = "DATA_MATRIX",
17
+ /** EAN-8 1D format. */
18
+ EAN_8 = "EAN_8",
19
+ /** EAN-13 1D format. */
20
+ EAN_13 = "EAN_13",
21
+ /** ITF (Interleaved Two of Five) 1D format. */
22
+ ITF = "ITF",
23
+ /** MaxiCode 2D barcode format. */
24
+ MAXICODE = "MAXICODE",
25
+ /** PDF417 format. */
26
+ PDF_417 = "PDF_417",
27
+ /** QR Code 2D barcode format. */
28
+ QR_CODE = "QR_CODE",
29
+ /** RSS 14 */
30
+ RSS_14 = "RSS_14",
31
+ /** RSS EXPANDED */
32
+ RSS_EXPANDED = "RSS_EXPANDED",
33
+ /** UPC-A 1D format. */
34
+ UPC_A = "UPC_A",
35
+ /** UPC-E 1D format. */
36
+ UPC_E = "UPC_E",
37
+ /** UPC/EAN extension format. Not a stand-alone format. */
38
+ UPC_EAN_EXTENSION = "UPC_EAN_EXTENSION"
39
+ }
40
+ export default BarcodeStringFormat;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Enumerates barcode formats known to this package as strings. These need to be the same as BarcodeFormat from "@zing/library".
3
+ */
4
+ export var BarcodeStringFormat;
5
+ (function (BarcodeStringFormat) {
6
+ /** Aztec 2D barcode format. */
7
+ BarcodeStringFormat["AZTEC"] = "AZTEC";
8
+ /** CODABAR 1D format. */
9
+ BarcodeStringFormat["CODABAR"] = "CODABAR";
10
+ /** Code 39 1D format. */
11
+ BarcodeStringFormat["CODE_39"] = "CODE_39";
12
+ /** Code 93 1D format. */
13
+ BarcodeStringFormat["CODE_93"] = "CODE_93";
14
+ /** Code 128 1D format. */
15
+ BarcodeStringFormat["CODE_128"] = "CODE_128";
16
+ /** Data Matrix 2D barcode format. */
17
+ BarcodeStringFormat["DATA_MATRIX"] = "DATA_MATRIX";
18
+ /** EAN-8 1D format. */
19
+ BarcodeStringFormat["EAN_8"] = "EAN_8";
20
+ /** EAN-13 1D format. */
21
+ BarcodeStringFormat["EAN_13"] = "EAN_13";
22
+ /** ITF (Interleaved Two of Five) 1D format. */
23
+ BarcodeStringFormat["ITF"] = "ITF";
24
+ /** MaxiCode 2D barcode format. */
25
+ BarcodeStringFormat["MAXICODE"] = "MAXICODE";
26
+ /** PDF417 format. */
27
+ BarcodeStringFormat["PDF_417"] = "PDF_417";
28
+ /** QR Code 2D barcode format. */
29
+ BarcodeStringFormat["QR_CODE"] = "QR_CODE";
30
+ /** RSS 14 */
31
+ BarcodeStringFormat["RSS_14"] = "RSS_14";
32
+ /** RSS EXPANDED */
33
+ BarcodeStringFormat["RSS_EXPANDED"] = "RSS_EXPANDED";
34
+ /** UPC-A 1D format. */
35
+ BarcodeStringFormat["UPC_A"] = "UPC_A";
36
+ /** UPC-E 1D format. */
37
+ BarcodeStringFormat["UPC_E"] = "UPC_E";
38
+ /** UPC/EAN extension format. Not a stand-alone format. */
39
+ BarcodeStringFormat["UPC_EAN_EXTENSION"] = "UPC_EAN_EXTENSION";
40
+ })(BarcodeStringFormat || (BarcodeStringFormat = {}));
41
+ export default BarcodeStringFormat;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- import BarcodeScannerComponent from './BarcodeScannerComponent';
2
- export default BarcodeScannerComponent;
1
+ import BarcodeScanner from "./BarcodeScanner";
2
+ export { default as BarcodeStringFormat } from "./BarcodeStringFormat";
3
+ export { BarcodeFormat } from "@zxing/library";
4
+ export default BarcodeScanner;
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
- import BarcodeScannerComponent from './BarcodeScannerComponent';
2
- export default BarcodeScannerComponent;
1
+ import BarcodeScanner from "./BarcodeScanner";
2
+ export { default as BarcodeStringFormat } from "./BarcodeStringFormat";
3
+ export { BarcodeFormat } from "@zxing/library";
4
+ export default BarcodeScanner;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-qr-barcode-scanner",
3
- "version": "2.0.4",
3
+ "version": "2.1.1",
4
4
  "description": "A simple React Component using the client's webcam to read barcodes and QR codes.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,14 +0,0 @@
1
- import React from "react";
2
- import { Result } from "@zxing/library";
3
- export declare const BarcodeScannerComponent: ({ onUpdate, onError, width, height, facingMode, torch, delay, videoConstraints, stopStream, }: {
4
- onUpdate: (arg0: unknown, arg1?: Result) => void;
5
- onError?: (arg0: string | DOMException) => void;
6
- width?: number | string;
7
- height?: number | string;
8
- facingMode?: "environment" | "user";
9
- torch?: boolean;
10
- delay?: number;
11
- videoConstraints?: MediaTrackConstraints;
12
- stopStream?: boolean;
13
- }) => React.ReactElement;
14
- export default BarcodeScannerComponent;
@@ -1,64 +0,0 @@
1
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
- import React from "react";
3
- import { BrowserMultiFormatReader } from "@zxing/library";
4
- import Webcam from "react-webcam";
5
- export const BarcodeScannerComponent = ({ onUpdate, onError, width = "100%", height = "100%", facingMode = "environment", torch, delay = 500, videoConstraints, stopStream, }) => {
6
- const webcamRef = React.useRef(null);
7
- const capture = React.useCallback(() => {
8
- const codeReader = new BrowserMultiFormatReader();
9
- // @ts-ignore
10
- const imageSrc = webcamRef?.current?.getScreenshot();
11
- if (imageSrc) {
12
- codeReader
13
- .decodeFromImage(undefined, imageSrc)
14
- .then((result) => {
15
- onUpdate(null, result);
16
- })
17
- .catch((err) => {
18
- onUpdate(err);
19
- });
20
- }
21
- }, [onUpdate]);
22
- React.useEffect(() => {
23
- // Turn on the flashlight if prop is defined and device has the capability
24
- if (typeof torch === "boolean" &&
25
- // @ts-ignore
26
- navigator?.mediaDevices?.getSupportedConstraints().torch) {
27
- // @ts-ignore
28
- const stream = webcamRef?.current?.video.srcObject;
29
- const track = stream?.getVideoTracks()[0]; // get the active track of the stream
30
- if (track &&
31
- track.getCapabilities().torch &&
32
- !track.getConstraints().torch) {
33
- track
34
- .applyConstraints({
35
- advanced: [{ torch }],
36
- })
37
- .catch((err) => onUpdate(err));
38
- }
39
- }
40
- }, [torch, onUpdate]);
41
- React.useEffect(() => {
42
- if (stopStream) {
43
- // @ts-ignore
44
- let stream = webcamRef?.current?.video.srcObject;
45
- if (stream) {
46
- stream.getTracks().forEach((track) => {
47
- stream.removeTrack(track);
48
- track.stop();
49
- });
50
- stream = null;
51
- }
52
- }
53
- }, [stopStream]);
54
- React.useEffect(() => {
55
- const interval = setInterval(capture, delay);
56
- return () => {
57
- clearInterval(interval);
58
- };
59
- }, []);
60
- return (_jsx(_Fragment, { children: _jsx(Webcam, { width: width, height: height, ref: webcamRef, screenshotFormat: "image/jpeg", videoConstraints: videoConstraints || {
61
- facingMode,
62
- }, audio: false, onUserMediaError: onError }) }));
63
- };
64
- export default BarcodeScannerComponent;