react-qr-barcode-scanner 2.0.3 → 2.1.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/README.md +10 -4
- package/dist/BarcodeScanner.d.ts +68 -0
- package/dist/BarcodeScanner.js +63 -0
- package/dist/BarcodeStringFormat.d.ts +40 -0
- package/dist/BarcodeStringFormat.js +41 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/package.json +1 -1
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
|
|
22
|
-
import
|
|
21
|
+
import { useState } from "react";
|
|
22
|
+
import BarcodeScanner from "react-qr-barcode-scanner";
|
|
23
23
|
|
|
24
24
|
function App() {
|
|
25
|
-
const [data, setData] =
|
|
25
|
+
const [data, setData] = useState("Not Found");
|
|
26
26
|
|
|
27
27
|
return (
|
|
28
28
|
<>
|
|
29
|
-
<
|
|
29
|
+
<BarcodeScanner
|
|
30
30
|
width={500}
|
|
31
31
|
height={500}
|
|
32
32
|
onUpdate={(err, result) => {
|
|
@@ -98,6 +98,12 @@ Type: `boolean`, Optional
|
|
|
98
98
|
|
|
99
99
|
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
100
|
|
|
101
|
+
### formats
|
|
102
|
+
|
|
103
|
+
Type: `BarcodeFormat[] | BarcodeStringFormat[]`, Optional
|
|
104
|
+
|
|
105
|
+
Array of barcode formats to decode. If not provided, all formats will be used. A smaller list may improve the speed of the scan.
|
|
106
|
+
|
|
101
107
|
## Supported Barcode Formats
|
|
102
108
|
|
|
103
109
|
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
ADDED
package/dist/index.js
ADDED
package/package.json
CHANGED