How to read a QR code that is already on your screen, and what a decoder does with the pixels
Someone sends you a QR code in a chat message. Your bank's app shows one on the laptop to link a new device. A conference PDF has a code for the Wi-Fi. A website you're setting up two-factor login on shows a code, and you want a second copy of the key for your password manager. In each case the code is on the screen of the device that would normally scan it, and a phone can't point its camera at its own display.
QR codes were designed for cameras pointed at paper. This post explains how a decoder finds and reads a code in an ordinary picture, why some screenshots and photos read instantly while others fail, what to check before you open a link that came out of a code, and how Scan QR Code from Image reads a screenshot, photo or pasted image without it leaving your browser.
Reading a code is a search problem
A camera app doesn't know where in the frame the code is, how big it is, which way up it is, or whether it is tilted. Neither does a decoder handed a screenshot. So decoding starts as a search, and the QR code is designed to make that search easy.
Step 1: black and white
A photo is millions of shades of colour. The decoder first turns it into pure dark and light, a step called binarisation. A single brightness threshold for the whole image fails as soon as one corner is in shadow, so good decoders compare each pixel with its neighbourhood instead. jsQR, the open-source decoder this tool uses, splits the image into 8 by 8 pixel blocks, works out a local threshold from each block and its surroundings, and decides dark or light pixel by pixel against that.
Step 2: find the three corners
Every QR code has three finder patterns, the large nested squares in three corners. Any straight line through the middle of one crosses dark, light, dark, light, dark in the proportions 1:1:3:1:1, whatever the angle and whatever the size. The decoder scans rows and columns of the black-and-white image for runs in that ratio, confirms candidates in the other direction, and looks for three that form a right-angled triangle. Which corner has no finder tells it which way up the code is.
Step 3: straighten it
From the three finders, plus the smaller alignment pattern that codes from version 2 up carry near the fourth corner, the decoder works out a perspective transform: a mapping from the tilted, possibly skewed shape in the image to a flat square grid. It estimates the module size from the finders, which gives the code's version and so the number of modules a side, and then samples the centre of each module in the image.
Step 4: read and repair
The format information, stored twice next to the finders, says which error correction level and which of the eight mask patterns were used. The decoder undoes the mask, reads the codewords out of the grid in the standard's zigzag order, and runs Reed–Solomon error correction over them, which repairs a limited number of misread or missing codewords. Then it decodes the bits back into text according to the mode indicators: digits, letters, raw bytes or kanji.
Each step can fail on its own terms. Blur and glare spoil the binarisation. A code cut off at the edge loses a finder. A code too small in the image leaves too few pixels per module to sample. Too many damaged modules exceed what error correction can repair.
Why some pictures fail
Too few pixels per module
The decoder samples one point per module, so each module needs a few pixels in the image. To find out how few, for this post a clean, computer-made version 6 code (41 modules across, 49 counting the blank border) was shrunk by averaging pixels together, as a downscaled photo is, and handed to jsQR at every width from 100 to 300 pixels. It read at every width from 129 pixels up, about 2.6 pixels per module, and failed at 100, 127 and 128. That was a perfect image with no blur, noise or tilt. A real photo needs a good deal more.
Too many pixels to search
The opposite problem: a 12-megapixel phone photo is 4,000 pixels across, and searching all of it is slow in a browser. Shrinking the picture first is fast, but it shrinks the code too. That's the trade-off the reader has to handle, below.
Inverted colours
A code printed light on dark has its finder patterns inverted too, so a search for dark-light-dark-light-dark runs finds nothing. A decoder has to try the inverted image as well.
Transparent backgrounds
A code saved as a PNG with a transparent background is black modules on nothing. Drawn onto a canvas, "nothing" has no brightness of its own, and depending on how it is handled the code can vanish into a uniformly dark image.
How the reader handles a picture
Scan QR Code from Image works in the page itself: the image is opened by the browser, drawn onto a canvas and decoded by jsQR.
- Getting the image in. Choose a file, drop it on the box, or paste a screenshot anywhere on the page (except into the text box). Any format your browser can open works.
- A white background first. The canvas is filled with white before the image is drawn on it, so a transparent PNG becomes a dark code on white rather than a dark code on nothing.
- A quick pass. The image is scaled so its longest side is at most 1,000 pixels, and decoded. Most screenshots are found here, fast.
- A detailed pass, if needed. If nothing was found and the image is larger than 1,000 pixels, it is decoded again at up to 2,400 pixels on its longest side. This is the pass that finds a small code in a large photo.
- Both polarities. Each pass tells jsQR to try the image as it is and inverted, so light-on-dark codes are read too.
When a code is found, the full text appears next to your image, and the page looks at what kind of text it is:
- A web address (
httporhttpsonly) gets an Open link button and the status Found a link. Check where it goes before you open it. Nothing is opened until you press it. - A Wi-Fi code (
WIFI:T:WPA;S:…;P:…;;) is split into network, password, security type and hidden flag, with the backslash escapes undone. - A 2FA setup code (
otpauth://totp/…orotpauth://hotp/…) shows the issuer, account, secret key grouped in fours, and the algorithm, digits and period. For time-based codes the page computes the code your authenticator should be showing right now, with Web Crypto, so you can check the two match. - Anything else is shown as raw text to copy: phone numbers, vCards, payment strings, serial numbers.
A worked example: a photo with a small code in it
Take the café Wi-Fi code from the QR Code Maker's example: network Cafe Rosa, password coffee;shop:2026. It is a version 6 code, 41 modules a side, 49 with its border.
As a screenshot, say a 980-pixel PNG of the code, it is found in the quick pass at full size: 20 pixels per module, far more than needed. The page shows:
| Network | Cafe Rosa |
|---|---|
| Password | coffee;shop:2026 |
| Security | WPA / WPA2 / WPA3 |
The raw text in the box underneath is WIFI:T:WPA;S:Cafe Rosa;P:coffee\;shop\:2026;;; the password above has its escapes removed, which is what you would type into a phone by hand.
As a photo of a table card, 4,000 pixels wide, where the printed code is 400 pixels across: the quick pass shrinks the photo to 1,000 pixels, and the code with it, to 100 pixels, about 2 pixels per module. That is below what even the perfect test image read reliably. The detailed pass shrinks the photo only to 2,400 pixels, leaving the code 240 pixels across, nearly 5 pixels per module, which is comfortably readable if the photo is sharp.
The same card in a 6,000-pixel photo, taken from further away so the code is only 300 pixels across, is shrunk even in the detailed pass to 120 pixels: back in borderline territory. Cropping the photo to the code before choosing it solves this. Fewer pixels around the code means less shrinking.
Why not always decode at full size? Because a big photo takes noticeably longer to search in a browser, and most images don't need it. Two passes keep the common case fast and still catch the small code in the big photo.
Checking a code before you trust it
A QR code hides its content until something decodes it, which makes it a convenient wrapper for a bad link: a sticker over a parking meter's real code, a "scan to pay" in a phishing email, a fake parcel notice. Reading the code here shows the whole address as plain text, with no redirect followed and nothing loaded. What to look for:
- Read the host correctly. The host is the part after
https://and before the next/, read from the right:login.example.com.verify-account.netbelongs toverify-account.net. - Anything before an @ is not the host.
https://www.mybank.com@evil.example/logingoes toevil.example; the first part is a user name the browser ignores. - Short links hide the destination. A code that opens a link shortener tells you nothing about where it ends up. Be more careful, not less.
- Unexpected payment or login pages from a code on a poster or in an email deserve the same suspicion as a link in an unexpected email. Go to the service directly instead.
Two-factor codes: treat the key like a password
A 2FA setup code holds a secret key. Anyone who has it can generate your one-time codes, so it should be treated like a password. When the reader finds one, it computes the live code in the page and the status line reminds you to keep the key private.
The page also avoids putting the key anywhere it could leak. For other codes, Make a QR from this opens the generator with the contents in its address, which means the site's server receives them when you follow it. For 2FA codes that button is hidden, so the key never goes into a link. Counter-based (HOTP) codes get their details but no live code, since the next code depends on a counter only the service knows.
One exception to know about: Google Authenticator's Transfer accounts export uses its own otpauth-migration:// format, which packs several accounts into one code. The reader shows that as raw text rather than as accounts.
Tips for a clean read
- Screenshot rather than photograph whenever the code is on a screen. A screenshot has perfectly sharp modules; a photo of a screen adds moiré, glare and blur. On Windows, Win+Shift+S snips part of the screen to the clipboard; paste it straight onto the page.
- Crop big photos to the code. It avoids the shrinking described above and makes the search faster.
- Take photos straight on, with the whole code in frame and a little blank margin around it. All three finder corners have to be visible.
- One code per image. If a picture has several, crop to the one you want: the decoder returns the first it finds.
- Check, then open. Copy the text or read the address before pressing Open link, especially for codes from stickers, posters and emails.
- Keep 2FA screenshots somewhere safe, or delete them once the key is in your authenticator or password manager. They are as sensitive as the key.
Need to make a code rather than read one? The QR Code Maker for Links & Wi-Fi draws static codes, Wi-Fi escapes and all.