2022-09-08 20:02:41 +02:00
|
|
|
|
|
|
|
<script>
|
|
|
|
import { Html5Qrcode } from 'html5-qrcode';
|
|
|
|
import { onMount } from 'svelte';
|
|
|
|
|
2022-09-08 21:49:35 +02:00
|
|
|
let codebar;
|
|
|
|
|
2022-09-08 20:02:41 +02:00
|
|
|
let scanning = false;
|
|
|
|
|
|
|
|
let html5Qrcode;
|
2022-09-08 21:49:35 +02:00
|
|
|
|
|
|
|
onMount(start);
|
|
|
|
|
2022-09-08 20:02:41 +02:00
|
|
|
function start() {
|
2022-09-08 21:49:35 +02:00
|
|
|
html5Qrcode = new Html5Qrcode('reader');
|
2022-09-08 20:02:41 +02:00
|
|
|
html5Qrcode.start(
|
|
|
|
{ facingMode: 'environment' },
|
|
|
|
{
|
|
|
|
fps: 10,
|
|
|
|
qrbox: { width: 250, height: 250 },
|
|
|
|
},
|
|
|
|
onScanSuccess,
|
|
|
|
onScanFailure
|
2022-09-15 21:01:10 +02:00
|
|
|
).catch((err) => {
|
|
|
|
console.debug("Could not start the scanner");
|
|
|
|
console.error(err);
|
|
|
|
onClose();
|
|
|
|
return;
|
|
|
|
});
|
2022-09-08 20:02:41 +02:00
|
|
|
scanning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stop() {
|
|
|
|
await html5Qrcode.stop();
|
|
|
|
scanning = false;
|
2022-09-08 21:49:35 +02:00
|
|
|
onClose();
|
2022-09-08 20:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onScanSuccess(decodedText, decodedResult) {
|
2022-09-08 21:49:35 +02:00
|
|
|
codebar = decodedText;
|
|
|
|
// alert(`Code matched = ${decodedText}`);
|
2022-09-08 20:02:41 +02:00
|
|
|
console.log(decodedResult);
|
2022-09-08 21:49:35 +02:00
|
|
|
onResult(codebar);
|
|
|
|
stop();
|
2022-09-08 20:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onScanFailure(error) {
|
|
|
|
console.warn(`Code scan error = ${error}`);
|
|
|
|
}
|
2022-09-08 21:49:35 +02:00
|
|
|
|
|
|
|
export let onClose, onResult;
|
2022-09-08 20:02:41 +02:00
|
|
|
</script>
|
|
|
|
|
2022-09-08 21:49:35 +02:00
|
|
|
<reader id="reader"/>
|
|
|
|
{#if scanning}
|
|
|
|
<button title="Stop barcode scanning" on:click={stop}>stop</button>
|
|
|
|
{:else}
|
|
|
|
<button title="Start barcode scanning" on:click={start}>start</button>
|
|
|
|
{/if}
|
|
|
|
|
2022-09-08 20:02:41 +02:00
|
|
|
<style>
|
|
|
|
reader {
|
|
|
|
width: 100%;
|
|
|
|
min-height: 500px;
|
|
|
|
background-color: black;
|
|
|
|
}
|
|
|
|
</style>
|