In Zimbabwe you buy airtime on a scratch card. You scratch off the foil, and underneath is a 17-digit PIN. Then you open your dialer and type *121* followed by all seventeen digits, followed by #.
If you mistype one digit, the network rejects it and you try again. If you mistype it twice you start to wonder whether you're reading a 5 or a 6 through a scratched-up panel in bad light. People do this several times a week.
So: point a camera at it, read the digits, build the string. A weekend project.
It was not a weekend project, and almost none of the difficulty was in the OCR.
First instinct: validate the PIN
Before you send a PIN anywhere, you'd like to know it's plausible. Credit card numbers have the Luhn check digit. ISBNs have a weighted modulus. IMEIs use Luhn too. If airtime PINs carried anything similar, a misread would be caught instantly on the device, before a wasted attempt.
I had four real Econet PINs, from four real cards I'd bought. I tested:
- Luhn, both digit orders
- Simple digit sum mod 7, 9, 10, 11
- Mod 97, the IBAN scheme, on the first 15 digits against the last 2
- Weighted schemes — alternating weights, positional weights, a few dozen combinations
Nothing. Not a single scheme produced a consistent remainder across all four.
Then I looked at what else is printed on the card. Econet cards carry three numbers: the 17-digit PIN under the panel, an 11-digit serial, and a 9-digit batch number. The serials across my four cards were sequential. The PINs were not correlated with them in any way I could find.
That's the answer, and in hindsight it's obvious. The serial is the card's identity, printed openly, and it increments. The PIN is a secret that has to be unguessable — so it's random, and validity is a server-side lookup against a table. There is nothing in those 17 digits for a client to verify.
Which means the app cannot tell a good PIN from a bad one. It can only try to read correctly in the first place.
That one finding set the shape of everything else.
OCR has no error correction
Here's the thing that took me a while to internalise.
A QR code can't be read wrong. It carries Reed–Solomon error correction, so a scanner either decodes the correct payload or fails outright. At the highest correction level roughly 30% of the symbol can be destroyed and you still get the right answer back. There is no state where a QR scanner confidently hands you the wrong string.
OCR has no equivalent. A text recogniser handed a blurry 8 will cheerfully return 0 or B, with no signal that anything went wrong. The confidence scores, where the model exposes them at all, are not calibrated for "is this digit right".
So for digits that are money, a single read is not evidence. It's a guess with good odds.
Voting across frames
The fix is to read the same card several times and require agreement.
final top = eligible.first;
_votes[top.pin] = (_votes[top.pin] ?? 0) + 1;
final leader = _votes.entries.reduce((a, b) => a.value >= b.value ? a : b);
if (leader.value >= _votesToAccept) {
await _accept();
}
Three agreeing frames before the PIN is accepted. On a clean card they agree immediately and it feels instant. On a worn one it takes a second or two, and that second is the whole point.
Two refinements worth mentioning.
One vote per frame. Early on I counted every candidate the extractor found, which meant the 11-digit serial number printed next to the panel accumulated votes alongside the PIN. On a hard-to-read card the serial could reach the threshold first. Now only the top-ranked candidate in each frame gets a vote, and anything whose length the carrier doesn't issue can't vote at all.
Early accept, strictly. Three votes earn their keep on a worn card where frames disagree. On a clean card the third frame confirms something nothing ever disputed — a third of the scan spent re-reading a settled question. So two agreeing frames is enough, but only when there has been exactly one distinct reading in the entire scan and its length is one the carrier actually issues. One disagreeing frame anywhere and it waits.
Three frames are not three samples
This is the part I got wrong for a while, and it's the most transferable lesson here.
Three consecutive frames from a camera stream, 90ms apart, are not three independent observations. They share an exposure, a focus state, a white balance, and very nearly the same glare. If a highlight across an 8 makes it read as 0 in one frame, it reads as 0 in all three. They agree, the vote threshold is met, and the agreement proves nothing.
Statistically I was sampling once and counting it three times.
A full-resolution still is different. takePicture() locks focus, uses a longer exposure, and gives roughly ten times the pixels of a stream frame. Three stills taken in sequence genuinely are three looks at the card.
That's why the shutter path in the app requires fewer agreeing frames than the live path, not more. Better evidence needs less of it.
Then they handed me four cards at once
Vendors sell airtime in strips — four cards still joined together, four PINs in a vertical stack.
Scanning them one at a time worked, and felt like feeding one sheet through a scanner four times.
The first attempt to fix this was embarrassing. The scanner has a band filter: it keeps OCR results whose vertical centre falls inside the guide brackets, so that printing elsewhere on the card doesn't compete with the PIN. Perfectly sensible for one card. Pointed at a strip it discards three of the four rows before anything else runs.
The second problem was the voting itself. The single-card path votes for one reading per frame and stops when it's sure. For a strip, four readings should be advancing together. Voting on one and discarding the rest is exactly what made it feel like four separate scans.
So the strip path keeps a separate tally per PIN, and each one is collected the moment it has its own three agreeing frames, independently of the others:
for (final c in found) {
if (serials.contains(c.pin)) continue;
if (!expected.contains(c.pin.length)) continue;
if (ScanQueue.contains(c.pin)) continue;
final votes = (_votes[c.pin] ?? 0) + 1;
_votes[c.pin] = votes;
if (votes < _votesToAccept) continue;
ScanQueue.add(QueuedCard(carrier: _carrier, pin: c.pin, /* … */));
}
Holding a strip still now collects all four in about the time the old path took to collect one. The vote threshold is not relaxed — a card in a strip is read under worse conditions than one filling the frame, so if anything it needs the agreement more.
One more thing that mattered: the stream runs at 1080p rather than 720p. One card filling the frame has pixels to spare at 720p. Four rows sharing that frame give each PIN about a quarter of the height — roughly 180 pixels for a line of 17 small digits, which is where a recogniser starts dropping characters. It costs a few milliseconds per frame and saves whole re-scans.
The modem carries one session
With four PINs in hand, the obvious next step is to send them all.
On Android you can do this in-app. TelephonyManager.sendUssdRequest (API 26+) sends the string and gives you the network's reply as text, instead of bouncing the user out to the dialer:
telephony.sendUssdRequest(ussd, object : TelephonyManager.UssdResponseCallback() {
override fun onReceiveUssdResponse(tm: TelephonyManager, req: String, res: CharSequence) {
// the network's own words, back inside the app
}
}, handler)
What you cannot do is send four at once. USSD is a session protocol and the modem carries exactly one session. Fire them in parallel and they don't queue — they collide, and some of them silently don't happen. With real money on each card, "silently didn't happen" is the worst possible outcome.
So the batch runner is strictly serial: send, wait for the reply or a timeout, record the result, pause, send the next. Four cards take about as long as four cards should take. The win isn't speed, it's that nobody has to stand there doing it.
Shipping it with no server
The app has no backend. No accounts, no analytics, no crash reporting of its own. Card images are read on-device with a bundled ML Kit model. The full PIN is never written to storage — history keeps a SHA-256 hash and the last four digits, enough to warn you about a card you've already used and useless to anyone who gets the file.
This is the right call for the product. "Your PINs never leave your phone" is the single claim that matters when you're asking people to point a camera at something worth money, and I can demonstrate it live by putting the phone in airplane mode and scanning a card.
It has a cost, and it's worth being honest about it: I am blind to field failures. I have no idea how often OCR fails on a worn card in bad light, because the only thing that would tell me is the telemetry I've deliberately refused to collect. Play Console gives me crashes and ANRs from the framework, but the quality signal I most want — scan success rate — is the one I gave up.
I'd make the same trade again. But it's a trade, not a free win.
The bug a one-star review found
Last week someone left this:
tried to recharge and always says the card has been used already, when i try the card manually on the phone it goes through with ease
That last clause is the whole diagnosis. If the card works when typed manually, the PIN is valid and unspent. So the "already used" message was coming from my own local history, not from the network.
The chain, as far as I can tell: a send fails for some reason, the network replies with a rejection — and my code decided the send had succeeded because a reply came back, rather than because of what the reply said. The card gets written into history as successfully redeemed. From then on every attempt hits the duplicate check and gets blocked, while the card itself sits there perfectly good.
One wrong boolean, and the app confidently tells you your money is gone.
It's worse than it looks, too, because that same flag feeds a spending total — which has been counting money that was never spent — and the batch runner, which has been marking failed cards as done. One root cause, three symptoms, none of which I noticed because, as above, I collect nothing.
The fix is to parse what the network actually says instead of counting replies. I'm doing that now, along with an escape hatch on the warning itself, because right now a wrongly-recorded card can only be cleared by wiping all history — and most people will uninstall before they find that setting.
The lesson I'd actually take from it: I designed carefully around the failure I'd thought about — a misread digit costing someone a real card — and shipped a different failure entirely, in the four lines that decide what "success" means. Every other decision in this post came from asking "what happens when this is wrong?" I just never asked it about the one piece of code whose entire job was to answer that question.
Recharge4Me is free on Google Play and the App Store. Built with Flutter and Google ML Kit, no backend.
Top comments (0)