diff --git a/.gitignore b/.gitignore index 76add87..b512c09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -node_modules -dist \ No newline at end of file +node_modules \ No newline at end of file diff --git a/README.md b/README.md index e748bf0..6fce3de 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# React Webcam Barcode Scanner +# React QR Barcode Scanner This is a simple React component built in Typescript to provide a webcam-based barcode scanner using [react-webcam](https://github.com/mozmorris/react-webcam) and [@zxing/library](https://github.com/zxing-js/library). This component works on Computers and Mobile Devices (iOS 11 and above and Android Phones). diff --git a/dist/BarcodeScannerComponent.d.ts b/dist/BarcodeScannerComponent.d.ts new file mode 100644 index 0000000..4a32183 --- /dev/null +++ b/dist/BarcodeScannerComponent.d.ts @@ -0,0 +1,11 @@ +import React from "react"; +import { Result } from "@zxing/library"; +declare const BarcodeScannerComponent: ({ onUpdate, width, height, facingMode, delay, videoConstraints, }: { + onUpdate: (arg0: unknown, arg1?: Result) => void; + width?: number | string; + height?: number | string; + facingMode?: "environment" | "user"; + delay?: number; + videoConstraints?: MediaTrackConstraints; +}) => React.ReactElement; +export default BarcodeScannerComponent; diff --git a/dist/BarcodeScannerComponent.js b/dist/BarcodeScannerComponent.js new file mode 100644 index 0000000..c234108 --- /dev/null +++ b/dist/BarcodeScannerComponent.js @@ -0,0 +1,36 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const react_1 = __importDefault(require("react")); +const library_1 = require("@zxing/library"); +const react_webcam_1 = __importDefault(require("react-webcam")); +const BarcodeScannerComponent = ({ onUpdate, width = "100%", height = "100%", facingMode = "environment", delay = 500, videoConstraints, }) => { + const webcamRef = react_1.default.useRef(null); + const codeReader = new library_1.BrowserMultiFormatReader(); + const capture = react_1.default.useCallback(() => { + var _a; + const imageSrc = (_a = webcamRef === null || webcamRef === void 0 ? void 0 : webcamRef.current) === null || _a === void 0 ? void 0 : _a.getScreenshot(); + if (imageSrc) { + codeReader + .decodeFromImage(undefined, imageSrc) + .then((result) => { + onUpdate(null, result); + }) + .catch((err) => { + onUpdate(err); + }); + } + }, [codeReader, onUpdate]); + react_1.default.useEffect(() => { + const interval = setInterval(capture, delay); + return () => { + clearInterval(interval); + }; + }, []); + return (react_1.default.createElement(react_webcam_1.default, { width: width, height: height, ref: webcamRef, screenshotFormat: "image/jpeg", videoConstraints: videoConstraints || { + facingMode, + }, audio: false })); +}; +exports.default = BarcodeScannerComponent; diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..d5b86eb --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,2 @@ +import BarcodeScannerComponent from './BarcodeScannerComponent'; +export default BarcodeScannerComponent; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..6ad57e1 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,7 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const BarcodeScannerComponent_1 = __importDefault(require("./BarcodeScannerComponent")); +exports.default = BarcodeScannerComponent_1.default; diff --git a/package.json b/package.json index e6b11d1..89a255b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-qr-barcode-scanner", - "version": "1.0.3", + "version": "1.0.4", "description": "A simple React Component using the client's webcam to read barcodes and QR codes.", "main": "dist/index.js", "types": "dist/index.d.ts",