Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

QR code notes (ISO/IEC 18004)

================================================================================
 QR CODE
 Encoder, decoder and picture detector to ISO/IEC 18004
 (StreamSec.QR.Encode / .Decode / .Image / .Image.VCL / .Image.FMX)
 (StreamSec Security Library for Embarcadero Delphi)
================================================================================

Generated: 2026-09-19  (First edition.)

A QR encoder, a decoder, and a detector that finds a symbol in a photograph.
Versions 1 to 40, all four error-correction levels. The three lower units are
framework-free and carry all of the difficulty; the two adapters are thin, one
per framework, and exist so that a bitmap you already have can go in and come
out without you writing the conversion.

Companion to nothing else in the library. No other subsystem consumes this one,
and it consumes nothing but SysUtils and the framework unit its adapter names.

--------------------------------------------------------------------------------
0. SUMMARY
--------------------------------------------------------------------------------

Five units. Four are in the StrSecIV runtime packages (170 through 370, i.e.
XE3 through Delphi 13); the FireMonkey adapter is deliberately not - see
section 6. No design-time registration: nothing here is a component. It is a
set of functions and records called from code.

  text or octets  --QrEncode-->  tQrCode  --QrDrawCode-->   a bitmap
  a bitmap  --QrReadBitmap-->  tQrDecoded  --AsText-->      text

Drawing matters as much as reading, and it is listed first on purpose: a
program usually needs to put a symbol on screen before it ever needs to read
one back.

WHAT THIS IS NOT. It is not a barcode library: no Code 128, no Data Matrix, no
Aztec, no PDF417, and no Micro QR. It is not a camera or capture layer either -
you supply the bitmap, from wherever you get bitmaps. And a QR code is an
encoding, NOT a protection: anything you put in one is readable by anyone who
photographs it. If the payload needs to be secret or authentic, encrypt or
sign it with the rest of this library first and put the result in the symbol.

================================================================================
1. UNIT INVENTORY
================================================================================

  StreamSec.QR.Encode    Octets to a module matrix. The version and mask
                         selection, the Reed-Solomon codewords, and the
                         geometry tables the decoder also reads. Framework
                         free.

  StreamSec.QR.Decode    A module matrix back to the octets that were
                         encoded, with the error correction actually applied
                         rather than assumed. Framework free.

  StreamSec.QR.Image     A grey raster to a module matrix: locating the
                         symbol in a picture, binarising it, and sampling it.
                         This is where the difficulty is. Framework free, and
                         it is the unit to call if you have pixels from a
                         source neither adapter covers.

  StreamSec.QR.Image.VCL Adapter: TBitmap and TGraphic in and out.

  StreamSec.QR.Image.FMX Adapter: the FireMonkey TBitmap in and out. NOT in
                         the runtime packages; see section 6.

Dependency direction is one way: Encode knows nothing of Decode or Image, and
neither adapter is known to anything below it.

================================================================================
2. STANDARDS MAP
================================================================================

  ISO/IEC 18004        Everything. Versions 1 to 40, error-correction levels
                       L, M, Q and H.
                       StreamSec.QR.Encode, StreamSec.QR.Decode

  ISO/IEC 18004        The block and error-correction tables, transcribed
  tables 13 to 22      once and published so the decoder derives its geometry
                       from the same source as the encoder rather than from a
                       second transcription.
                       StreamSec.QR.Encode

There is no separate standard for finding a symbol in a photograph. The
detector in StreamSec.QR.Image is an implementation of the usual approach and
is not specified by ISO/IEC 18004, which describes the symbol and not how to
see one.

================================================================================
3. ENCODING  (StreamSec.QR.Encode)
================================================================================

  tQrEcLevel             (qrEcL, qrEcM, qrEcQ, qrEcH) - the four levels of the
                         standard, in the standard's own order.
  tQrModules             TArray<Boolean>, one entry per module, row major.
  tQrCode                The encoded symbol. Dark(x, y) asks whether a module
                         is dark; AsLines renders it to text, with the strings
                         used for a dark and a light module as parameters, so
                         a console or a memo can show it without a bitmap.
  eQrError               The subsystem's exception root.
  eQrEncode              Raised when the data will not fit the version and
                         level asked for.

  QrEncode               Octets to a tQrCode, at the smallest version that
                         will hold them or at one you name.
  QrEncodeText           The same for a string, encoded as UTF-8.
  QrCapacity             How many octets a version and level will hold.
  QrVersionOfSize        The version a module count implies. Returns 0 rather
                         than raising when the size is not a legal one.
  cQrMinVersion          1 and 40. A version outside them is not a QR code.
  cQrMaxVersion

Published for the decoder, and useful if you are rendering by hand:

  QrFunctionModules      Which modules are structure rather than payload.
  QrEcCodewordsPerBlock  The block geometry of a version and level.
  QrEcBlocks
  QrTotalCodewords

NO QUIET ZONE IS ADDED. The standard's four-module light border around the
symbol is the renderer's business, and a renderer that forgets it produces a
code no reader will see - including this one. QrRenderImage and both adapters'
QrDrawCode add it for you; if you draw the modules yourself, you must.

================================================================================
4. DECODING  (StreamSec.QR.Decode)
================================================================================

  tQrDecoded             The result. AsText returns the payload as a string.
  eQrDecode              Raised when the matrix is not a readable symbol.
  eQrUnreadable          A symbol was found and is too damaged to correct.
                         Descends from eQrDecode.

  QrDecode               A module matrix and its size back to the payload.
  QrDecodeCode           The same from a tQrCode, which is what you have if
                         you encoded it.

THE ERROR CORRECTION IS APPLIED, NOT ASSUMED. A symbol within its level's
correction capacity decodes; one past it raises eQrUnreadable rather than
returning a payload that is quietly wrong. That distinction is the reason to
prefer this over reading the modules yourself.

================================================================================
5. FROM A PICTURE  (StreamSec.QR.Image)
================================================================================

  tQrGrayImage           A grey raster. Init(width, height) sizes it and
                         Gray(x, y) reads a pixel.
  tQrLocation            Where in the picture the symbol was found.
  eQrImage               Raised for a picture that cannot be worked with.
                         Descends from eQrError.
  eQrNotFound            There is no symbol in the picture. Descends from
                         eQrImage - so a handler for eQrImage catches it, and
                         one for eQrError catches everything in the subsystem.

  QrReadImage            Grey raster to payload. The second overload also
                         reports where the symbol was.
  QrLocateImage          Find the symbol without decoding it.
  QrBinarise             The raster to one Boolean per PIXEL - the array is
                         Width * Height entries, in the raster's own
                         dimensions. It is NOT a module matrix, and indexing
                         it as one is the mistake to avoid; sampling pixels
                         down to modules is what QrReadImage does after this.
  QrRenderImage          A tQrCode, or a module array and its size, to a grey
                         raster, at a module size you choose. The quiet zone
                         is included.

THE PICTURE USUALLY ARRIVES AS A FILE OR FROM THE CLIPBOARD rather than as a
camera frame, and the detector is written for that: a still, roughly square
on, with whatever lighting the photograph had.

================================================================================
6. THE TWO ADAPTERS, AND WHY ONE IS NOT IN THE PACKAGES
================================================================================

  StreamSec.QR.Image.VCL   QrGrayFromBitmap, QrGrayFromGraphic,
                           QrReadBitmap (two overloads), QrReadGraphic,
                           QrTryReadBitmap, QrDrawCode, QrDrawText

  StreamSec.QR.Image.FMX   QrGrayFromBitmap, QrReadBitmap (two overloads),
                           QrTryReadBitmap, QrDrawCode, QrDrawText

The VCL adapter is the larger of the two because the VCL has TGraphic: a
TPngImage, a TJPEGImage, a TIcon or a TPicture's Graphic all go in through
QrGrayFromGraphic, with transparency composited over white. FireMonkey has one
bitmap type and needs no equivalent.

READING AND DRAWING HAVE DIFFERENT PRECONDITIONS, and it is worth knowing
which:

  reading   a nil bitmap, and an empty one, both raise eQrImage
  drawing   a nil bitmap raises eQrImage; an empty one does not, because the
            drawing routines size the bitmap themselves

QrTryReadBitmap is the one to call on frames you are pulling in a loop. It
returns False instead of raising, because most frames have no symbol in them
and that is ordinary rather than exceptional. Use it there and keep the
raising forms for the case where a symbol is expected.

Both overloads of QrReadBitmap can raise EITHER eQrNotFound, when there is no
symbol in the picture, OR eQrUnreadable, when there is one too damaged to
correct. Those are different answers - "nothing here" and "something here I
cannot read" - and a caller that only handles the first will be surprised by
the second. Handling eQrError catches both.

WHY StreamSec.QR.Image.FMX IS NOT IN THE RUNTIME PACKAGES. The packages
require the VCL. Putting a FireMonkey unit in one would make every project
that uses the library drag FireMonkey in with it. A FireMonkey project
compiles the adapter into itself instead, which needs no package and no
setting: the unit is in the same folder as the rest of the library.

Two consequences follow, and neither is a defect:

  The FireMonkey adapter has no topic in StreamSecTools.chm, because the help
  is generated from the runtime package's contains clause. This file is its
  reference.

  The two adapters export the same function names. That is deliberate - the
  same call reads a bitmap under either framework - but it means you name one
  of them in a uses clause, not both. If both appear, the later one wins for
  every colliding name and an unqualified call binds to it silently.

================================================================================
7. KNOWN LIMITS
================================================================================

  Micro QR is not supported.                 Versions 1 to 40 only.

  No other symbology: not Code 128, Data     Nothing here generalises to
  Matrix, Aztec or PDF417.                   them.

  Structured append - one payload across     Not implemented. Encode and
  several symbols - is not supported.        decode one symbol at a time.

  The detector expects a still, roughly      It is not a video pipeline and
  square on. Severe perspective, a symbol    does not track between frames.
  at a steep angle, or motion blur may
  yield eQrNotFound.

  One symbol per picture.                    QrLocateImage reports the one it
                                             found, not all of them.

  A QR code protects nothing.                Encrypt or sign the payload
                                             first if it needs it.

Stated plainly, because an accurate limit is worth more than an absolute
claim.


================================================================================
 END OF FILE
================================================================================