Created
November 30, 2021 20:20
-
-
Save cgutwin/529f80ed148b6336166094f256253a57 to your computer and use it in GitHub Desktop.
Quagga 2 Implementation in Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef, useState } from "react" | |
import styled from "styled-components" | |
import useScanner from "./useScanner" | |
import type { QuaggaJSResultObject } from "@ericblade/quagga2" | |
interface ScannerProps extends React.HTMLAttributes<HTMLDivElement> { | |
active: boolean | |
onResults: any | |
} | |
export default function Scanner({ active, onResults, ...rest }: ScannerProps) { | |
const [ isScanning, setIsScanning ] = useState<boolean>(true) | |
const scannerRef = useRef(null) | |
const onDetectedHandler = (results: QuaggaJSResultObject) => { | |
setIsScanning(false) | |
onResults(results) | |
} | |
// Injects the scanner into the ref. | |
useScanner(active, onDetectedHandler, () => null, scannerRef) | |
return isScanning ? <ScannerWrapper {...rest} ref={scannerRef}/> : null | |
} | |
const ScannerWrapper = styled.div` | |
max-height: 100vh; | |
overflow: hidden; | |
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Quagga, { QuaggaJSConfigObject } from "@ericblade/quagga2" | |
import { useEffect } from "react" | |
// todo: set correct types. | |
type useScannerTypes = { | |
readonly onError: any | |
readonly onDetected: any | |
scannerRef: any | |
} | |
export default function useScanner( | |
active: boolean, | |
onDetected: useScannerTypes["onDetected"], | |
onError: useScannerTypes["onError"], | |
scannerRef: useScannerTypes["scannerRef"]) { | |
useEffect(() => { | |
const config: QuaggaJSConfigObject = { | |
inputStream: { | |
constraints: { | |
// For some reason, the width and height fields affect their counterparts, so the below fixes that. | |
// noinspection JSSuspiciousNameCombination | |
width: window.outerHeight, | |
height: window.innerWidth, | |
facingMode: "environment" | |
}, | |
name: "Live", | |
target: scannerRef.current, | |
type: "LiveStream" | |
}, | |
locate: false, | |
frequency: 1, | |
decoder: { | |
readers: [ "upc_reader" ], | |
debug: { | |
drawBoundingBox: true, | |
showFrequency: true, | |
drawScanline: true, | |
showPattern: true | |
}, | |
multiple: false | |
} | |
} | |
Quagga.init({ ...config }) | |
.then(() => Quagga.start()) | |
.catch(err => onError(err)) | |
active && Quagga.onDetected(onDetected) | |
return () => { | |
Quagga.stop() | |
.catch(() => null) | |
Quagga.offDetected() | |
} | |
}, [ active, scannerRef ]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment