react-qr-barcode-scanner 2.0.2 → 2.0.4
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/dist/BarcodeScannerComponent.d.ts +14 -0
- package/dist/BarcodeScannerComponent.js +64 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +2 -2
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
@@ -0,0 +1,64 @@
|
|
|
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;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-qr-barcode-scanner",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"author": "Jamena McInteer (https://jamena.dev)",
|