-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement badge scanner with html5-qrcode
- Still some weird `useEffect` issues with scanner opening twice
- Loading branch information
Showing
2 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useEffect } from "react"; | ||
|
||
import { | ||
Html5QrcodeScanner, | ||
QrcodeErrorCallback, | ||
QrcodeSuccessCallback, | ||
} from "html5-qrcode"; | ||
|
||
const scannerRegionId = "badge-scanner-full-region"; | ||
|
||
interface BadgeScannerProps { | ||
fps?: number; | ||
qrbox?: number; | ||
aspectRatio?: number; | ||
disableFlip?: boolean; | ||
verbose?: boolean; | ||
onSuccess: QrcodeSuccessCallback; | ||
onError: QrcodeErrorCallback; | ||
} | ||
|
||
function BadgeScanner(props: BadgeScannerProps) { | ||
useEffect(() => { | ||
const { | ||
fps, | ||
qrbox, | ||
aspectRatio, | ||
disableFlip, | ||
verbose, | ||
onSuccess, | ||
onError, | ||
} = props; | ||
|
||
const html5QrcodeScanner = new Html5QrcodeScanner( | ||
scannerRegionId, | ||
{ | ||
fps: fps ?? 5, | ||
qrbox: qrbox ?? 300, | ||
aspectRatio: aspectRatio ?? 1, | ||
disableFlip: disableFlip ?? true, | ||
}, | ||
verbose ?? false, | ||
); | ||
html5QrcodeScanner.render(onSuccess, onError); | ||
|
||
// Clean up when unmount | ||
return () => { | ||
html5QrcodeScanner.clear().catch((error) => { | ||
console.error("Failed to clear html5QrcodeScanner. ", error); | ||
}); | ||
}; | ||
}, [props]); | ||
|
||
return <div id={scannerRegionId} />; | ||
} | ||
|
||
export default BadgeScanner; |