Module Qrc

QR code encoder.

Consult the limitations and an encoding example.


ISO/IEC 18004:2015. QR Code bar code symbology specification.


module Matrix : sig ... end

QR 2D matrices.


type version = [
| `V of int

The type for QR code versions, from 1 to 40.

type ec_level = [
| `L


| `M


| `Q


| `H



The type for QR code error correction levels. Four levels respectively allowing 7%, 15%, 25% and 30% recovery of the QR code.

type mode = [
| `Byte

byte data


The type for (supported) data encoding modes.

module Gf_256 : sig ... end

Arithmetic over galois field GF(28).

module Prop : sig ... end

QR code properties.


val encode : ?⁠mask:int -> ?⁠version:version -> ?⁠mode:mode -> ?⁠ec_level:ec_level -> string -> Matrix.t option

encode ~version ~mode ~ec_level data is a QR matrix encoding bytes data with:

  • ec_level, the error correction level, defaults to `M.
  • version, the version of the QR code. If unspecified the minimal needed version for the given ec_level is used.
  • mode is the encoding mode. If unspecified it is guessed, but guess what, only `Byte mode is supported.
  • mask can be used to force the data mask. This should not be used, the best mask to please your scanner is automatically selected as mandated by the standard.

None is returned if data is too large to be fit the specified QR code parameters. Use Prop.mode_capacity to find data capacity for given QR code properties beforehand.

Note. Sometimes once the version and ec_level constraints are satisfied for data we can fit the data in a higher ec_level with the same version (i.e. same matrix width). In that case that higher error correction level is used instead of the given or default ec_level.

Notes and limitations


  • Only the (universal) byte mode data encoding is supported. This may mean larger QR codes if you are trying to encode only decimal digits, only decimal digits and uppercase US-ASCII letters or only Kanji characters.
  • For textual data, using UTF-8 with `Bytes should work reasonably well. The module does not implement the ECI scheme to specify the encoding. Signals from the interwebs seem to indicate it's better to let scanners auto-discover the encoding as some do not understand the ECI scheme.
  • Structured append, i.e. data represented by up to 16 linked QR codes, is not supported – who does ?


The following generates a QR code matrix for the given data bytes and outputs it as an SVG image on stdout.

let output_svg_qr data = match Qrc.encode data with
| None -> prerr_endline "Data capacity exceeded!"
| Some m -> print_endline (Qrc.Matrix.to_svg m)