-
Notifications
You must be signed in to change notification settings - Fork 41
fix(frontend): Improve QR scanning reliability for dark borderless codes #12162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AntonioVentilii
merged 5 commits into
main
from
fix-frontend/Improve-QR-scanning-reliability-for-dark-borderless-codes
Mar 25, 2026
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ead4d8
fix(frontend): Improve QR scanning reliability for dark borderless codes
AntonioVentilii 8ce6624
package
AntonioVentilii b629dcd
rename
AntonioVentilii 32c6e94
Merge branch 'main' into fix-frontend/Improve-QR-scanning-reliability…
AntonioVentilii 81109c9
Merge branch 'main' into fix-frontend/Improve-QR-scanning-reliability…
AntonioVentilii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,160 @@ | ||
| <script lang="ts"> | ||
| import { isNullish, nonNullish } from '@dfinity/utils'; | ||
| import { createEventDispatcher, onDestroy, onMount } from 'svelte'; | ||
| import { isDesktop } from '$lib/utils/device.utils'; | ||
| import { nextElementId } from '$lib/utils/html.utils'; | ||
|
|
||
| const id = nextElementId('qrcode-reader-'); | ||
|
|
||
| const dispatch = createEventDispatcher(); | ||
|
|
||
| let videoElement: HTMLVideoElement | undefined; | ||
| let stream: MediaStream | undefined; | ||
| let scanInterval: NodeJS.Timeout | undefined; | ||
| let isDestroyed = false; | ||
| let isProcessingFrame = false; | ||
|
|
||
| onMount(async () => { | ||
| try { | ||
| stream = await navigator.mediaDevices.getUserMedia({ | ||
| video: { | ||
| facingMode: 'environment', | ||
| width: { ideal: 1920 }, | ||
| height: { ideal: 1080 } | ||
| }, | ||
| audio: false | ||
| }); | ||
|
|
||
| if (isDestroyed) { | ||
| stopStream(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (nonNullish(videoElement)) { | ||
| videoElement.srcObject = stream; | ||
|
|
||
| await videoElement.play(); | ||
|
|
||
| await startScanning(); | ||
| } | ||
| } catch (err: unknown) { | ||
| dispatch('nnsQRCodeError', err); | ||
| } | ||
| }); | ||
|
|
||
| const startScanning = async () => { | ||
| try { | ||
| const { BarcodeDetector } = await import('barcode-detector/ponyfill'); | ||
|
|
||
| if (isDestroyed) { | ||
| return; | ||
| } | ||
|
|
||
| const detector = new BarcodeDetector({ | ||
| formats: ['qr_code'] | ||
| }); | ||
|
|
||
| const scan = async () => { | ||
| if ( | ||
| isProcessingFrame || | ||
| isDestroyed || | ||
| isNullish(videoElement) || | ||
| videoElement.readyState < HTMLMediaElement.HAVE_CURRENT_DATA | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| isProcessingFrame = true; | ||
|
|
||
| try { | ||
| const results = await detector.detect(videoElement); | ||
|
|
||
| const [qrResult] = results; | ||
|
|
||
| if (nonNullish(qrResult)) { | ||
| dispatch('nnsQRCode', qrResult.rawValue); | ||
| } | ||
| } catch { | ||
| // Decoding failed on this frame — expected when no QR code is visible | ||
| } finally { | ||
| isProcessingFrame = false; | ||
| } | ||
| }; | ||
|
|
||
| scanInterval = setInterval(scan, 100); | ||
| } catch (err: unknown) { | ||
| dispatch('nnsQRCodeError', err); | ||
| } | ||
| }; | ||
|
|
||
| const stopStream = () => { | ||
| if (nonNullish(stream)) { | ||
| stream.getTracks().forEach((track) => track.stop()); | ||
|
|
||
| stream = undefined; | ||
| } | ||
| }; | ||
|
|
||
| onDestroy(() => { | ||
| isDestroyed = true; | ||
|
|
||
| if (nonNullish(scanInterval)) { | ||
| clearInterval(scanInterval); | ||
|
|
||
| scanInterval = undefined; | ||
| } | ||
|
|
||
| stopStream(); | ||
| }); | ||
|
|
||
| // We optimistically assume that if the QR code reader is used on desktop, it has most probably only a single "user" facing camera and that we can invert the displayed video | ||
| const mirror = isDesktop(); | ||
DenysKarmazynDFINITY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </script> | ||
|
|
||
| <article {id} class="reader" class:mirror> | ||
| <video bind:this={videoElement} autoplay muted playsinline></video> | ||
|
|
||
| <div class="scan-overlay"> | ||
| <div class="scan-region"></div> | ||
| </div> | ||
| </article> | ||
|
|
||
| <style lang="scss"> | ||
| .reader { | ||
| position: relative; | ||
| width: 100%; | ||
| height: 100%; | ||
|
|
||
| border-radius: var(--border-radius); | ||
| overflow: hidden; | ||
|
|
||
| &.mirror { | ||
| transform: scaleX(-1); | ||
| } | ||
| } | ||
|
|
||
| video { | ||
| display: block; | ||
| width: 100%; | ||
| height: 100%; | ||
| object-fit: cover; | ||
| } | ||
|
|
||
| .scan-overlay { | ||
| position: absolute; | ||
| inset: 0; | ||
| container-type: size; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| .scan-region { | ||
| width: min(90cqw, 90cqh); | ||
| aspect-ratio: 1; | ||
| box-shadow: 0 0 0 9999px white; | ||
| border: 2px solid rgba(var(--primary-rgb), 0.4); | ||
| } | ||
| </style> | ||
This file contains hidden or 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 hidden or 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,10 @@ | ||
| let elementsCounters: Record<string, number> = {}; | ||
|
|
||
| export const nextElementId = (prefix: string): string => { | ||
| elementsCounters = { | ||
| ...elementsCounters, | ||
| [prefix]: (elementsCounters[prefix] ?? 0) + 1 | ||
| }; | ||
|
|
||
| return `${prefix}${elementsCounters[prefix]}`; | ||
AntonioVentilii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.