-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: QR Codes Scan & Pending Connections (#12)
* feat: qr code improvements * apply pending connection events * chore: hide import contacts for now
- Loading branch information
1 parent
b802781
commit bb55b98
Showing
10 changed files
with
114 additions
and
99 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
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
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
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 |
---|---|---|
@@ -1,66 +1,38 @@ | ||
<script lang="ts"> | ||
import { onDestroy } from 'svelte' | ||
import QR, { type QRCode } from 'jsqr' | ||
import { onMount } from 'svelte' | ||
import { Html5Qrcode, type Html5QrcodeResult } from 'html5-qrcode' | ||
import { replace, pop } from 'svelte-spa-router' | ||
import { Camera, CloseIcon, IconButton } from '@resplice/components' | ||
import { CloseIcon, IconButton } from '@resplice/components' | ||
let qrCode: QRCode | ||
let streamInterval: number | ||
async function handleQr(qrData: string | null) { | ||
if (!qrData) return | ||
const url = new URL(qrData) | ||
function getScanBox(width: number, height: number) { | ||
const constraint = Math.min(width, height) | ||
const size = Math.min(constraint - 48, 512) | ||
return { width: size, height: size } | ||
} | ||
async function onScan(data: string, _result: Html5QrcodeResult) { | ||
const url = new URL(data) | ||
replace(url.hash.replace('#', '')) | ||
} | ||
$: handleQr(qrCode?.data) | ||
async function onVideoStream(e: CustomEvent<HTMLVideoElement>) { | ||
// TODO: Look into Screen Wake Lock API | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API | ||
const stream = e.detail | ||
const canvasEl = document.createElement('canvas') | ||
const canvas = canvasEl.getContext('2d') | ||
onMount(() => { | ||
const scanner = new Html5Qrcode('camera', { formatsToSupport: [0], verbose: false }) | ||
// Maybe use requestAnimationFrame here instead of interval | ||
streamInterval = window.setInterval(async () => { | ||
if (qrCode) return | ||
if (stream.readyState !== stream.HAVE_ENOUGH_DATA) return | ||
const { videoWidth: width, videoHeight: height } = stream | ||
canvasEl.height = height | ||
canvasEl.width = width | ||
if (!canvas) return | ||
scanner.start( | ||
{ facingMode: 'environment' }, | ||
{ fps: 10, qrbox: getScanBox, aspectRatio: 0.5625 }, | ||
onScan, | ||
() => {} | ||
) | ||
canvas.drawImage(stream, 0, 0, width, height) | ||
// Might need to make these dimensions smaller for performance | ||
const imageData = canvas.getImageData(0, 0, width, height) | ||
const code = QR(imageData.data, imageData.width, imageData.height, { | ||
inversionAttempts: 'dontInvert' | ||
}) | ||
if (code) { | ||
clearInterval(streamInterval) | ||
qrCode = code | ||
} | ||
}, 500) | ||
} | ||
onDestroy(() => { | ||
clearInterval(streamInterval) | ||
return () => scanner.stop() | ||
}) | ||
</script> | ||
|
||
<main | ||
class="h-full w-full bg-zinc-800 rounded-t-3xl rounded-b-3xl flex-1 flex flex-col justify-center items-center" | ||
> | ||
<Camera hideControls on:stream={onVideoStream} /> | ||
<main class="relative h-full w-full bg-zinc-800 flex-1 flex flex-col justify-center items-center"> | ||
<div id="camera" class="w-full top-0 left-0" /> | ||
|
||
<div class="absolute bottom-0 z-10 flex items-center justify-center w-full p-4"> | ||
<IconButton Icon={CloseIcon} on:click={() => pop()} /> | ||
</div> | ||
|
||
<div | ||
class="border-4 border-brand-primary bg-transparent w-5/6 h-1/2 z-10 rounded-2xl flex justify-center items-center" | ||
/> | ||
</main> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1,79 @@ | ||
<script lang="ts"> | ||
import { onDestroy } from 'svelte' | ||
import { Camera, CloseIcon, IconButton } from '$lib' | ||
import { browser } from '$app/environment' | ||
let streamInterval: number | ||
async function onVideoStream(e: CustomEvent<HTMLVideoElement>) { | ||
const stream = e.detail | ||
const canvasEl = document.createElement('canvas') | ||
const canvas = canvasEl.getContext('2d') | ||
// Maybe use requestAnimationFrame here instead of interval | ||
streamInterval = window.setInterval(async () => { | ||
// if (qrCode) return | ||
if (stream.readyState !== stream.HAVE_ENOUGH_DATA) return | ||
const { videoWidth: width, videoHeight: height } = stream | ||
console.log(stream) | ||
canvasEl.height = height | ||
canvasEl.width = width | ||
if (!canvas) return | ||
canvas.drawImage(stream, 0, 0, width, height) | ||
// Might need to make these dimensions smaller for performance | ||
const imageData = canvas.getImageData(0, 0, width, height) | ||
console.log(imageData) | ||
// const code = QR(imageData.data, imageData.width, imageData.height, { | ||
// inversionAttempts: 'dontInvert' | ||
// }) | ||
// if (code) { | ||
// clearInterval(streamInterval) | ||
// qrCode = code | ||
// } | ||
}, 500) | ||
import { onMount } from 'svelte' | ||
import { Html5Qrcode, type Html5QrcodeResult } from 'html5-qrcode' | ||
import { CloseIcon, IconButton } from '$lib' | ||
function getScanBox(width: number, height: number) { | ||
const constraint = Math.min(width, height) | ||
const size = Math.min(constraint - 48, 512) | ||
return { width: size, height: size } | ||
} | ||
async function onScan(data: string, _result: Html5QrcodeResult) { | ||
console.log(data, _result) | ||
} | ||
onDestroy(() => { | ||
clearInterval(streamInterval) | ||
onMount(() => { | ||
const scanner = new Html5Qrcode('camera', { formatsToSupport: [0], verbose: false }) | ||
scanner.start( | ||
{ facingMode: 'environment' }, | ||
{ fps: 10, qrbox: getScanBox, aspectRatio: 0.5625 }, | ||
onScan, | ||
() => {} | ||
) | ||
return () => scanner.stop() | ||
}) | ||
// async function handleQr(qrData: string | null) { | ||
// if (!qrData) return | ||
// const url = new URL(qrData) | ||
// replace(url.hash.replace('#', '')) | ||
// } | ||
// $: handleQr(qrCode?.data) | ||
// async function onVideoStream(e: CustomEvent<HTMLVideoElement>) { | ||
// // TODO: Look into Screen Wake Lock API | ||
// // https://developer.mozilla.org/en-US/docs/Web/API/Screen_Wake_Lock_API | ||
// const stream = e.detail | ||
// const canvasEl = document.createElement('canvas') | ||
// const canvas = canvasEl.getContext('2d') | ||
// // Maybe use requestAnimationFrame here instead of interval | ||
// streamInterval = window.setInterval(async () => { | ||
// if (qrCode) return | ||
// if (stream.readyState !== stream.HAVE_ENOUGH_DATA) return | ||
// const { videoWidth: width, videoHeight: height } = stream | ||
// canvasEl.height = height | ||
// canvasEl.width = width | ||
// if (!canvas) return | ||
// canvas.drawImage(stream, 0, 0, width, height) | ||
// // Might need to make these dimensions smaller for performance | ||
// const imageData = canvas.getImageData(0, 0, width, height) | ||
// const code = QR(imageData.data, imageData.width, imageData.height, { | ||
// inversionAttempts: 'dontInvert' | ||
// }) | ||
// if (code) { | ||
// clearInterval(streamInterval) | ||
// qrCode = code | ||
// } | ||
// }, 500) | ||
// } | ||
</script> | ||
|
||
<main | ||
class="h-full w-full bg-zinc-800 rounded-t-3xl rounded-b-3xl flex-1 flex flex-col justify-center items-center" | ||
> | ||
{#if browser} | ||
<Camera hideControls on:stream={onVideoStream} /> | ||
{/if} | ||
<main class="relative h-full w-full bg-zinc-800 flex-1 flex flex-col justify-center items-center"> | ||
<div id="camera" class="w-full top-0 left-0" /> | ||
|
||
<div class="absolute bottom-0 z-10 flex items-center justify-center w-full p-4"> | ||
<IconButton Icon={CloseIcon} /> | ||
</div> | ||
|
||
<div | ||
<!-- <div | ||
class="border-4 border-brand-primary bg-transparent w-5/6 h-1/2 z-10 rounded-2xl flex justify-center items-center" | ||
/> | ||
/> --> | ||
</main> |