CLEARDESK TOOLS

How Paperly's scanner source is designed

Everything below describes what the source code does, read from the code. None of it was run on a device or in a test run for this note. No speed, accuracy, battery, privacy or offline claim is made, and the current build has not been device-tested by this team.

1. Edge detection written in Kotlin

A pure-Kotlin detector works on a luminance grid downscaled to a 288-pixel long side: Otsu threshold, then the largest connected bright region (it must cover at least 12% of the frame, otherwise there is no result), then corner cells from the min and max of x+y and x-y. A small tracker averages the last six detections and reports steady when corner movement stays under a set limit. The app also includes Google's ML Kit document scanner as a separate guided-scan path. Limits: tuned values come from the code, not from measured accuracy. Behaviour on real camera frames is untested here.

2. OCR layer

ML Kit recognizers for Latin, Devanagari, Chinese, Japanese and Korean are created on first use and cached. An "Auto" mode tries them in a fixed order and stops once a result has 40 or more non-space characters. A recognizer error returns an empty result instead of stopping the sweep. Bitmap recycling is handed to the ML task's completion listener so a cancelled caller does not recycle pixels still in use. A catalog of extra Tesseract language models exists, pinned by commit, size and SHA-256. Models download on first use. Limits: recognition accuracy is not tested and the catalog's language count is not verified.

3. PDF output

PDFs are written to a temp file in the same folder and renamed into place. If the job is cancelled between save and rename, the temp file is deleted. Split writes parts into a batch folder and deletes the batch on failure. Each export goes into its own folder so a later export does not change the bytes an earlier share is still reading. Password protection uses AES-256 with a 127 UTF-8 byte password cap. Limits stated in code: 1 to 1,000 pages per document and 200 MB total for a merge. Not verified: behaviour on a real device under memory pressure.

4. Tests

Unit tests use Robolectric and screenshot tests use Roborazzi, per the build files. The edge detector takes a plain grid, so tests can feed it synthetic grids. This note does not claim any test result.

Back to the Paperly demo Source listing on Payhip