Most document scanner apps send your page to a server to pull the text out of it. For a receipt or a signed contract, that means a photo of something you probably did not want to upload is now sitting in someone else's logs. You do not have to build it that way. Android can read the text off an image on the device, with no network call at all, and the API for it is free.
I have shipped this in a scanner app that runs OCR on the phone, and the quickstart gets you a demo in ten minutes but hides the parts that actually matter in production. Here is the whole thing, including the parts that bit me.
What ML Kit gives you
ML Kit's Text Recognition v2 runs a model locally and returns the text in an image. On-device means the bytes never leave the phone, it works in airplane mode, and there is no per-call cost because there is no call. Latency is a few hundred milliseconds on a mid-range phone instead of a round trip plus server time.
The Latin-script model is the default. Chinese, Devanagari, Japanese and Korean each have their own model you pull in separately, so you are not shipping scripts you do not need.
You get two ways to ship the model:
-
Bundled (
com.google.mlkit:text-recognition): the model ships inside your APK. It works on first launch with no download, and it adds a few MB to your app size. -
Unbundled (
com.google.android.gms:play-services-mlkit-text-recognition): Google Play Services downloads the model the first time it is needed. Smaller APK, but the first OCR call can fail or stall if the download has not finished.
// build.gradle (bundled Latin model)
implementation("com.google.mlkit:text-recognition:16.0.1")
The minimal working version
val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
// rotationDegrees matters. See the gotchas below.
val image = InputImage.fromBitmap(bitmap, rotationDegrees)
recognizer.process(image)
.addOnSuccessListener { result ->
for (block in result.textBlocks) {
for (line in block.lines) {
// line.text is the string, line.boundingBox is where it sits
Log.d("OCR", "${line.text} @ ${line.boundingBox}")
}
}
}
.addOnFailureListener { e ->
Log.e("OCR", "recognition failed", e)
}
The result is a tree: blocks hold lines, lines hold elements (roughly words), and every node carries a boundingBox and the recognized text. Most apps want result.text for the raw dump, but the boxes are what let you rebuild layout, highlight a field, or line up a translation over the original.
The parts the quickstart skips
Clean the image before you hand it over. OCR accuracy on a raw camera frame is noticeably worse than on a cropped, deskewed, contrast-corrected page. The order that worked for me is: detect the document edges, warp to a flat rectangle, convert to grayscale, then push that into ML Kit. Feeding the model a full photo of a page lying at an angle on a desk is the single biggest cause of garbage output.
Rotation is a real bug source. InputImage needs the correct rotationDegrees, and camera frames are almost never upright. If you pass a media.Image from CameraX, read the rotation from the ImageProxy. If you forget this, the model quietly returns worse results instead of failing, so it is easy to miss until someone scans a landscape receipt.
Do not run it on the main thread for batch work. One frame is fine. Running OCR across twenty pages of a scan on the UI thread will jank the app. Move batch recognition to a background dispatcher and show progress.
Close the recognizer. TextRecognizer holds native resources. Call recognizer.close() when you are done, or tie it to the lifecycle, or you will leak.
Decide bundled vs unbundled on purpose. If OCR is a headline feature people use on first launch, bundle the model so it is there offline from minute one. If it is a secondary feature and app size matters, go unbundled, but then handle the "model still downloading" state with a real message instead of a silent failure.
Where it still falls short
It reads printed text well. It does not read handwriting in any way you can trust. Low light and motion blur drop accuracy fast, which is another reason the edge-detect-and-clean step earns its place. Tables come back as a stream of lines, so if you need the grid you have to reconstruct columns yourself from the bounding boxes, and that is its own project. And the bundled model is Latin only, so anything beyond that is an extra model and an extra decision about app size.
None of that makes it the wrong choice. It makes it a tool with edges, and knowing the edges is the difference between a demo and something people rely on.
Why I went on-device
Disclosure: I build Paperora, an Android document scanner, so I had a reason to care about this beyond a blog post. The whole app runs scanning, PDF creation and OCR on the phone, so documents stay on the device and it works with no signal. That was a product decision as much as a privacy one: people scan IDs, medical forms and contracts, and "we never had your file in the first place" is a stronger promise than "we deleted it."
If you are building something in this space, start on-device and only reach for a server when you hit something the on-device model genuinely cannot do. You will ship a faster, cheaper, more private app, and most of the time you will never need the server at all.
Happy to answer ML Kit questions in the comments.
Top comments (2)
The rotationDegrees point is the one that would have bitten me — a model that quietly returns worse results instead of failing is the worst kind of bug, because everything looks fine until a user holds the scan next to the paper. I build Android apps too, and the "bytes never leave the phone" framing is what sells on-device OCR for me: for a receipt or a signed contract, the privacy story is the feature, not a side benefit.
One thing I would love to know: do you run the recognizer on the full camera frame, or crop to the detected document edges first? My guess is the crop matters more than the model version on a wrinkled receipt, but that is a guess — what did your scans actually show? And which way did you go on bundled vs unbundled for Paperora — bundle the model so first launch works offline, or keep the APK lean and let Play Services fetch it?
Good instinct on both.
Crop first. The capture path runs edge detection and a perspective warp before anything, so OCR sees the cropped, deskewed page rather than the raw frame, and you are right that on a wrinkled receipt that crop does more for accuracy than the model version does. A flat, evenly lit page is most of the battle.
On bundled vs unbundled, we went unbundled: the build excludes ML Kit's bundled Latin model and pulls play-services-mlkit-text-recognition instead, with the Play Services dependency metadata so the model downloads on demand. It keeps the APK small, and the cost is exactly the one in the post: you handle the "model is still downloading" state on first use instead of assuming OCR is there from minute one. For a scanner where OCR is a secondary action rather than the first thing a new user hits, that trade felt right. If OCR were the headline on first launch I would bundle it.