Using the Svelte library, you can easily read QR codes from a webcam or mobile camera while maintaining control over camera operations. This method relies on the CDN version of the jsQR.js library for QR code decoding. Alternatively, you can provide the URL path for your own 'jsQR.min.js' file.
npm install @cloudparker/easy-qrcode-reader-svelte --save-dev
<script lang="ts">
import EasyQrcodeReader from '$lib';
import { onMount } from 'svelte';
let qrcodeReaderRef: EasyQrcodeReader;
let clientWidth: number;
const handleQrcode = (ev: CustomEvent) => {
let data = ev.detail;
if (data) {
console.log(data);
//TODO: Handle the code and resume or close the camera
qrcodeReaderRef.pause();
setTimeout(() => {
qrcodeReaderRef.resume();
}, 15000);
}
};
onMount(() => {
return () => {
qrcodeReaderRef && qrcodeReaderRef.close();
};
});
</script>
<div class="camera-container" bind:clientWidth>
<EasyQrcodeReader bind:this={qrcodeReaderRef} width={clientWidth} on:qrcode={handleQrcode} />
</div>
<style>
.camera-container {
display: flex;
align-items: center;
justify-content: center;
}
</style>
required
Width of the camera view, height can be adjusted automatically.
default: https://cdn.jsdelivr.net/npm/[email protected]/dist/jsQR.min.js
Update your jsQR.js
library path.
default: true
Automatically start the camera and scan qrcode
It will triggers when a qrcode scanned succesfully. it return the qrdcode data in sting format.
Open camera for scan qrcode.
Close camera.
Pause camera.
Resume camera.
Capture image and return a promise with base64 image data. Also it will trigger on:image event when image captured.