DEV Community

fan ei
fan ei

Posted on

How I Built an Open Source Android Document Scanner with Kotlin

Introduction

Building a document scanner app seems complex — edge detection, perspective correction, PDF generation, OCR. But with modern Android development tools, it's more approachable than you think.

Here's how I built SmartScan, a fully open source document scanner app using Kotlin.

Architecture

The app follows clean architecture principles:

  • Presentation Layer — MVVM with ViewModels and Compose UI
  • Domain Layer — Use cases for scan, OCR, and export operations
  • Data Layer — Room database for document storage, CameraX for capture

Key Technical Decisions

CameraX for Document Capture

CameraX makes camera integration straightforward. For a scanner app, the key is handling the preview and capture lifecycle correctly.

OpenCV for Edge Detection

Edge detection is the core of any scanner app. I used OpenCV's Canny edge detection combined with contour finding:

val gray = Mat()
Imgproc.cvtColor(original, gray, Imgproc.COLOR_RGBA2GRAY)
Imgproc.Canny(gray, edges, 50.0, 150.0)
Enter fullscreen mode Exit fullscreen mode

PDF Generation with iText

For PDF creation, iText provides a robust API. The challenge was keeping the library size reasonable — I used iText 7 core module only.

OCR with ML Kit

Google's ML Kit makes on-device OCR simple and accurate. It works offline, which is crucial for users in areas with limited connectivity.

Challenges

  1. Performance on low-end devices — Had to optimize image processing to avoid OOM errors
  2. Camera compatibility — Different Android OEMs implement camera APIs differently
  3. APK size — Kept it under 15MB by using modular dependencies
  4. Offline OCR — ML Kit handles this well but language packs add size

What I Learned

  • Start with MVP features (scan + save)
  • Optimize for the weakest devices your users have
  • Test on real devices, not just emulators
  • Offline-first is not optional for emerging markets

Open Source

The full source code is available: https://github.com/fanei/scan

Contributions, issues, and suggestions are welcome!

Try the App

Google Play: https://play.google.com/store/apps/details?id=com.smartscan.smartscan

100% free, no ads, no watermarks. Built for everyone.

Top comments (0)