Building a QR code scanner comes down to three working parts: a live camera feed, a barcode/QR decoding library, and a handler for whatever the decoded text turns out to be (a URL, Wi-Fi credentials, contact info, plain text). The fastest reliable route is to use a mature decoder library such as ZXing (Java/Kotlin), its C# port, the ML Kit barcode scanning API in a mobile app, or a JavaScript library like `jsQR` or `zxing-js` for the web, then wire the camera into it. Everything below is written for a from-scratch project, but the steps map onto an existing app just as well.

What You'll Need

  • A development environment for your target: Android Studio, Xcode, Visual Studio/Xcode for cross-platform frameworks, or a Node/npm toolchain for the web.
  • A decoder library matched to your platform (see Step 2). Do not hand-roll QR decoding for production; the Reed–Solomon error correction and format detection are hard to get right.
  • A test device with a camera and, on mobile, permission to use it at runtime.
  • A set of sample QR codes you generate yourself: a plain URL, a long URL, a Wi-Fi code, a vCard, a code with a logo in the centre, and a low-contrast or slightly blurred code to test failure paths.
  • Lighting and something to print on, if you plan to test with paper codes as well as on-screen ones.

Step-by-Step

  1. Decide what your scanner must do. Write down the outputs you will handle: open URLs in a browser, join Wi-Fi, save a contact, copy text. This determines how much parsing logic you need beyond raw decoding. A scanner that only shows the decoded string is a valid first milestone.
  2. Pick and install the decoder. On Android, the ML Kit barcode scanning library or ZXing are the common choices; on iOS, the built-in Vision framework's barcode detection or AVFoundation metadata output; on the web, a JavaScript library reading frames from a <video> element. Install it through the official package manager for your platform (Gradle, CocoaPods/Swift Package Manager, npm) — never a copy-pasted archive from an untrusted source.
  3. Request camera permission correctly. On Android, declare the camera permission in the manifest and request it at runtime before opening the camera; explain in one short line why the app needs it. On iOS, add the camera usage description to the app's information property list, or the app will be rejected and the camera call will fail. On the web, the browser prompts automatically, but it only works on HTTPS or localhost.
  4. Open a preview and feed it to the decoder. Mobile SDKs usually let you attach an analyser to the camera preview; web libraries need you to copy each video frame onto a canvas and pass the image data to the decoder. Aim for roughly 10–15 decode attempts per second — faster than a person can move the camera, and far cheaper than running at full display refresh rate.
  5. Decode off the preview callback. Run decoding on a background thread or worker so the preview stays smooth. On mobile, throttle by skipping frames; on the web, use requestAnimationFrame and skip work while a decode is already in flight.
  6. Handle the result immediately and once. When a code decodes, stop the scanning loop, show the decoded content, and act on it. Guard against the same code firing repeatedly while it stays in frame; a simple flag or a short cooldown after a successful read fixes it.
  7. Parse common formats defensively. Treat URLs as untrusted: show the full URL before opening it, and refuse non-http(s) schemes unless you deliberately support them. Wi-Fi payloads and vCards follow their own field layouts — parse them, do not assume a simple colon split. Anything unrecognised should fall back to displaying the raw text.
  8. Design the failure and empty states. Cover: permission denied (offer a link to system settings), no camera present, camera busy in another app, code not detected, and decoded text that your app cannot act on. Each should show a plain message and a way to retry.
  9. Test against your sample set. Scan each generated code at arm's length, at an angle, in dim light, and on a screen. Note which fail and why. Then test the happy path end to end: scan a URL, confirm the browser opens it, come back, scan again.

Common Mistakes

  • Decoding on the UI thread. This makes the preview stutter and drains the battery; the camera feed is the sensitive part, not the decoder.
  • Forgetting the iOS camera usage description. The camera simply refuses to start, and the app can be rejected during review.
  • Auto-opening URLs without showing them. A QR code is just text. A malicious code can point anywhere, so display the destination and require a tap to continue.
  • Assuming every code is a URL. Many are Wi-Fi, contact cards, or just text; a parser that crashes on those is a broken scanner.
  • Ignoring the multiple-decode case. Leaving the loop running after a hit causes duplicate actions or a flickering result screen.
  • Testing only perfect on-screen codes. Printed, angled, or low-contrast codes are the real world; the decoder settings may need adjustment (for example, enabling all barcode formats you actually want).

Tips

  • Constrain decoding to the formats you support. Scanning for every possible symbology is slower and more error-prone than scanning for QR only.
  • Give the user visual feedback — a viewfinder box, a short vibration, or a highlight — the instant a code is found.
  • Keep the decode loop running only while the screen is visible; pause it when the app goes to the background to save power.
  • Log the raw decoded string during development. It is the single most useful piece of information when something behaves oddly.
  • If you use a library with an embedded or hosted scanning UI, check its privacy policy before shipping, since camera frames can leave the device.

Troubleshooting

If codes are not detected at all, first confirm the camera preview is actually live — a black preview means permission or camera access is the problem, not the decoder. Check that the permission was granted at runtime, that no other app holds the camera, and that the preview is not being handed an image smaller than the code.

If codes only decode from close up, the analyser is probably receiving a downscaled frame. Feed it the highest resolution the camera offers that still keeps performance acceptable.

If the preview stutters, reduce how often you decode (skip frames), move decoding off the main thread, and lower the preview resolution rather than the decode resolution.

If a code decodes but your app does nothing, print the raw string. A URL missing its scheme, a code with a leading newline, or a Wi-Fi payload with an unexpected escape sequence will all break naive parsing. Trim whitespace and use a strict parser with a fallback.

If it used to work and now does not, update the library and the platform SDK, then rebuild. If a single test code fails while others pass, the code itself is likely damaged or low-contrast — regenerate it before blaming the scanner.

If all else fails, replace the decode loop with a minimal one that only logs the raw result, confirm that works, and add features back one at a time.

Frequently Asked Questions

Do I need a paid library to build a QR scanner?

No. ZXing, ML Kit's barcode scanning, and the Vision framework are all available at no charge for the platforms they support.

Can a web page scan QR codes without a native app?

Yes. Browsers can access the camera from a page served over HTTPS or from localhost, and a JavaScript decoding library can read the frames. The user gets a permission prompt the first time.

Why does my scanner see the code but not decode it?

Usually the frame is too small, too dark, or too blurry, or the format is not enabled in the decoder's settings. Try a larger frame and enable QR explicitly.

Is it safe to open whatever URL a QR code contains?

Not blindly. A code can encode any web address. Show the destination to the user and let them confirm before opening it, and be wary of codes that ask for personal details.

Does the camera need to be held still?

No, but detection is faster and more reliable when the code fills a decent portion of the frame. Most decoders handle slight motion and angle well.

Can I scan codes from an image file instead of the camera?

Yes. Every decoder listed above can process a still image; feed it a user-picked picture instead of a camera frame.