Open trasherdk opened 2 years ago
And another cersion
<div id="reader"></div>
<script>
import { onDestroy, onMount } from 'svelte'
import {Html5Qrcode as Scanner, Html5QrcodeSupportedFormats as ScannerFormats} from "html5-qrcode"
// set camera label you want here if you have more than one
const docCameraLabel = 'blah blah'
// expose barcode data to other components
let barcodeData = ''
async function getQRData (scanner, deviceID) {
return new Promise((resolve, reject) => {
return scanner.start(deviceID, {
fps: 10,
}, (decodedText, decodedResult) => {
resolve(decodedText)
}, (msg, err) => {
if (msg.indexOf("NotFoundException") >= 0) {
return
}
reject(err)
})
})
}
async function getCamera () {
const scanner = new Scanner("reader", {
formatsToSupport: [
ScannerFormats.QR_CODE,
ScannerFormats.PDF_417,
]
}, false);
try {
const videoInputDevices = await Scanner.getCameras()
for (let dev of videoInputDevices) {
// you can log the device label here to see what to use above,
// or just use the first one, etc.
// console.log(dev.label)
if (dev.label === docCameraLabel) {
const data = await getQRData(scanner, dev.id)
scanner.stop()
return data
}
}
} catch (err) {
scanner.stop()
throw err
}
}
onMount(() => {
getCamera().then(data => {
console.log(data)
barcodeData = data
}, console.error)
})
onDestroy(() => {
scanner.stop()
})
</script>
<style>
</style>
Source: https://github.com/mebjas/html5-qrcode/issues/354#issuecomment-1119490569 Fork: trasherdk/qrcode-scanner