← All blog posts

QR Code Generator formula — how it actually works

WhiteUI · A plain-English guide to the QR Code Generator

The QR Code Generator has no single arithmetic formula like a calculator, because it is an encoder, not a calculator. It takes your text, works out the smallest QR version that can hold it at your chosen error-correction level, draws the grid, and sizes the picture. Each step follows a fixed rule, and the tool prints the result of each one under the code.

modules = 4 × version + 17
cell = size ÷ modules, rounded down
canvas = modules × cell

At a glance

Version 2: 25 modules on a 0 to 177 scale0 modules89 modules177 modulesVersion 2 is a 25 by 25 grid; version 40 is 177 by 177
Version 2, the tool’s grid for its default link, is 25 modules wide on a scale that runs to 177 modules at version 40.

Step one: pick the smallest version that fits

A QR code comes in 40 versions. Version 1 is 21 × 21 modules and version 40 is 177 × 177, and each version holds more text than the one before it. The tool starts at version 1 and tries each version in turn until it finds the smallest one that fits your text at the error level you picked. For the default text “https://whiteui.com” at level M, that is version 2, a 25 × 25 grid, because 4 × 2 + 17 = 25. A longer text needs a bigger version, and a higher error level needs a bigger version for the same text, because more of the grid is spent on spare copies of the data.

Step two: error correction

Error correction is spare data woven into the grid so the code still reads when part of it is damaged or covered. The four levels are L, which recovers about 7 percent of damaged modules, M, about 15 percent, Q, about 25 percent, and H, about 30 percent. The tool’s default is M. Picking H for a short link is harmless, but picking H for a long message can push the code to a bigger version, or past what fits, because the spare data takes room that the message could have used.

Step three: draw the grid on a canvas

Once the version is fixed, the tool draws the grid on a canvas, one square per module, black for a dark module and white for a light one. The canvas size comes from the image size you picked: the cell width is the size divided by the module count, rounded down, and the canvas is the module count times that cell width. At version 2 and 256 px, the cell is 256 ÷ 25 = 10 px, rounded down to 10, and the canvas is 25 × 10 = 250 px across. At 512 px the cell is 20 px and the canvas is 500 px, and at 1,024 px the cell is 40 px and the canvas is 1,000 px.

Step four: download as a PNG

The canvas is turned into a PNG file when you click “Download PNG”, and the file is saved as qr-code.png. The PNG holds the same black and white squares you see on screen, at the size you picked. Nothing is sent to a server at any point: the library that does the encoding, qrcode-generator 1.4.4, is bundled into the page and runs in your browser, so your link never leaves your own computer.

Worked example: the numbers behind the default code

Start with “https://whiteui.com”, 19 characters, size 256 px and error correction M. The tool picks version 2, because 19 characters fit in version 2 at level M. The grid is 4 × 2 + 17 = 25 modules wide. The cell is 256 ÷ 25 = 10 px, and the canvas is 25 × 10 = 250 px across. The line under the code reads “Version 2 • 25 × 25 modules • error correction M (~15% recoverable) • 19 characters,” and every number in that line comes from the three rules above.

The common mistake

People assume a bigger image size means a code that holds more text. It does not. The size only changes the cell width, from 10 px to 20 px or 40 px at version 2. What holds more text is a bigger version, which the tool picks for you, or a lower error-correction level, which frees room by storing less spare data.

Frequently asked questions

What is the QR Code Generator formula?

Modules = 4 × version + 17, cell = size ÷ modules rounded down, and canvas = modules × cell. The tool picks the smallest version that fits your text.

How many modules is version 2?

25 by 25, because 4 × 2 + 17 = 25. Version 40, the largest, is 177 by 177 modules.

What does error correction M mean?

M recovers about 15 percent of damaged modules. L is about 7 percent, Q about 25 percent and H about 30 percent.

How big is the PNG at 256 px for the default link?

The canvas is 250 px across, because each of the 25 modules is 10 px wide. At 512 px it is 500 px, and at 1,024 px it is 1,000 px.

More guides on the QR Code Generator

Try it yourself

Try the QR Code Generator — free, in your browser.

Open the QR Code Generator