← Writing

How to Build a QR Code Scanner with a PWA

PWA · October 27, 2026
How to Build a QR Code Scanner with a PWA
This series started with what a PWA is. Since then I’ve covered vibration, motion sensors and saving GPS locations. Now: a QR code scanner that runs in the browser, keeps a table of every code it reads, and exports the list to a text file. What it does Opens the rear camera and scans continuously. Saves each code with an ID, the date and the content. Shows everything in a table, newest first. Exports the table as a .txt file. Step 1: open the camera
const stream = await navigator.mediaDevices.getUserMedia({
  video: { facingMode: "environment" },
  audio: false
});
video.srcObject = stream;
await video.play();
Call this from a button tap, not on page load. Chrome asks for permission the first time. Step 2: detect the QR code Chrome on Android has a built-in QR detector, so you don’t need a library:
const detector = new BarcodeDetector({ formats: ["qr_code"] });

async function scan() {
  const found = await detector.detect(video);
  if (found.length) save(found[0].rawValue);
  setTimeout(scan, 200); // about 5 scans per second
}
Not every browser has BarcodeDetector. As a backup I added jsQR, a small library that decodes frames drawn on a canvas:
ctx.drawImage(video, 0, 0, w, h);
const img = ctx.getImageData(0, 0, w, h);
const code = jsQR(img.data, w, h);
if (code) save(code.data);
The app uses the built-in detector when it exists and jsQR when it doesn’t. Step 3: save each code Like the GPS project, the records go in IndexedDB, the database built into every browser. They stay on the phone after you close the app.
const db = await openDB(); // object store with autoIncrement id
const tx = db.transaction("codes", "readwrite");
tx.objectStore("codes").add({ timestamp: Date.now(), content: value });
The id column is generated automatically. One detail that matters: if the camera keeps looking at the same QR, you don’t want 50 copies. The app ignores the same value if it was seen in the last 3 seconds.
if (value === lastValue && Date.now() - lastSeen < 3000) return;
Step 4: download the .txt
const lines = records.map(r =>
  `${r.id}\t${new Date(r.timestamp).toLocaleString()}\t${r.content}`
);
const blob = new Blob(["ID\tDate\tQR content\n" + lines.join("\n")],
                      { type: "text/plain" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "qr-codes.txt";
a.click();
What you need to know HTTPS is required for camera access. QR content is untrusted. Anyone can print a QR with any text, so show it with textContent, never innerHTML. The app never opens the link inside a code. It only records the text, which is safer than following it blindly. Everything stays on the phone. Nothing is sent to a server. Clearing the site’s data in Chrome also deletes the saved codes, because IndexedDB lives there. What’s next Next in the series: reading NFC tags from a PWA.
#html #js #pwa #web