QRdx

QR detection

Read and decode QR codes from images in the browser.

Read and decode QR codes from uploaded images.

Basic Detection

import { handleImageLoad } from 'qrdx';

async function detectQR(file: File) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = async (e) => {
      const img = new Image();
      img.onload = async () => {
        try {
          const result = await handleImageLoad(img, 400, false);
          if (result === "NO_QR") {
            resolve("No QR code found");
          } else {
            resolve(result);
          }
        } catch (error) {
          reject(error);
        }
      };
      img.src = e.target?.result as string;
    };
    reader.readAsDataURL(file);
  });
}

React Component

import { useState } from 'react';
import { handleImageLoad } from 'qrdx';

export default function QRDetector() {
  const [result, setResult] = useState('');

  const handleFile = async (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (!file) return;

    try {
      const qrData = await detectQR(file);
      setResult(qrData);
    } catch (error) {
      setResult(`Error: ${error.message}`);
    }
  };

  return (
    <div>
      <input type="file" accept="image/*" onChange={handleFile} />
      {result && <p>{result}</p>}
    </div>
  );
}

Detection Parameters

handleImageLoad(
  imgElement: HTMLImageElement,
  size: number,              // Processing size (recommended: 400)
  returnFullData?: boolean   // Return full data or just text
)

Size Parameter

  • 200-400 — Faster, good for clear images
  • 600+ — Better for small/damaged QR codes
  • Recommended — 400 for most cases

Full Data Response

Get complete detection information:

const fullData = await handleImageLoad(img, 400, true);
// Returns:
// {
//   data: "https://example.com",
//   binaryData: Uint8ClampedArray,
//   location: { topLeftCorner, topRightCorner, ... },
//   chunks: [...]
// }

Debug Visualization

Download images with bounding boxes:

import { downloadImageWithBoundingBox } from 'qrdx';

const fullData = await handleImageLoad(img, 400, true);
if (fullData?.location) {
  const boundingBox = [
    fullData.location.topLeftCorner,
    fullData.location.topRightCorner,
    fullData.location.bottomRightCorner,
    fullData.location.bottomLeftCorner,
  ];
  downloadImageWithBoundingBox(img, boundingBox, "debug.png");
}

Error Handling

try {
  const result = await handleImageLoad(img, 400, false);
  if (result === "NO_QR") {
    console.log("No QR code found");
  } else {
    console.log("QR data:", result);
  }
} catch (error) {
  console.error("Detection failed:", error);
}

When to Use

  • Process uploaded QR code images
  • Extract data from screenshots
  • Build QR code scanners
  • Verify QR code contents
  • Debug QR code issues

Browser Requirements

  • FileReader API
  • HTMLCanvasElement
  • ImageData and getImageData
  • ES6+ features

Next Steps

Last updated on

On this page