A fully functional Android document/receipt/page scanner app built with CameraX and pure Kotlin.
- Live camera preview with real-time document edge detection overlay
- Auto-capture — holds steady detection for ~0.4s then captures automatically
- Perspective correction — bilinear quad-to-rect warp crops and straightens documents
- Filters: Original, Grayscale, Black & White, Enhanced (contrast + sharpness boost)
- Rotate captured images 90° at a time
- Export to JPG (saved to device Pictures folder)
- Export to PDF using Android's native PdfDocument API
- Share via any app
- Document gallery with grid thumbnails and long-press to delete
- Flash toggle, manual shutter button, auto-capture toggle
- Open Android Studio (Electric Eel or newer recommended)
- Click File → Open and select the
DocScannerfolder (this directory) - Wait for Gradle sync to complete (first sync downloads dependencies, takes 1–3 min)
- Connect an Android device (API 24+) or start an emulator
- Click Run ▶
Note: Camera features require a real device for best results. Emulator camera is limited.
DocScanner/
├── app/src/main/
│ ├── java/com/docscanner/
│ │ ├── MainActivity.kt # Document gallery home screen
│ │ ├── CameraActivity.kt # Live camera + capture
│ │ ├── ReviewActivity.kt # Post-capture review & export
│ │ ├── processor/
│ │ │ ├── DocumentDetector.kt # Edge detection & corner finding
│ │ │ └── ImageProcessor.kt # Perspective warp, filters, sharpen
│ │ ├── data/
│ │ │ ├── ScannedDocument.kt # Data model
│ │ │ └── DocumentRepository.kt # File-based persistence
│ │ ├── adapter/
│ │ │ └── ScannedDocAdapter.kt # RecyclerView adapter
│ │ ├── views/
│ │ │ └── DocumentOverlayView.kt # Green corner overlay on preview
│ │ └── utils/
│ │ ├── BitmapUtils.kt
│ │ └── PdfExporter.kt
│ ├── res/ # Layouts, drawables, strings, themes
│ └── AndroidManifest.xml
└── app/build.gradle # Dependencies (CameraX, Glide, Coroutines)
| Library | Purpose |
|---|---|
| CameraX 1.3.1 | Camera preview, image capture, frame analysis |
| Kotlin Coroutines | Async image processing off the main thread |
| Glide 4.16 | Thumbnail loading in grid |
| Android PdfDocument | Native PDF export (no extra library needed) |
| Material Components | UI components |
- CameraX ImageAnalysis delivers frames at up to 30fps
- Each frame is converted YUV→Bitmap and passed to
DocumentDetector - The detector runs Gaussian blur → Sobel edge detection → dilation → boundary scanning to find the document quadrilateral
DocumentOverlayViewdraws the green corner brackets over the live preview- When Capture is pressed (or auto-capture triggers),
ImageCapturetakes a full-resolution photo ImageProcessor.perspectiveCorrect()applies a bilinear quad-to-rect warp to straighten the document- Auto-enhance applies contrast adjustment and a 3×3 sharpening convolution
- Result is saved as JPEG to external app storage and indexed in a JSON manifest
For production-grade accuracy, replace DocumentDetector.kt with:
- ML Kit Document Scanner API (
com.google.mlkit:document-scanner) — Google's on-device model - OpenCV for Android — full contour/Hough-based detection
The rest of the pipeline (capture, warp, filter, export) stays the same.
CAMERA— live preview and captureREAD_MEDIA_IMAGES/READ_EXTERNAL_STORAGE— saving to gallery (Android 13+)WRITE_EXTERNAL_STORAGE— saving on Android ≤ 28