DEV Community

Getinfo Toyou
Getinfo Toyou

Posted on

Building Sharp QR: Tackling Android Camera APIs and Real-Time Barcode Scanning

If you search the Google Play Store for a QR code scanner, you will immediately be hit with an endless wall of results. It is one of the most saturated categories on the platform. So, why would any developer willingly choose to build another one?

The answer is simple: developer frustration.

Every time I needed to quickly scan a menu, a WiFi credential, or a conference badge, I found myself downloading an app that was either bloated with full-screen ads, requested intrusive permissions like contact access, or simply struggled to focus on a code in low light. I wanted a clean, privacy-respecting utility that did exactly two things well: scan codes with high precision and generate them on the fly.

That frustration led to the creation of Sharp QR. Here is a look under the hood at how I built it, the tech stack I chose, and the hurdles of working with Android's camera hardware.

The Tech Stack

For this project, I wanted to use a modern, robust Android stack. I chose:

  • Kotlin: The undisputed standard for modern Android development.
  • Jetpack Compose: For building a reactive, declarative user interface.
  • CameraX: To handle the notoriously difficult Android camera hardware APIs.
  • Google ML Kit (Barcode Scanning API): For on-device, machine learning-powered barcode detection.
  • Room & Coroutines: For handling the local database to save scan history.

The Technical Challenges

Building a camera app is rarely straightforward. Even with modern libraries, dealing with hardware sensors across thousands of different Android device models introduces edge cases.

1. Taming the Camera Lifecycle with Compose
One of the first challenges was integrating CameraX with Jetpack Compose. CameraX is inherently lifecycle-aware, but bridging that imperative API with Compose's declarative state management required some boilerplate. I had to ensure the PreviewView properly bound and unbound to the Android lifecycle owner, handling backgrounding, screen rotations, and permission grants without crashing or freezing the camera preview.

2. High-Speed Frame Processing
The core feature of Sharp QR is speed. I wanted the app to recognize a QR code the millisecond it entered the frame. To do this, I fed the CameraX image stream directly into Google's ML Kit.

The technical hurdle here was backpressure. The camera hardware generates frames much faster than the ML Kit analyzer can process them. If you queue every frame, the app will quickly run out of memory or experience severe lag. The solution was implementing ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST. This ensures that if the analyzer is busy, older frames are dropped, and it only processes the most recent image. This resulted in a snappy, responsive scanning experience without battery drain.

3. Generating Crisp Codes
Beyond scanning, Sharp QR allows users to generate custom codes for URLs, text, and contact info. Generating the QR matrix requires encoding the data and mapping it to a Bitmap. The challenge was ensuring these generated bitmaps were rendered sharply on high-density displays (xxhdpi/xxxhdpi) without artifacting, especially when users try to export or share them. I utilized a customized encoding algorithm to output clean, perfectly contrasted PNGs.

Lessons Learned

Building Sharp QR taught me a lot about the current state of Android development.

First, ML Kit is a massive improvement over older image processing libraries. The machine learning models are incredibly resilient to poor lighting, skewed angles, and partially damaged codes.

Second, while CameraX abstracts away the nightmare of the older camera2 API, testing on physical devices is still mandatory. Emulators simply cannot replicate autofocus quirks or hardware-level frame rate drops.

Conclusion

What started as a weekend project to solve a personal annoyance turned into a deeply educational dive into modern Android hardware APIs. I built Sharp QR to be the exact tool I wanted on my own phone—fast, precise, and completely focused on the user's task.

If you are an Android user, a marketer dealing with campaign codes, or just someone who appreciates a straightforward utility without the fluff, I would love for you to try it out. You can grab Sharp QR on Google Play: https://play.google.com/store/apps/details?id=com.getinfotoyou.sharpqr

Let me know how quickly it scans for you, and feel free to ask any questions about the CameraX implementation in the comments!

Top comments (0)