← Binderdex dev log

Scanning a stack of cards without touching the screen

#mobile#game-dev#product#solo-founder

A fanned stack of cards inside white viewfinder corners, with an amber ring locked onto a small amber number strip at the bottom of the top card and three result chips to the right

Scanning a card in Binderdex, my Pokémon card collection and price app, used to mean one tap, one photo, one round trip to a vision API, and a median wait of around four seconds. It found the right card 24% of the time. Nobody works through a stack of cards that way. The scanner exists so a collector can photograph a card and have the app identify it, instead of searching for it by hand.

I spent 26 hours rebuilding it in three phases. By the end the match rate was 92%, a scan took roughly half the time, and you could feed cards under the camera without touching the phone.

Phase 1: read the number before the picture

Every card has a collector number, the small number printed at the bottom that identifies the card within its set, in a fixed spot and a known format. That's a far easier target than the artwork. So the scanner now reads text first. On-device OCR with ML Kit pulls the number, and the app resolves it against a cached snapshot of the catalog without leaving the phone. The image pipeline only runs when text fails.

The biggest win in this phase was a parser fix. The old parser read "134/108" and kept "134". The full fraction says far more: the set has 108 cards and this one is numbered 134, which marks it as a parallel (an alternate printing of a card) or a promo (a special-release card outside the main set). Capturing both halves turned a 2.2-second server scan, which used the number only as a clue, into an instant local match.

The fast path is short enough to show whole. Everything above the last line happens on the device:

// Simplified: the fast path that skips the cloud
async function scanCard(frame: CameraFrame): Promise<ScanResult> {
  const ocrText = await mlKit.recognize(frame);        // on-device
  const collectorNo = parseCollectorNumber(ocrText);   // "134/108" not "134"
  if (collectorNo) {
    const match = catalog.lookup(collectorNo);          // local hash match
    if (match) return { ...match, viaHints: true };
  }
  return scanViaImage(frame);  // the old pipeline, already prepared
}

Phase 2: let the camera decide when

Next I removed the tap. A small piece of lock logic, a state machine that tracks whether a card is stable yet, polls the preview about twice a second. When it sees the same collector number hold steady for 900 ms, it takes one 2-megapixel photo and sends it into the unchanged scan pipeline, with no shutter sound. Then it cools down until the card leaves the frame.

The state machine was the easy part. The hard part was knowing whether the text-reading code existed in the installed copy of the app, since phones that haven't updated lack it. Checking for it directly misfired in two ways. In one setup the check returned wrong answers. In development, the app's bundler reported a missing piece as a fatal error before my code could catch it.

So I stopped checking. The app now decides by its installed version: 1.1.3 or newer has the text reader, and anything older keeps the tap. A version number can't throw.

Phase 3: OCR on every frame

Polling twice a second still left a gap between showing a card and seeing it lock. Version 1.1.4 reads text on every frame the camera produces, instead of twice a second. Text outside the on-screen card frame is filtered out before parsing, so stray text elsewhere in view never reaches the parser. The same lock logic sits on top, and a lock takes one photo from a camera that's already set up and waiting, so there's no startup delay.

EVERY FRAME on the device, no network round trip ML Kit OCR on each camera frame Drop text outside the card frame Parse the full collector number ONCE PER CARD the lock fires a single capture Same number steady for 900 ms One pre-warmed photo capture Scan pipeline matches the card Tray chip with price and total Above 0.8 confidence a match goes to the tray; weaker matches open the picker.
The top row runs on every frame and costs nothing visible. The bottom row runs once, when a number holds still.

A tray that keeps the camera on screen

Hands-free capture is wasted if every match interrupts you. Matches above 0.8 confidence now drop into a floating chip that shows the price and a running total, and the camera stays up. Weaker matches open a picker, a short list of likely cards to choose from. When the stack is done, a review screen adds everything in sequence.

What I tried and took back

I dropped the vision input from 1024 px to 768 px to save time, and digits started misreading. A card numbered 167/159 came back as 163/159. I reverted it. When the whole design rests on one small printed number, resolution is the wrong place to economise.

The per-frame OCR plugin also raised the Android floor. It locks frame buffers through AHardwareBuffer, which needs API 26, so minSdk went from 24 to 26. Android 8.0 is now the lowest version that can install, and older devices keep the version they already have.

Ask the cheapest question first

The old scanner asked the hardest question, "what is this picture?", on every single scan. The new one asks the easiest, "what does this printed number say?", and saves the hard question for when the easy one fails. The rebuild took the match rate from 24% to 92%.

Before tuning an expensive recogniser, look for the part of the object that was designed to be read. It's usually small, boring and printed in the same place every time.

This is the build log for Binderdex (www.binderdex.com) · All entries · Weekly essays