qrDecode.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * QR decoding for the spool scanner (#1574): the platform BarcodeDetector where
  3. * there is one that reads QR codes, jsQR otherwise.
  4. *
  5. * Nothing here throws or rejects for an unreadable frame or photo -- that is a
  6. * frame or photo without a QR -- so a failure can't stop the live scan loop.
  7. */
  8. import jsQR from 'jsqr';
  9. // BarcodeDetector (platform QR decoder, e.g. Android Chrome) isn't in the TS DOM
  10. // lib yet — minimal shape for the bit we use.
  11. interface DetectedBarcode {
  12. rawValue: string;
  13. }
  14. export interface QrDetector {
  15. detect(source: CanvasImageSource): Promise<DetectedBarcode[]>;
  16. }
  17. type BarcodeDetectorCtor = new (opts?: { formats?: string[] }) => QrDetector;
  18. /**
  19. * The platform QR decoder, or null when there is none or it can't read QR
  20. * codes: its constructor throws for a format the platform doesn't support.
  21. */
  22. export function createQrDetector(): QrDetector | null {
  23. const BD = (globalThis as unknown as { BarcodeDetector?: BarcodeDetectorCtor }).BarcodeDetector;
  24. if (!BD) return null;
  25. try {
  26. return new BD({ formats: ['qr_code'] });
  27. } catch {
  28. return null;
  29. }
  30. }
  31. /**
  32. * Draw a frame/bitmap onto a canvas and decode a QR with jsQR. A canvas that
  33. * can't be drawn or read (jsdom without the canvas package, a video with no
  34. * frame yet) yields undefined. The caller owns the canvas so the live loop can
  35. * reuse one across frames.
  36. */
  37. export function decodeCanvas(
  38. canvas: HTMLCanvasElement,
  39. source: CanvasImageSource,
  40. w: number,
  41. h: number,
  42. inversionAttempts: 'dontInvert' | 'attemptBoth',
  43. ): string | undefined {
  44. if (w <= 0 || h <= 0) return undefined;
  45. try {
  46. canvas.width = w;
  47. canvas.height = h;
  48. const ctx = canvas.getContext('2d', { willReadFrequently: true });
  49. if (!ctx) return undefined;
  50. ctx.drawImage(source, 0, 0, w, h);
  51. const img = ctx.getImageData(0, 0, w, h);
  52. return jsQR(img.data, w, h, { inversionAttempts })?.data ?? undefined;
  53. } catch {
  54. return undefined;
  55. }
  56. }
  57. /** jsQR's first, fast read of a photo: its long side scaled down to this. */
  58. const PHOTO_FAST_SIDE = 1600;
  59. /**
  60. * The second read, at the photo's own size up to this long side. iOS Safari
  61. * refuses a canvas over about 16.7 MP, which a 24 MP photo would be.
  62. */
  63. const PHOTO_FULL_SIDE = 4096;
  64. function scaledTo(bitmap: ImageBitmap, maxSide: number): [number, number] {
  65. const scale = Math.min(1, maxSide / Math.max(bitmap.width, bitmap.height));
  66. return [Math.max(1, Math.round(bitmap.width * scale)), Math.max(1, Math.round(bitmap.height * scale))];
  67. }
  68. /**
  69. * Decode a QR from a captured photo. The photo is taken with the phone's native
  70. * camera (file input + capture), which focuses far better than an in-page
  71. * getUserMedia preview. Prefer the platform decoder; fall back to jsQR, first
  72. * on the photo scaled down for speed, then, if that finds nothing, near its
  73. * own size: a small label in the photo has too few pixels per module left
  74. * after the downscale (#3616). Rejects only when the image itself can't be
  75. * opened.
  76. */
  77. export async function decodeImageBlob(blob: Blob): Promise<string | undefined> {
  78. const bitmap = await createImageBitmap(blob, { imageOrientation: 'from-image' });
  79. try {
  80. const detector = createQrDetector();
  81. if (detector) {
  82. try {
  83. const codes = await detector.detect(bitmap);
  84. if (codes[0]?.rawValue) return codes[0].rawValue;
  85. } catch {
  86. /* the platform decoder failed on this image — jsQR gets a go */
  87. }
  88. }
  89. const canvas = document.createElement('canvas');
  90. const [w, h] = scaledTo(bitmap, PHOTO_FAST_SIDE);
  91. const fast = decodeCanvas(canvas, bitmap, w, h, 'attemptBoth');
  92. if (fast) return fast;
  93. const [fw, fh] = scaledTo(bitmap, PHOTO_FULL_SIDE);
  94. // A photo no larger than the first read has nothing more to give.
  95. if (fw === w && fh === h) return undefined;
  96. const full = decodeCanvas(canvas, bitmap, fw, fh, 'attemptBoth');
  97. // iOS Safari counts a canvas's memory until it is resized, not until it
  98. // is collected: give the 50 MB back now, for the next photo.
  99. canvas.width = 0;
  100. canvas.height = 0;
  101. return full;
  102. } finally {
  103. bitmap.close?.();
  104. }
  105. }
  106. /**
  107. * A decoder for the live preview's frames. It starts on the platform decoder
  108. * and drops it for jsQR the first time it throws or rejects. The returned
  109. * promise never rejects, so the loop waiting on it always gets to its next
  110. * frame.
  111. */
  112. export function createFrameDecoder(): (video: HTMLVideoElement, canvas: HTMLCanvasElement) => Promise<string | undefined> {
  113. let detector = createQrDetector();
  114. return async (video, canvas) => {
  115. if (detector) {
  116. try {
  117. const codes = await detector.detect(video);
  118. return codes[0]?.rawValue;
  119. } catch {
  120. detector = null;
  121. }
  122. }
  123. // 'dontInvert' (cheapest) for the hot loop; the photo path uses 'attemptBoth'.
  124. return decodeCanvas(canvas, video, video.videoWidth, video.videoHeight, 'dontInvert');
  125. };
  126. }