Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][locker] Add document scanner UI

laurenspriem6f892fdf17de
diff --git a/mobile/apps/locker/android/app/src/main/AndroidManifest.xml b/mobile/apps/locker/android/app/src/main/AndroidManifest.xml
index ceffd1f6f8..3b0148afe3 100644
--- a/mobile/apps/locker/android/app/src/main/AndroidManifest.xml
+++ b/mobile/apps/locker/android/app/src/main/AndroidManifest.xml
@@ -8,6 +8,7 @@
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29"
+ tools:replace="android:maxSdkVersion"
tools:ignore="ScopedStorage" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
diff --git a/mobile/apps/locker/ios/Podfile.lock b/mobile/apps/locker/ios/Podfile.lock
index 3b74266e8c..1272efcd17 100644
--- a/mobile/apps/locker/ios/Podfile.lock
+++ b/mobile/apps/locker/ios/Podfile.lock
@@ -1,6 +1,8 @@
PODS:
- app_links (6.4.1):
- Flutter
+ - camera_avfoundation (0.0.1):
+ - Flutter
- cupertino_http (0.0.1):
- Flutter
- FlutterMacOS
@@ -94,6 +96,7 @@ PODS:
DEPENDENCIES:
- app_links (from `.symlinks/plugins/app_links/ios`)
+ - camera_avfoundation (from `.symlinks/plugins/camera_avfoundation/ios`)
- cupertino_http (from `.symlinks/plugins/cupertino_http/darwin`)
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- ente_locker_frb (from `.symlinks/plugins/ente_locker_frb/ios`)
@@ -130,6 +133,8 @@ SPEC REPOS:
EXTERNAL SOURCES:
app_links:
:path: ".symlinks/plugins/app_links/ios"
+ camera_avfoundation:
+ :path: ".symlinks/plugins/camera_avfoundation/ios"
cupertino_http:
:path: ".symlinks/plugins/cupertino_http/darwin"
device_info_plus:
@@ -181,6 +186,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
app_links: 3dbc685f76b1693c66a6d9dd1e9ab6f73d97dc0a
+ camera_avfoundation: 5675ca25298b6f81fa0a325188e7df62cc217741
cupertino_http: 94ac07f5ff090b8effa6c5e2c47871d48ab7c86c
device_info_plus: 21fcca2080fbcd348be798aa36c3e5ed849eefbe
DKImagePickerController: 946cec48c7873164274ecc4624d19e3da4c1ef3c
diff --git a/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart b/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart
new file mode 100644
index 0000000000..97717eb5ee
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/auto_capture_controller.dart
@@ -0,0 +1,112 @@
+import 'package:locker/services/scanner/scanner_models.dart';
+
+enum AutoCaptureState { searching, arming, cooldown }
+
+/// Time-based state machine for hands-free capture, fed once per analyzed
+/// frame. A stabilizer-approved quad that keeps passing the gates for
+/// [armHold] fires a capture, after which the frame must stay quad-free for
+/// [clearHold] (with no capture processing pending) before re-arming, so a
+/// document left in view is captured exactly once.
+class AutoCaptureController {
+ /// How long an eligible quad must be held before firing.
+ static const armHold = Duration(milliseconds: 1200);
+
+ /// Ineligible time tolerated while arming before progress resets.
+ static const armGrace = Duration(milliseconds: 250);
+
+ /// How long the frame must stay quad-free after a capture to re-arm.
+ static const clearHold = Duration(milliseconds: 700);
+
+ /// Cap on per-frame elapsed time, so stalls don't jump the timers.
+ static const maxFrameGap = Duration(milliseconds: 200);
+
+ /// Minimum quad area as a fraction of the mask area.
+ static const minAreaFraction = 0.15;
+
+ AutoCaptureState _state = AutoCaptureState.searching;
+ double _progress = 0;
+ Duration _graceElapsed = Duration.zero;
+ Duration _clearElapsed = Duration.zero;
+ DateTime? _lastFrameAt;
+
+ AutoCaptureState get state => _state;
+
+ /// Arming progress in [0, 1]; 0 outside [AutoCaptureState.arming].
+ double get progress => _progress;
+
+ /// Advances the machine with the stabilizer output for one frame. Returns
+ /// true when the hold completed and a capture should fire now.
+ bool onFrame(ScanQuad? stableQuad, {required bool captureBusy}) {
+ final now = DateTime.now();
+ final last = _lastFrameAt;
+ _lastFrameAt = now;
+ var dt = last == null ? Duration.zero : now.difference(last);
+ if (dt.isNegative) dt = Duration.zero;
+ if (dt > maxFrameGap) dt = maxFrameGap;
+
+ final eligible =
+ stableQuad != null && _areaFraction(stableQuad) >= minAreaFraction;
+
+ switch (_state) {
+ case AutoCaptureState.searching:
+ if (eligible) {
+ _state = AutoCaptureState.arming;
+ _progress = 0;
+ _graceElapsed = Duration.zero;
+ }
+ return false;
+ case AutoCaptureState.arming:
+ if (eligible) {
+ _graceElapsed = Duration.zero;
+ _progress += dt.inMicroseconds / armHold.inMicroseconds;
+ if (_progress >= 1) {
+ notifyCaptureStarted();
+ return true;
+ }
+ } else {
+ _graceElapsed += dt;
+ if (_graceElapsed >= armGrace) {
+ _state = AutoCaptureState.searching;
+ _progress = 0;
+ }
+ }
+ return false;
+ case AutoCaptureState.cooldown:
+ if (stableQuad != null || captureBusy) {
+ _clearElapsed = Duration.zero;
+ } else {
+ _clearElapsed += dt;
+ if (_clearElapsed >= clearHold) {
+ _state = AutoCaptureState.searching;
+ }
+ }
+ return false;
+ }
+ }
+
+ /// Disarms until the document leaves the frame. Called for every capture,
+ /// manual or automatic, so a document still in view is not re-captured.
+ void notifyCaptureStarted() {
+ _state = AutoCaptureState.cooldown;
+ _progress = 0;
+ _clearElapsed = Duration.zero;
+ }
+
+ void reset() {
+ _state = AutoCaptureState.searching;
+ _progress = 0;
+ _graceElapsed = Duration.zero;
+ _clearElapsed = Duration.zero;
+ _lastFrameAt = null;
+ }
+
+ static double _areaFraction(ScanQuad quad) {
+ var doubled = 0.0;
+ for (var i = 0; i < 4; i++) {
+ final a = quad.corners[i];
+ final b = quad.corners[(i + 1) % 4];
+ doubled += a.dx * b.dy - b.dx * a.dy;
+ }
+ return doubled.abs() / 2 / (ScanQuad.maskSide * ScanQuad.maskSide);
+ }
+}
diff --git a/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart b/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart
new file mode 100644
index 0000000000..c012f26301
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/document_scanner_service.dart
@@ -0,0 +1,56 @@
+import 'dart:typed_data';
+
+import 'package:locker/services/scanner/scanner_models.dart';
+
+/// Seam between the scanner UI and the native processing pipeline.
+///
+/// Implementations own the storage of page artifacts (temp files); callers
+/// own page lifecycle via [disposePage].
+abstract class DocumentScannerService {
+ Future<void> init();
+
+ bool get isReady;
+
+ /// Live document detection on an Android YUV420 preview frame.
+ /// Returns the detected quad in mask space of the upright
+ /// ([rotationDegrees]-applied) frame, or null when nothing is detected.
+ Future<ScanQuad?> detectLiveYuv({
+ required Uint8List y,
+ required Uint8List u,
+ required Uint8List v,
+ required int yRowStride,
+ required int uvRowStride,
+ required int uvPixelStride,
+ required int width,
+ required int height,
+ required int rotationDegrees,
+ });
+
+ /// Live document detection on an iOS BGRA8888 preview frame.
+ Future<ScanQuad?> detectLiveBgra(
+ Uint8List bytes,
+ int rowStride,
+ int width,
+ int height,
+ int rotationDegrees,
+ );
+
+ /// Detects the document in [capturedJpeg], applies perspective crop,
+ /// auto color mode and enhancement at high quality.
+ Future<ScannedPage> processCapture(Uint8List capturedJpeg);
+
+ /// Re-renders [page] from its source with a manual [quad] (mask space)
+ /// and/or [rotationDegrees]. Returns a page that supersedes [page]; its
+ /// processedJpeg is a new file and superseded render artifacts are cleaned
+ /// up by the service.
+ Future<ScannedPage> updatePage(
+ ScannedPage page, {
+ ScanQuad? quad,
+ int? rotationDegrees,
+ });
+
+ /// Deletes all files backing [page].
+ Future<void> disposePage(ScannedPage page);
+
+ Future<void> dispose();
+}
diff --git a/mobile/apps/locker/lib/services/scanner/pdf_writer.dart b/mobile/apps/locker/lib/services/scanner/pdf_writer.dart
new file mode 100644
index 0000000000..eae357717b
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/pdf_writer.dart
@@ -0,0 +1,207 @@
+import 'dart:convert';
+import 'dart:typed_data';
+
+/// Minimal PDF writer: one page per JPEG, each embedded verbatim as a
+/// DCTDecode image XObject. Only the JPEG's SOF header is parsed (for
+/// dimensions and color space); the compressed bytes are never touched.
+
+class JpegInfo {
+ const JpegInfo({
+ required this.width,
+ required this.height,
+ required this.components,
+ });
+
+ final int width;
+ final int height;
+ final int components;
+
+ String get pdfColorSpace => switch (components) {
+ 1 => '/DeviceGray',
+ 4 => '/DeviceCMYK',
+ _ => '/DeviceRGB',
+ };
+
+ static JpegInfo parse(Uint8List bytes) {
+ if (bytes.length < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8) {
+ throw const FormatException('not a JPEG stream');
+ }
+ var offset = 2;
+ while (offset + 3 < bytes.length) {
+ if (bytes[offset] != 0xFF) {
+ offset++;
+ continue;
+ }
+ final marker = bytes[offset + 1];
+ // Standalone markers carry no length.
+ if (marker == 0xD8 ||
+ marker == 0x01 ||
+ (marker >= 0xD0 && marker <= 0xD7)) {
+ offset += 2;
+ continue;
+ }
+ if (marker == 0xD9 || marker == 0xDA) break;
+ if (offset + 3 >= bytes.length) break;
+ final length = (bytes[offset + 2] << 8) | bytes[offset + 3];
+ final isFrameHeader =
+ marker >= 0xC0 &&
+ marker <= 0xCF &&
+ marker != 0xC4 &&
+ marker != 0xC8 &&
+ marker != 0xCC;
+ if (isFrameHeader) {
+ if (offset + 9 >= bytes.length) break;
+ return JpegInfo(
+ height: (bytes[offset + 5] << 8) | bytes[offset + 6],
+ width: (bytes[offset + 7] << 8) | bytes[offset + 8],
+ components: bytes[offset + 9],
+ );
+ }
+ offset += 2 + length;
+ }
+ throw const FormatException('no JPEG frame header found');
+ }
+}
+
+class PdfPageSpec {
+ const PdfPageSpec({
+ required this.jpeg,
+ required this.widthMm,
+ required this.heightMm,
+ });
+
+ final Uint8List jpeg;
+ final double widthMm;
+ final double heightMm;
+}
+
+/// Shrinks a sheet until it fits inside A4's long edge and Letter's short
+/// edge; never enlarges.
+({double widthMm, double heightMm}) constrainToMaxFormat(
+ double widthMm,
+ double heightMm,
+) {
+ const maxDim = 297.0;
+ const minDim = 215.9;
+ final scale = [
+ maxDim / (widthMm > heightMm ? widthMm : heightMm),
+ minDim / (widthMm < heightMm ? widthMm : heightMm),
+ 1.0,
+ ].reduce((a, b) => a < b ? a : b);
+ return (widthMm: widthMm * scale, heightMm: heightMm * scale);
+}
+
+const double pointsPerMm = 72.0 / 25.4;
+
+class PdfWriter {
+ const PdfWriter({required this.creator});
+
+ final String creator;
+
+ Uint8List build(List<PdfPageSpec> pages, {DateTime? creationDate}) {
+ final out = BytesBuilder(copy: false);
+ final offsets = <int>[];
+
+ void raw(List<int> bytes) => out.add(bytes);
+ void text(String value) => raw(latin1.encode(value));
+
+ // Objects: 1 catalog, 2 page tree, 3 info, then page/contents/image per
+ // page.
+ int pageObj(int index) => 4 + index * 3;
+ int contentObj(int index) => pageObj(index) + 1;
+ int imageObj(int index) => pageObj(index) + 2;
+ final objectCount = 3 + pages.length * 3;
+
+ void startObject(int number) {
+ while (offsets.length < number) {
+ offsets.add(0);
+ }
+ offsets[number - 1] = out.length;
+ text('$number 0 obj\n');
+ }
+
+ text('%PDF-1.4\n');
+ raw([0x25, 0xE2, 0xE3, 0xCF, 0xD3, 0x0A]);
+
+ startObject(1);
+ text('<< /Type /Catalog /Pages 2 0 R >>\nendobj\n');
+
+ startObject(2);
+ final kids = List.generate(
+ pages.length,
+ (i) => '${pageObj(i)} 0 R',
+ ).join(' ');
+ text('<< /Type /Pages /Kids [$kids] /Count ${pages.length} >>\nendobj\n');
+
+ startObject(3);
+ final stamp = _pdfDate(creationDate ?? DateTime.now());
+ text(
+ '<< /Producer (${_escape(creator)}) /Creator (${_escape(creator)}) '
+ '/CreationDate ($stamp) >>\nendobj\n',
+ );
+
+ for (var i = 0; i < pages.length; i++) {
+ final page = pages[i];
+ final info = JpegInfo.parse(page.jpeg);
+ final widthPt = page.widthMm * pointsPerMm;
+ final heightPt = page.heightMm * pointsPerMm;
+
+ startObject(pageObj(i));
+ text(
+ '<< /Type /Page /Parent 2 0 R '
+ '/MediaBox [0 0 ${_num(widthPt)} ${_num(heightPt)}] '
+ '/Resources << /XObject << /Im0 ${imageObj(i)} 0 R >> >> '
+ '/Contents ${contentObj(i)} 0 R >>\nendobj\n',
+ );
+
+ final content =
+ 'q\n${_num(widthPt)} 0 0 ${_num(heightPt)} 0 0 cm\n/Im0 Do\nQ\n';
+ final contentBytes = latin1.encode(content);
+ startObject(contentObj(i));
+ text('<< /Length ${contentBytes.length} >>\nstream\n');
+ raw(contentBytes);
+ text('endstream\nendobj\n');
+
+ startObject(imageObj(i));
+ text(
+ '<< /Type /XObject /Subtype /Image /Width ${info.width} '
+ '/Height ${info.height} /ColorSpace ${info.pdfColorSpace} '
+ '/BitsPerComponent 8 /Filter /DCTDecode '
+ '/Length ${page.jpeg.length} >>\nstream\n',
+ );
+ raw(page.jpeg);
+ text('\nendstream\nendobj\n');
+ }
+
+ final xrefOffset = out.length;
+ text('xref\n0 ${objectCount + 1}\n');
+ text('0000000000 65535 f \n');
+ for (var i = 0; i < objectCount; i++) {
+ text('${offsets[i].toString().padLeft(10, '0')} 00000 n \n');
+ }
+ text(
+ 'trailer\n<< /Size ${objectCount + 1} /Root 1 0 R /Info 3 0 R >>\n'
+ 'startxref\n$xrefOffset\n%%EOF\n',
+ );
+
+ return out.takeBytes();
+ }
+
+ static String _num(double value) {
+ final rounded = (value * 1000).roundToDouble() / 1000;
+ if (rounded == rounded.roundToDouble()) return rounded.toStringAsFixed(0);
+ return rounded.toString();
+ }
+
+ static String _escape(String value) => value
+ .replaceAll(r'\', r'\\')
+ .replaceAll('(', r'\(')
+ .replaceAll(')', r'\)');
+
+ static String _pdfDate(DateTime when) {
+ final utc = when.toUtc();
+ String two(int v) => v.toString().padLeft(2, '0');
+ return 'D:${utc.year}${two(utc.month)}${two(utc.day)}'
+ '${two(utc.hour)}${two(utc.minute)}${two(utc.second)}Z';
+ }
+}
diff --git a/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart b/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart
new file mode 100644
index 0000000000..db19280ba7
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/rust_document_scanner_service.dart
@@ -0,0 +1,291 @@
+import 'dart:io';
+import 'dart:typed_data';
+import 'dart:ui';
+
+import 'package:locker/services/scanner/document_scanner_service.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+import 'package:locker/src/rust/api/document_scanner_api.dart';
+import 'package:logging/logging.dart';
+import 'package:path/path.dart' as p;
+import 'package:path_provider/path_provider.dart';
+
+/// Rust-backed implementation: ONNX segmentation for detection, perspective
+/// extraction and enhancement in the native pipeline.
+class RustDocumentScannerService implements DocumentScannerService {
+ static const int _maxPixels = 4000000;
+
+ final _logger = Logger('RustDocumentScannerService');
+ Directory? _dir;
+ ScannerSession? _session;
+ Future<void>? _initialization;
+ final Map<String, ({int width, int height})> _sourceDims = {};
+ int _counter = 0;
+ DateTime _lastLiveLog = DateTime.fromMillisecondsSinceEpoch(0);
+
+ @override
+ Future<void> init() {
+ return _initialization ??= () async {
+ try {
+ await _initialize();
+ } catch (_) {
+ _initialization = null;
+ rethrow;
+ }
+ }();
+ }
+
+ Future<void> _initialize() async {
+ final watch = Stopwatch()..start();
+ try {
+ final tmp = await getTemporaryDirectory();
+ _dir = await Directory(
+ p.join(tmp.path, 'document_scan'),
+ ).create(recursive: true);
+ final support = await getApplicationSupportDirectory();
+ // The Rust side downloads the model into this store on first use and
+ // reuses the verified copy afterwards.
+ _session = await ScannerSession.create(
+ assetsDir: p.join(support.path, 'assets'),
+ );
+ _logger.info(
+ 'Initialized: session ready in ${watch.elapsedMilliseconds}ms',
+ );
+ } catch (e, s) {
+ _logger.severe('Initialization failed', e, s);
+ rethrow;
+ }
+ }
+
+ @override
+ bool get isReady => _session != null;
+
+ ScannerSession get _readySession {
+ final session = _session;
+ if (session == null) {
+ throw StateError('DocumentScannerService not initialized');
+ }
+ return session;
+ }
+
+ Directory get _root {
+ final dir = _dir;
+ if (dir == null) {
+ throw StateError('DocumentScannerService not initialized');
+ }
+ return dir;
+ }
+
+ @override
+ Future<ScanQuad?> detectLiveYuv({
+ required Uint8List y,
+ required Uint8List u,
+ required Uint8List v,
+ required int yRowStride,
+ required int uvRowStride,
+ required int uvPixelStride,
+ required int width,
+ required int height,
+ required int rotationDegrees,
+ }) async {
+ final session = _session;
+ if (session == null) return null;
+ try {
+ final quad = await session.liveDetectYuv420(
+ y: y,
+ u: u,
+ v: v,
+ layout: RustPlaneLayout(
+ width: width,
+ height: height,
+ yRowStride: yRowStride,
+ uvRowStride: uvRowStride,
+ uvPixelStride: uvPixelStride,
+ ),
+ rotationDegrees: rotationDegrees,
+ );
+ return quad == null ? null : _maskQuadFromRust(quad);
+ } catch (e) {
+ _logLiveThrottled(e);
+ return null;
+ }
+ }
+
+ @override
+ Future<ScanQuad?> detectLiveBgra(
+ Uint8List bytes,
+ int rowStride,
+ int width,
+ int height,
+ int rotationDegrees,
+ ) async {
+ final session = _session;
+ if (session == null) return null;
+ try {
+ final quad = await session.liveDetectBgra(
+ bgra: bytes,
+ rowStride: rowStride,
+ width: width,
+ height: height,
+ rotationDegrees: rotationDegrees,
+ );
+ return quad == null ? null : _maskQuadFromRust(quad);
+ } catch (e) {
+ _logLiveThrottled(e);
+ return null;
+ }
+ }
+
+ void _logLiveThrottled(Object error) {
+ final now = DateTime.now();
+ if (now.difference(_lastLiveLog).inSeconds < 10) return;
+ _lastLiveLog = now;
+ _logger.warning('Live detection frame failed', error);
+ }
+
+ @override
+ Future<ScannedPage> processCapture(Uint8List capturedJpeg) async {
+ try {
+ final result = await _readySession.processCapture(
+ imageBytes: capturedJpeg,
+ options: const RustScanOptions(
+ maxPixels: _maxPixels,
+ rotationDegrees: 0,
+ ),
+ );
+ final id = '${DateTime.now().microsecondsSinceEpoch}_${_counter++}';
+ final source = File(p.join(_root.path, '${id}_src.jpg'));
+ final processed = File(p.join(_root.path, '${id}_r0.jpg'));
+ await source.writeAsBytes(capturedJpeg, flush: true);
+ await processed.writeAsBytes(result.processedImage, flush: true);
+ _sourceDims[id] = (
+ width: result.sourceWidth,
+ height: result.sourceHeight,
+ );
+ final quad = result.quad;
+ return ScannedPage(
+ id: id,
+ sourceJpeg: source,
+ processedJpeg: processed,
+ quad: quad == null
+ ? ScanQuad.fullFrame()
+ : _maskQuadFromSource(
+ quad,
+ result.sourceWidth,
+ result.sourceHeight,
+ ),
+ rotationDegrees: 0,
+ resolvedColorMode: _colorModeFromRust(result.colorMode),
+ width: result.outputWidth,
+ height: result.outputHeight,
+ );
+ } catch (e, s) {
+ _logger.severe('processCapture failed', e, s);
+ rethrow;
+ }
+ }
+
+ @override
+ Future<ScannedPage> updatePage(
+ ScannedPage page, {
+ ScanQuad? quad,
+ int? rotationDegrees,
+ }) async {
+ try {
+ final dims = _sourceDims[page.id];
+ if (dims == null) {
+ throw StateError('Unknown page ${page.id}');
+ }
+ final sourceBytes = await page.sourceJpeg.readAsBytes();
+ final result = await _readySession.reprocess(
+ sourceBytes: sourceBytes,
+ options: RustReprocessOptions(
+ quad: _sourceQuadFromMask(quad ?? page.quad, dims.width, dims.height),
+ rotationDegrees: rotationDegrees ?? page.rotationDegrees,
+ colorMode: _colorModeToRust(page.resolvedColorMode),
+ maxPixels: _maxPixels,
+ ),
+ );
+ final processed = File(
+ p.join(_root.path, '${page.id}_r${++_counter}.jpg'),
+ );
+ await processed.writeAsBytes(result.processedImage, flush: true);
+ if (page.processedJpeg.path != processed.path) {
+ await _deleteIfExists(page.processedJpeg);
+ }
+ return page.copyWith(
+ processedJpeg: processed,
+ quad: quad,
+ rotationDegrees: rotationDegrees,
+ width: result.outputWidth,
+ height: result.outputHeight,
+ );
+ } catch (e, s) {
+ _logger.severe('updatePage failed', e, s);
+ rethrow;
+ }
+ }
+
+ @override
+ Future<void> disposePage(ScannedPage page) async {
+ _sourceDims.remove(page.id);
+ await _deleteIfExists(page.sourceJpeg);
+ await _deleteIfExists(page.processedJpeg);
+ }
+
+ @override
+ Future<void> dispose() async {
+ _session?.dispose();
+ _session = null;
+ _initialization = null;
+ _sourceDims.clear();
+ _dir = null;
+ }
+
+ static ScanQuad _maskQuadFromRust(RustQuad quad) => ScanQuad([
+ Offset(quad.topLeft.x, quad.topLeft.y),
+ Offset(quad.topRight.x, quad.topRight.y),
+ Offset(quad.bottomRight.x, quad.bottomRight.y),
+ Offset(quad.bottomLeft.x, quad.bottomLeft.y),
+ ]);
+
+ /// Source pixel space -> 256 mask space.
+ static ScanQuad _maskQuadFromSource(RustQuad quad, int width, int height) {
+ final sx = ScanQuad.maskSide / width;
+ final sy = ScanQuad.maskSide / height;
+ Offset scale(RustPoint point) => Offset(point.x * sx, point.y * sy);
+ return ScanQuad([
+ scale(quad.topLeft),
+ scale(quad.topRight),
+ scale(quad.bottomRight),
+ scale(quad.bottomLeft),
+ ]);
+ }
+
+ /// 256 mask space -> source pixel space.
+ static RustQuad _sourceQuadFromMask(ScanQuad quad, int width, int height) {
+ final sx = width / ScanQuad.maskSide;
+ final sy = height / ScanQuad.maskSide;
+ RustPoint scale(Offset corner) =>
+ RustPoint(x: corner.dx * sx, y: corner.dy * sy);
+ return RustQuad(
+ topLeft: scale(quad.corners[0]),
+ topRight: scale(quad.corners[1]),
+ bottomRight: scale(quad.corners[2]),
+ bottomLeft: scale(quad.corners[3]),
+ );
+ }
+
+ static ScanColorMode _colorModeFromRust(RustColorMode mode) => switch (mode) {
+ RustColorMode.color => ScanColorMode.color,
+ RustColorMode.grayscale => ScanColorMode.grayscale,
+ };
+
+ static RustColorMode _colorModeToRust(ScanColorMode mode) => switch (mode) {
+ ScanColorMode.grayscale => RustColorMode.grayscale,
+ ScanColorMode.color || ScanColorMode.auto => RustColorMode.color,
+ };
+
+ static Future<void> _deleteIfExists(File file) async {
+ if (await file.exists()) await file.delete();
+ }
+}
diff --git a/mobile/apps/locker/lib/services/scanner/scan_geometry.dart b/mobile/apps/locker/lib/services/scanner/scan_geometry.dart
new file mode 100644
index 0000000000..8020ec3439
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/scan_geometry.dart
@@ -0,0 +1,84 @@
+import 'dart:math' as math;
+import 'dart:ui';
+
+import 'package:locker/services/scanner/scanner_models.dart';
+
+ScanQuad lerpQuad(ScanQuad a, ScanQuad b, double t) => ScanQuad([
+ for (var i = 0; i < 4; i++) Offset.lerp(a.corners[i], b.corners[i], t)!,
+]);
+
+double maxCornerDistance(ScanQuad a, ScanQuad b) {
+ var worst = 0.0;
+ for (var i = 0; i < 4; i++) {
+ final d = (a.corners[i] - b.corners[i]).distance;
+ if (d > worst) worst = d;
+ }
+ return worst;
+}
+
+/// Orders four corners clockwise by angle around their centroid; the first
+/// corner after sorting is the top-left role.
+ScanQuad orderClockwise(List<Offset> corners) {
+ assert(corners.length == 4);
+ final cx = corners.map((c) => c.dx).reduce((a, b) => a + b) / 4;
+ final cy = corners.map((c) => c.dy).reduce((a, b) => a + b) / 4;
+ final sorted = [...corners]
+ ..sort(
+ (a, b) => math
+ .atan2(a.dy - cy, a.dx - cx)
+ .compareTo(math.atan2(b.dy - cy, b.dx - cx)),
+ );
+ return ScanQuad(sorted);
+}
+
+/// The rectangle content of aspect ratio [aspect] occupies inside [container]
+/// under BoxFit.contain.
+Rect fittedRect(Size container, double aspect) {
+ final containerAspect = container.width / container.height;
+ final Size display = aspect > containerAspect
+ ? Size(container.width, container.width / aspect)
+ : Size(container.height * aspect, container.height);
+ return Rect.fromLTWH(
+ (container.width - display.width) / 2,
+ (container.height - display.height) / 2,
+ display.width,
+ display.height,
+ );
+}
+
+/// A quad is surfaced only once [requiredStableFrames] consecutive detections
+/// agree to within [maxCornerDrift] in mask space.
+class QuadStabilizer {
+ static const double maxCornerDrift = 20.0;
+ static const int requiredStableFrames = 3;
+
+ int _stableCount = 0;
+ ScanQuad? _lastRawQuad;
+
+ ScanQuad? update(ScanQuad? rawQuad) {
+ final previous = _lastRawQuad;
+ _lastRawQuad = rawQuad;
+
+ if (rawQuad == null) {
+ _stableCount = 0;
+ return null;
+ }
+ if (previous == null) {
+ _stableCount = 1;
+ return null;
+ }
+
+ if (maxCornerDistance(previous, rawQuad) < maxCornerDrift) {
+ _stableCount++;
+ } else {
+ _stableCount = 1;
+ }
+
+ return _stableCount >= requiredStableFrames ? rawQuad : null;
+ }
+
+ void reset() {
+ _stableCount = 0;
+ _lastRawQuad = null;
+ }
+}
diff --git a/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart b/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart
new file mode 100644
index 0000000000..34fd520aab
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/scan_session_controller.dart
@@ -0,0 +1,235 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:flutter/foundation.dart';
+import 'package:locker/services/scanner/document_scanner_service.dart';
+import 'package:locker/services/scanner/pdf_writer.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+import 'package:locker/services/scanner/scanner_service_locator.dart';
+import 'package:logging/logging.dart';
+import 'package:path/path.dart' as p;
+import 'package:path_provider/path_provider.dart';
+
+/// State of one scan session: the ordered page list, the capture-processing
+/// queue and the temp artifacts. Disposed when the scanner flow is left.
+class ScanSessionController extends ChangeNotifier {
+ ScanSessionController({DocumentScannerService? service})
+ : _service = service ?? documentScannerService;
+
+ static const _pdfExtension = '.pdf';
+
+ final _logger = Logger('ScanSessionController');
+ final DocumentScannerService _service;
+ final List<ScannedPage> _pages = [];
+ final List<File> _exports = [];
+ Future<void> _queue = Future.value();
+ int _pendingCount = 0;
+ bool _disposed = false;
+ Object? _lastError;
+ String? _fileName;
+
+ List<ScannedPage> get pages => List.unmodifiable(_pages);
+ int get pageCount => _pages.length;
+ bool get isProcessing => _pendingCount > 0;
+
+ /// PDF filename including the extension, once initialized.
+ String? get fileName => _fileName;
+
+ void ensureFileName(String fileName) {
+ _fileName ??= fileName;
+ }
+
+ /// Renames the PDF to [baseName] (extension re-appended). Empty or
+ /// separator-only input keeps the current name.
+ void renameFile(String baseName) {
+ var cleaned = baseName.replaceAll(RegExp(r'[/\\]'), '').trim();
+ if (cleaned.toLowerCase().endsWith(_pdfExtension)) {
+ cleaned = cleaned
+ .substring(0, cleaned.length - _pdfExtension.length)
+ .trim();
+ }
+ if (cleaned.isEmpty) return;
+ _fileName = '$cleaned$_pdfExtension';
+ notifyListeners();
+ }
+
+ ScannedPage? get lastPage => _pages.isEmpty ? null : _pages.last;
+
+ /// Returns and clears the most recent processing error, if any.
+ Object? takeLastError() {
+ final error = _lastError;
+ _lastError = null;
+ return error;
+ }
+
+ Future<void> init() => _service.init();
+
+ bool get isServiceReady => _service.isReady;
+
+ Future<ScanQuad?> detectLiveYuv({
+ required Uint8List y,
+ required Uint8List u,
+ required Uint8List v,
+ required int yRowStride,
+ required int uvRowStride,
+ required int uvPixelStride,
+ required int width,
+ required int height,
+ required int rotationDegrees,
+ }) => _service.detectLiveYuv(
+ y: y,
+ u: u,
+ v: v,
+ yRowStride: yRowStride,
+ uvRowStride: uvRowStride,
+ uvPixelStride: uvPixelStride,
+ width: width,
+ height: height,
+ rotationDegrees: rotationDegrees,
+ );
+
+ Future<ScanQuad?> detectLiveBgra(
+ Uint8List bytes,
+ int rowStride,
+ int width,
+ int height,
+ int rotationDegrees,
+ ) =>
+ _service.detectLiveBgra(bytes, rowStride, width, height, rotationDegrees);
+
+ /// Enqueues a capture; processing is sequential so page order follows
+ /// capture order.
+ void addCapture(Uint8List capturedJpeg) {
+ _pendingCount++;
+ notifyListeners();
+ _queue = _queue.then((_) async {
+ try {
+ final page = await _service.processCapture(capturedJpeg);
+ if (_disposed) {
+ await _service.disposePage(page);
+ return;
+ }
+ _pages.add(page);
+ } catch (e, s) {
+ _logger.severe('Failed to process capture', e, s);
+ _lastError = e;
+ } finally {
+ _pendingCount--;
+ if (!_disposed) notifyListeners();
+ }
+ });
+ }
+
+ /// Completes when every enqueued capture has been processed.
+ Future<void> waitForPending() async {
+ while (_pendingCount > 0) {
+ await _queue;
+ }
+ }
+
+ Future<void> updatePage(
+ String pageId, {
+ ScanQuad? quad,
+ int? rotationDegrees,
+ }) async {
+ final index = _pages.indexWhere((page) => page.id == pageId);
+ if (index < 0) return;
+ final updated = await _service.updatePage(
+ _pages[index],
+ quad: quad,
+ rotationDegrees: rotationDegrees,
+ );
+ if (_disposed) {
+ await _service.disposePage(updated);
+ return;
+ }
+ _pages[index] = updated;
+ notifyListeners();
+ }
+
+ Future<void> rotatePageClockwise(String pageId) async {
+ final index = _pages.indexWhere((page) => page.id == pageId);
+ if (index < 0) return;
+ await updatePage(
+ pageId,
+ rotationDegrees: (_pages[index].rotationDegrees + 90) % 360,
+ );
+ }
+
+ /// Moves a page; indices follow ReorderableListView's onReorder convention.
+ void reorderPage(int oldIndex, int newIndex) {
+ if (oldIndex < 0 || oldIndex >= _pages.length) return;
+ if (newIndex > oldIndex) newIndex -= 1;
+ newIndex = newIndex.clamp(0, _pages.length - 1);
+ if (newIndex == oldIndex) return;
+ final page = _pages.removeAt(oldIndex);
+ _pages.insert(newIndex, page);
+ notifyListeners();
+ }
+
+ Future<void> deletePage(String pageId) async {
+ final index = _pages.indexWhere((page) => page.id == pageId);
+ if (index < 0) return;
+ final page = _pages.removeAt(index);
+ notifyListeners();
+ await _service.disposePage(page);
+ }
+
+ /// Assembles all pages, in order, into a PDF named [fileName] in the temp
+ /// dir.
+ Future<File> buildPdf() async {
+ final fileName = _fileName;
+ if (fileName == null) {
+ throw StateError('fileName has not been set');
+ }
+ final specs = <PdfPageSpec>[];
+ for (final page in List.of(_pages)) {
+ final jpeg = await page.processedJpeg.readAsBytes();
+ final size = _pageSizeMm(jpeg);
+ specs.add(
+ PdfPageSpec(jpeg: jpeg, widthMm: size.widthMm, heightMm: size.heightMm),
+ );
+ }
+ final bytes = const PdfWriter(creator: 'Ente Locker').build(specs);
+ final tmp = await getTemporaryDirectory();
+ final dir = await Directory(
+ p.join(tmp.path, 'document_scan'),
+ ).create(recursive: true);
+ final file = File(p.join(dir.path, fileName));
+ await file.writeAsBytes(bytes, flush: true);
+ _exports.add(file);
+ return file;
+ }
+
+ /// Long edge scaled to A4 height, then constrained to standard formats.
+ static ({double widthMm, double heightMm}) _pageSizeMm(Uint8List jpeg) {
+ final info = JpegInfo.parse(jpeg);
+ const a4HeightMm = 297.0;
+ final longestPx = info.width > info.height ? info.width : info.height;
+ final scale = a4HeightMm / longestPx;
+ return constrainToMaxFormat(info.width * scale, info.height * scale);
+ }
+
+ Future<void> disposeSession() async {
+ if (_disposed) return;
+ _disposed = true;
+ await waitForPending();
+ for (final page in _pages) {
+ try {
+ await _service.disposePage(page);
+ } catch (e) {
+ _logger.warning('Failed to dispose page', e);
+ }
+ }
+ _pages.clear();
+ for (final file in _exports) {
+ try {
+ if (await file.exists()) await file.delete();
+ } catch (e) {
+ _logger.warning('Failed to delete export', e);
+ }
+ }
+ _exports.clear();
+ dispose();
+ }
+}
diff --git a/mobile/apps/locker/lib/services/scanner/scanner_models.dart b/mobile/apps/locker/lib/services/scanner/scanner_models.dart
new file mode 100644
index 0000000000..a981607647
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/scanner_models.dart
@@ -0,0 +1,73 @@
+import 'dart:io';
+import 'dart:ui';
+
+enum ScanColorMode { auto, color, grayscale }
+
+/// Four corners in the detector's 256x256 mask space, clockwise from
+/// top-left. Mask space maps proportionally onto the upright frame it
+/// describes (live preview frame or source capture).
+class ScanQuad {
+ const ScanQuad(this.corners);
+
+ static const double maskSide = 256;
+
+ final List<Offset> corners;
+
+ factory ScanQuad.fullFrame() => const ScanQuad([
+ Offset(0, 0),
+ Offset(maskSide, 0),
+ Offset(maskSide, maskSide),
+ Offset(0, maskSide),
+ ]);
+}
+
+class ScannedPage {
+ const ScannedPage({
+ required this.id,
+ required this.sourceJpeg,
+ required this.processedJpeg,
+ required this.quad,
+ required this.rotationDegrees,
+ required this.resolvedColorMode,
+ required this.width,
+ required this.height,
+ });
+
+ final String id;
+
+ /// The upright (EXIF-normalized) capture, kept for re-cropping.
+ final File sourceJpeg;
+
+ /// Rendered output; crop, enhancement and [rotationDegrees] are baked in.
+ final File processedJpeg;
+
+ /// Quad used for the render, in mask space of [sourceJpeg].
+ final ScanQuad quad;
+
+ final int rotationDegrees;
+
+ /// Color or grayscale after auto-detection resolved.
+ final ScanColorMode resolvedColorMode;
+
+ /// Dimensions of [processedJpeg].
+ final int width;
+ final int height;
+
+ ScannedPage copyWith({
+ File? processedJpeg,
+ ScanQuad? quad,
+ int? rotationDegrees,
+ ScanColorMode? resolvedColorMode,
+ int? width,
+ int? height,
+ }) => ScannedPage(
+ id: id,
+ sourceJpeg: sourceJpeg,
+ processedJpeg: processedJpeg ?? this.processedJpeg,
+ quad: quad ?? this.quad,
+ rotationDegrees: rotationDegrees ?? this.rotationDegrees,
+ resolvedColorMode: resolvedColorMode ?? this.resolvedColorMode,
+ width: width ?? this.width,
+ height: height ?? this.height,
+ );
+}
diff --git a/mobile/apps/locker/lib/services/scanner/scanner_service_locator.dart b/mobile/apps/locker/lib/services/scanner/scanner_service_locator.dart
new file mode 100644
index 0000000000..8942f9ba43
--- /dev/null
+++ b/mobile/apps/locker/lib/services/scanner/scanner_service_locator.dart
@@ -0,0 +1,7 @@
+import 'package:locker/services/scanner/document_scanner_service.dart';
+import 'package:locker/services/scanner/rust_document_scanner_service.dart';
+
+DocumentScannerService? _documentScannerService;
+
+DocumentScannerService get documentScannerService =>
+ _documentScannerService ??= RustDocumentScannerService();
diff --git a/mobile/apps/locker/lib/ui/components/save_to_locker_empty_state_widget.dart b/mobile/apps/locker/lib/ui/components/save_to_locker_empty_state_widget.dart
index 5b63df9b1e..9ef5f728b3 100644
--- a/mobile/apps/locker/lib/ui/components/save_to_locker_empty_state_widget.dart
+++ b/mobile/apps/locker/lib/ui/components/save_to_locker_empty_state_widget.dart
@@ -1,3 +1,5 @@
+import "dart:io";
+
import "package:ente_components/ente_components.dart";
import "package:flutter/material.dart";
import "package:hugeicons/hugeicons.dart";
@@ -15,9 +17,11 @@ class SaveToLockerEmptyStateWidget extends StatelessWidget {
const SaveToLockerEmptyStateWidget({
super.key,
required this.onUploadDocument,
+ required this.onUploadFiles,
});
final Future<bool> Function() onUploadDocument;
+ final Future<bool> Function(List<File> files) onUploadFiles;
@override
Widget build(BuildContext context) {
@@ -35,6 +39,7 @@ class SaveToLockerEmptyStateWidget extends StatelessWidget {
context,
option.type,
onUploadDocument: onUploadDocument,
+ onUploadFiles: onUploadFiles,
),
),
],
diff --git a/mobile/apps/locker/lib/ui/pages/home_page.dart b/mobile/apps/locker/lib/ui/pages/home_page.dart
index 8c0f796b6b..2acc79291d 100644
--- a/mobile/apps/locker/lib/ui/pages/home_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/home_page.dart
@@ -753,7 +753,10 @@ class _HomePageState extends UploaderPageState<HomePage>
right: Spacing.xl,
bottom: scrollBottomPadding,
),
- child: SaveToLockerEmptyStateWidget(onUploadDocument: addFile),
+ child: SaveToLockerEmptyStateWidget(
+ onUploadDocument: addFile,
+ onUploadFiles: uploadFiles,
+ ),
);
}
return SizedBox.expand(
@@ -831,6 +834,10 @@ class _HomePageState extends UploaderPageState<HomePage>
}
void _openSavePage() {
- showSaveBottomSheet(context, onUploadDocument: addFile);
+ showSaveBottomSheet(
+ context,
+ onUploadDocument: addFile,
+ onUploadFiles: uploadFiles,
+ );
}
}
diff --git a/mobile/apps/locker/lib/ui/pages/save_page.dart b/mobile/apps/locker/lib/ui/pages/save_page.dart
index b96d8423b8..f521793364 100644
--- a/mobile/apps/locker/lib/ui/pages/save_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/save_page.dart
@@ -1,14 +1,17 @@
import 'dart:async';
+import 'dart:io';
import "package:ente_components/ente_components.dart";
import 'package:ente_strings/ente_strings.dart';
+import 'package:ente_ui/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:hugeicons/hugeicons.dart';
import 'package:locker/ui/pages/account_credentials_page.dart';
import 'package:locker/ui/pages/personal_note_page.dart';
import 'package:locker/ui/pages/physical_records_page.dart';
+import 'package:locker/ui/pages/scanner/scanner_capture_page.dart';
-enum SaveOptionType { document, note, physicalRecord, credentials }
+enum SaveOptionType { scanDocument, file, note, physicalRecord, credentials }
class SaveOption {
const SaveOption({
@@ -24,14 +27,23 @@ class SaveOption {
final String description;
}
+const bool docScannerEnabled = false;
+
List<SaveOption> saveOptions(BuildContext context) {
final l10n = context.strings;
return [
+ if (docScannerEnabled)
+ SaveOption(
+ type: SaveOptionType.scanDocument,
+ icon: HugeIcons.strokeRoundedFileScan,
+ title: l10n.scanDocumentTitle,
+ description: l10n.scanDocumentDescription,
+ ),
SaveOption(
- type: SaveOptionType.document,
+ type: SaveOptionType.file,
icon: HugeIcons.strokeRoundedFile01,
- title: l10n.saveDocumentTitle,
- description: l10n.saveDocumentDescription,
+ title: l10n.saveFileTitle,
+ description: l10n.saveFileDescription,
),
SaveOption(
type: SaveOptionType.note,
@@ -58,10 +70,32 @@ void handleSaveOption(
BuildContext context,
SaveOptionType type, {
required Future<bool> Function() onUploadDocument,
+ required Future<bool> Function(List<File> files) onUploadFiles,
VoidCallback? onCancelWithoutSaving,
}) {
switch (type) {
- case SaveOptionType.document:
+ case SaveOptionType.scanDocument:
+ if (!Platform.isAndroid && !Platform.isIOS) {
+ showShortToast(context, context.strings.scannerNotSupportedOnDevice);
+ onCancelWithoutSaving?.call();
+ return;
+ }
+ unawaited(
+ Navigator.of(context)
+ .push<bool>(
+ MaterialPageRoute(
+ builder: (context) =>
+ ScannerCapturePage(onUploadFiles: onUploadFiles),
+ ),
+ )
+ .then((didSave) {
+ if (didSave != true) {
+ onCancelWithoutSaving?.call();
+ }
+ }),
+ );
+ return;
+ case SaveOptionType.file:
unawaited(
onUploadDocument().then((didUpload) {
if (!didUpload) {
@@ -101,12 +135,14 @@ void handleSaveOption(
Future<void> showSaveBottomSheet(
BuildContext context, {
required Future<bool> Function() onUploadDocument,
+ required Future<bool> Function(List<File> files) onUploadFiles,
}) {
return showBottomSheetComponent<void>(
context: context,
builder: (_) => SaveBottomSheet(
rootContext: context,
onUploadDocument: onUploadDocument,
+ onUploadFiles: onUploadFiles,
),
);
}
@@ -116,10 +152,12 @@ class SaveBottomSheet extends StatelessWidget {
super.key,
required this.rootContext,
required this.onUploadDocument,
+ required this.onUploadFiles,
});
final BuildContext rootContext;
final Future<bool> Function() onUploadDocument;
+ final Future<bool> Function(List<File> files) onUploadFiles;
@override
Widget build(BuildContext context) {
@@ -192,6 +230,7 @@ class SaveBottomSheet extends StatelessWidget {
showSaveBottomSheet(
navigator.context,
onUploadDocument: onUploadDocument,
+ onUploadFiles: onUploadFiles,
);
});
}
@@ -200,6 +239,7 @@ class SaveBottomSheet extends StatelessWidget {
context,
type,
onUploadDocument: onUploadDocument,
+ onUploadFiles: onUploadFiles,
onCancelWithoutSaving: reopenSheet,
);
}
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart b/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart
new file mode 100644
index 0000000000..a7d95e8939
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scan_quad_overlay.dart
@@ -0,0 +1,99 @@
+import 'package:flutter/material.dart';
+import 'package:flutter/scheduler.dart' show Ticker;
+import 'package:locker/services/scanner/scan_geometry.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+
+/// Draws the stabilized detection quad over the preview, easing towards each
+/// new target a little every frame. [quad] is in mask space and is stretched
+/// onto this widget's box, which must exactly cover the preview frame.
+class ScanQuadOverlay extends StatefulWidget {
+ const ScanQuadOverlay({super.key, required this.quad, required this.color});
+
+ static const double _smoothing = 0.15;
+
+ final ScanQuad? quad;
+ final Color color;
+
+ @override
+ State<ScanQuadOverlay> createState() => _ScanQuadOverlayState();
+}
+
+class _ScanQuadOverlayState extends State<ScanQuadOverlay>
+ with SingleTickerProviderStateMixin {
+ Ticker? _ticker;
+ ScanQuad? _displayed;
+
+ @override
+ void initState() {
+ super.initState();
+ _ticker = createTicker(_onFrame)..start();
+ }
+
+ @override
+ void dispose() {
+ _ticker?.dispose();
+ super.dispose();
+ }
+
+ void _onFrame(Duration _) {
+ final target = widget.quad;
+ if (target == null) {
+ if (_displayed != null) setState(() => _displayed = null);
+ return;
+ }
+ final current = _displayed;
+ final next = current == null
+ ? target
+ : lerpQuad(current, target, ScanQuadOverlay._smoothing);
+ setState(() => _displayed = next);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return IgnorePointer(
+ child: CustomPaint(
+ size: Size.infinite,
+ painter: _QuadPainter(quad: _displayed, color: widget.color),
+ ),
+ );
+ }
+}
+
+class _QuadPainter extends CustomPainter {
+ const _QuadPainter({required this.quad, required this.color});
+
+ final ScanQuad? quad;
+ final Color color;
+
+ @override
+ void paint(Canvas canvas, Size size) {
+ final source = quad;
+ if (source == null || size.isEmpty) return;
+ final points = [
+ for (final c in source.corners)
+ Offset(
+ c.dx / ScanQuad.maskSide * size.width,
+ c.dy / ScanQuad.maskSide * size.height,
+ ),
+ ];
+ final path = Path()..addPolygon(points, true);
+ canvas.drawPath(
+ path,
+ Paint()
+ ..color = color.withValues(alpha: 0.16)
+ ..style = PaintingStyle.fill,
+ );
+ canvas.drawPath(
+ path,
+ Paint()
+ ..color = color
+ ..style = PaintingStyle.stroke
+ ..strokeWidth = 4
+ ..strokeJoin = StrokeJoin.round,
+ );
+ }
+
+ @override
+ bool shouldRepaint(_QuadPainter oldDelegate) =>
+ oldDelegate.quad != quad || oldDelegate.color != color;
+}
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
new file mode 100644
index 0000000000..543e2c47ad
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_capture_page.dart
@@ -0,0 +1,718 @@
+import 'dart:async';
+import 'dart:io';
+import 'dart:math' as math;
+
+import 'package:camera/camera.dart';
+import 'package:ente_components/ente_components.dart';
+import 'package:ente_strings/ente_strings.dart';
+import 'package:ente_ui/utils/toast_util.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:hugeicons/hugeicons.dart';
+import 'package:locker/services/scanner/auto_capture_controller.dart';
+import 'package:locker/services/scanner/scan_geometry.dart';
+import 'package:locker/services/scanner/scan_session_controller.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+import 'package:locker/ui/pages/scanner/scan_quad_overlay.dart';
+import 'package:locker/ui/pages/scanner/scanner_review_page.dart';
+import 'package:logging/logging.dart';
+
+enum _CameraStatus { starting, ready, permissionDenied, error }
+
+/// Camera screen of the document scanner. Pops with `true` when the scan was
+/// saved, otherwise the caller treats the flow as cancelled.
+class ScannerCapturePage extends StatefulWidget {
+ const ScannerCapturePage({super.key, required this.onUploadFiles});
+
+ final Future<bool> Function(List<File> files) onUploadFiles;
+
+ @override
+ State<ScannerCapturePage> createState() => _ScannerCapturePageState();
+}
+
+class _ScannerCapturePageState extends State<ScannerCapturePage>
+ with WidgetsBindingObserver {
+ final _logger = Logger('ScannerCapturePage');
+ final _session = ScanSessionController();
+ final _stabilizer = QuadStabilizer();
+ final _autoCapture = AutoCaptureController();
+
+ CameraController? _camera;
+ _CameraStatus _status = _CameraStatus.starting;
+ ScanQuad? _stableQuad;
+ bool _analysisInFlight = false;
+ bool _takingPicture = false;
+ bool _torchOn = false;
+ bool _autoMode = false;
+ bool _scannerInitFailed = false;
+
+ @override
+ void initState() {
+ super.initState();
+ WidgetsBinding.instance.addObserver(this);
+ _session.addListener(_onSessionChanged);
+ unawaited(_initScanner());
+ unawaited(_startCamera());
+ }
+
+ Future<void> _initScanner() async {
+ if (_scannerInitFailed) setState(() => _scannerInitFailed = false);
+ try {
+ await _session.init();
+ if (mounted) setState(() {});
+ } catch (_) {
+ if (mounted) setState(() => _scannerInitFailed = true);
+ }
+ }
+
+ @override
+ void dispose() {
+ WidgetsBinding.instance.removeObserver(this);
+ _session.removeListener(_onSessionChanged);
+ unawaited(_camera?.dispose());
+ unawaited(_session.disposeSession());
+ super.dispose();
+ }
+
+ @override
+ void didChangeAppLifecycleState(AppLifecycleState state) {
+ final camera = _camera;
+ if (state == AppLifecycleState.inactive) {
+ if (camera != null) {
+ _camera = null;
+ unawaited(camera.dispose());
+ }
+ _autoCapture.reset();
+ } else if (state == AppLifecycleState.resumed) {
+ if (_camera == null && _status != _CameraStatus.starting) {
+ unawaited(_startCamera());
+ }
+ }
+ }
+
+ void _onSessionChanged() {
+ if (!mounted) return;
+ final error = _session.takeLastError();
+ if (error != null) {
+ showShortToast(context, context.strings.somethingWentWrong);
+ }
+ }
+
+ Future<void> _startCamera() async {
+ setState(() {
+ _status = _CameraStatus.starting;
+ _stableQuad = null;
+ });
+ _stabilizer.reset();
+ _autoCapture.reset();
+ try {
+ final cameras = await availableCameras();
+ if (cameras.isEmpty) {
+ setState(() => _status = _CameraStatus.error);
+ return;
+ }
+ final back = cameras.firstWhere(
+ (c) => c.lensDirection == CameraLensDirection.back,
+ orElse: () => cameras.first,
+ );
+ final controller = CameraController(
+ back,
+ // Lower presets cap still-capture resolution at preview-like sizes;
+ // `max` is required for full-resolution captures.
+ ResolutionPreset.max,
+ enableAudio: false,
+ imageFormatGroup: Platform.isIOS
+ ? ImageFormatGroup.bgra8888
+ : ImageFormatGroup.yuv420,
+ );
+ await controller.initialize();
+ if (!mounted) {
+ await controller.dispose();
+ return;
+ }
+ await controller.startImageStream(_onFrame);
+ _torchOn = false;
+ setState(() {
+ _camera = controller;
+ _status = _CameraStatus.ready;
+ });
+ } on CameraException catch (e) {
+ _logger.warning('Camera unavailable: ${e.code}', e);
+ if (!mounted) return;
+ setState(() {
+ _status = e.code.startsWith('CameraAccessDenied')
+ ? _CameraStatus.permissionDenied
+ : _CameraStatus.error;
+ });
+ } catch (e, s) {
+ _logger.severe('Failed to start camera', e, s);
+ if (mounted) setState(() => _status = _CameraStatus.error);
+ }
+ }
+
+ Future<void> _pauseCamera() async {
+ final camera = _camera;
+ if (camera == null) return;
+ setState(() {
+ _camera = null;
+ _stableQuad = null;
+ });
+ _stabilizer.reset();
+ _autoCapture.reset();
+ await camera.dispose();
+ }
+
+ // One analysis frame in flight at a time; newer frames are dropped until
+ // the current one finishes.
+ void _onFrame(CameraImage image) {
+ if (_analysisInFlight || _takingPicture || !_session.isServiceReady) {
+ return;
+ }
+ _analysisInFlight = true;
+ unawaited(_analyze(image));
+ }
+
+ Future<void> _analyze(CameraImage image) async {
+ try {
+ final rotation = _camera?.description.sensorOrientation ?? 0;
+ ScanQuad? raw;
+ if (image.planes.length >= 3) {
+ raw = await _session.detectLiveYuv(
+ y: image.planes[0].bytes,
+ u: image.planes[1].bytes,
+ v: image.planes[2].bytes,
+ yRowStride: image.planes[0].bytesPerRow,
+ uvRowStride: image.planes[1].bytesPerRow,
+ uvPixelStride: image.planes[1].bytesPerPixel ?? 1,
+ width: image.width,
+ height: image.height,
+ rotationDegrees: rotation,
+ );
+ } else if (image.planes.isNotEmpty) {
+ raw = await _session.detectLiveBgra(
+ image.planes[0].bytes,
+ image.planes[0].bytesPerRow,
+ image.width,
+ image.height,
+ rotation,
+ );
+ }
+ final stable = _stabilizer.update(raw);
+ var fire = false;
+ if (_autoMode) {
+ fire = _autoCapture.onFrame(
+ stable,
+ captureBusy: _takingPicture || _session.isProcessing,
+ );
+ }
+ if (mounted) {
+ setState(() => _stableQuad = stable);
+ if (fire) {
+ unawaited(HapticFeedback.lightImpact());
+ unawaited(_capture());
+ }
+ }
+ } catch (_) {
+ // Dropped frames are fine; the next one will do.
+ } finally {
+ _analysisInFlight = false;
+ }
+ }
+
+ Future<void> _capture() async {
+ final camera = _camera;
+ if (camera == null || _takingPicture) return;
+ _autoCapture.notifyCaptureStarted();
+ setState(() {
+ _takingPicture = true;
+ _stableQuad = null;
+ });
+ _stabilizer.reset();
+ try {
+ final shot = await camera.takePicture();
+ final bytes = await shot.readAsBytes();
+ unawaited(_deleteQuietly(File(shot.path)));
+ _session.addCapture(bytes);
+ } catch (e, s) {
+ _logger.severe('Capture failed', e, s);
+ if (mounted) {
+ showShortToast(context, context.strings.somethingWentWrong);
+ }
+ } finally {
+ if (mounted) setState(() => _takingPicture = false);
+ }
+ }
+
+ static Future<void> _deleteQuietly(File file) async {
+ try {
+ if (await file.exists()) await file.delete();
+ } catch (_) {}
+ }
+
+ void _toggleAutoMode() {
+ setState(() => _autoMode = !_autoMode);
+ _autoCapture.reset();
+ }
+
+ Future<void> _toggleTorch() async {
+ final camera = _camera;
+ if (camera == null) return;
+ final next = !_torchOn;
+ try {
+ await camera.setFlashMode(next ? FlashMode.torch : FlashMode.off);
+ setState(() => _torchOn = next);
+ } on CameraException catch (e) {
+ _logger.warning('Torch unavailable: ${e.code}');
+ }
+ }
+
+ Future<void> _openReview() async {
+ if (_session.pageCount == 0 && !_session.isProcessing) return;
+ final navigator = Navigator.of(context);
+ await _pauseCamera();
+ final saved = await navigator.push<bool>(
+ MaterialPageRoute(
+ builder: (_) => ScannerReviewPage(
+ session: _session,
+ onUploadFiles: widget.onUploadFiles,
+ ),
+ ),
+ );
+ if (!mounted) return;
+ if (saved == true) {
+ navigator.pop(true);
+ } else {
+ unawaited(_startCamera());
+ }
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ return Scaffold(
+ backgroundColor: Colors.black,
+ body: Stack(
+ fit: StackFit.expand,
+ children: [
+ _buildPreview(colors),
+ SafeArea(
+ child: Padding(
+ padding: const EdgeInsets.all(Spacing.lg),
+ child: Column(
+ children: [
+ Row(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: [
+ _ChromeButton(
+ icon: HugeIcons.strokeRoundedCancel01,
+ onTap: () => Navigator.of(context).pop(false),
+ tooltip: context.strings.close,
+ ),
+ _ChromeButton(
+ icon: _torchOn
+ ? HugeIcons.strokeRoundedFlash
+ : HugeIcons.strokeRoundedFlashOff,
+ onTap: _camera == null ? null : _toggleTorch,
+ tooltip: _torchOn
+ ? context.strings.scannerTorchOff
+ : context.strings.scannerTorchOn,
+ ),
+ ],
+ ),
+ const Spacer(),
+ SizedBox(
+ height: 88,
+ child: Stack(
+ alignment: Alignment.center,
+ children: [
+ _ShutterButton(
+ enabled:
+ _status == _CameraStatus.ready &&
+ !_takingPicture &&
+ _session.isServiceReady,
+ armingProgress: _autoMode ? _autoCapture.progress : 0,
+ armingColor: colors.primary,
+ onTap: _capture,
+ ),
+ Align(
+ alignment: Alignment.centerLeft,
+ child: _AutoToggle(
+ active: _autoMode,
+ onTap: _toggleAutoMode,
+ ),
+ ),
+ Align(
+ alignment: Alignment.centerRight,
+ child: ListenableBuilder(
+ listenable: _session,
+ builder: (context, _) => _DoneButton(
+ session: _session,
+ accent: colors.primary,
+ onTap: _openReview,
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+
+ Widget _buildPreview(ColorTokens colors) {
+ if (_scannerInitFailed) {
+ return _CameraMessage(
+ message: context.strings.somethingWentWrong,
+ onRetry: _initScanner,
+ );
+ }
+ switch (_status) {
+ case _CameraStatus.starting:
+ return Center(
+ child: CircularProgressIndicator(color: colors.specialWhite),
+ );
+ case _CameraStatus.permissionDenied:
+ return _CameraMessage(
+ message: context.strings.cameraPermissionSettings,
+ onRetry: _startCamera,
+ );
+ case _CameraStatus.error:
+ return _CameraMessage(
+ message: context.strings.somethingWentWrong,
+ onRetry: _startCamera,
+ );
+ case _CameraStatus.ready:
+ final camera = _camera;
+ if (camera == null || !camera.value.isInitialized) {
+ return Center(
+ child: CircularProgressIndicator(color: colors.specialWhite),
+ );
+ }
+ return Center(
+ child: AspectRatio(
+ aspectRatio: 1 / camera.value.aspectRatio,
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ CameraPreview(camera),
+ ScanQuadOverlay(
+ quad: _takingPicture ? null : _stableQuad,
+ color: colors.primary,
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+ }
+}
+
+class _CameraMessage extends StatelessWidget {
+ const _CameraMessage({required this.message, required this.onRetry});
+
+ final String message;
+ final Future<void> Function() onRetry;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ return Center(
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: Spacing.xxl),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Text(
+ message,
+ textAlign: TextAlign.center,
+ style: TextStyles.body.copyWith(color: colors.specialWhite),
+ ),
+ const SizedBox(height: Spacing.xl),
+ ButtonComponent(
+ label: context.strings.retry,
+ size: ButtonComponentSize.small,
+ onTap: onRetry,
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class _ChromeButton extends StatelessWidget {
+ const _ChromeButton({required this.icon, required this.onTap, this.tooltip});
+
+ final List<List<dynamic>> icon;
+ final VoidCallback? onTap;
+ final String? tooltip;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final button = GestureDetector(
+ onTap: onTap,
+ child: Container(
+ width: 44,
+ height: 44,
+ decoration: BoxDecoration(
+ color: Colors.black.withValues(alpha: 0.4),
+ shape: BoxShape.circle,
+ ),
+ child: Center(
+ child: HugeIcon(
+ icon: icon,
+ color: onTap == null
+ ? colors.specialWhite.withValues(alpha: 0.4)
+ : colors.specialWhite,
+ size: 22,
+ ),
+ ),
+ ),
+ );
+ if (tooltip == null) return button;
+ return Tooltip(message: tooltip!, child: button);
+ }
+}
+
+class _ShutterButton extends StatelessWidget {
+ const _ShutterButton({
+ required this.enabled,
+ required this.armingProgress,
+ required this.armingColor,
+ required this.onTap,
+ });
+
+ final bool enabled;
+
+ /// Auto-capture arming progress in [0, 1]; drawn as an arc over the ring.
+ final double armingProgress;
+ final Color armingColor;
+ final Future<void> Function() onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final white = colors.specialWhite;
+ return GestureDetector(
+ onTap: enabled ? () => onTap() : null,
+ child: AnimatedOpacity(
+ duration: const Duration(milliseconds: 150),
+ opacity: enabled ? 1 : 0.5,
+ child: SizedBox(
+ width: 76,
+ height: 76,
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ Container(
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ border: Border.all(color: white, width: 4),
+ ),
+ child: Center(
+ child: Container(
+ width: 60,
+ height: 60,
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ color: white,
+ ),
+ ),
+ ),
+ ),
+ TweenAnimationBuilder<double>(
+ tween: Tween(begin: 0, end: armingProgress.clamp(0.0, 1.0)),
+ duration: Motion.quick,
+ builder: (context, value, _) => CustomPaint(
+ painter: _ArmingArcPainter(
+ progress: value,
+ color: armingColor,
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+class _ArmingArcPainter extends CustomPainter {
+ const _ArmingArcPainter({required this.progress, required this.color});
+
+ final double progress;
+ final Color color;
+
+ @override
+ void paint(Canvas canvas, Size size) {
+ if (progress <= 0 || size.isEmpty) return;
+ canvas.drawArc(
+ // Centered on the button's 4px ring, which is inset in the same box.
+ Rect.fromCircle(
+ center: size.center(Offset.zero),
+ radius: size.shortestSide / 2 - 2,
+ ),
+ -math.pi / 2,
+ progress * 2 * math.pi,
+ false,
+ Paint()
+ ..color = color
+ ..style = PaintingStyle.stroke
+ ..strokeWidth = 4
+ ..strokeCap = StrokeCap.round,
+ );
+ }
+
+ @override
+ bool shouldRepaint(_ArmingArcPainter oldDelegate) =>
+ oldDelegate.progress != progress || oldDelegate.color != color;
+}
+
+/// Auto/manual capture-mode toggle: pill with the auto-camera glyph and the
+/// current mode's label, filled with the accent color while auto is on.
+class _AutoToggle extends StatelessWidget {
+ const _AutoToggle({required this.active, required this.onTap});
+
+ final bool active;
+ final VoidCallback onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ return Tooltip(
+ message: active
+ ? context.strings.scannerAutoCaptureOff
+ : context.strings.scannerAutoCaptureOn,
+ child: GestureDetector(
+ onTap: onTap,
+ child: AnimatedContainer(
+ duration: Motion.standard,
+ height: 44,
+ padding: const EdgeInsets.symmetric(horizontal: Spacing.md),
+ decoration: BoxDecoration(
+ color: active
+ ? colors.primary
+ : Colors.black.withValues(alpha: 0.4),
+ borderRadius: BorderRadius.circular(22),
+ ),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ HugeIcon(
+ icon: HugeIcons.strokeRoundedCameraAutomatically01,
+ color: colors.specialWhite,
+ size: 20,
+ ),
+ const SizedBox(width: Spacing.xs),
+ Text(
+ active
+ ? context.strings.scannerCaptureModeAuto
+ : context.strings.scannerCaptureModeManual,
+ style: TextStyles.mini.copyWith(color: colors.specialWhite),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
+
+/// Prominent "done capturing" button: filled accent circle with a checkmark
+/// and a page-count badge. Animates in when the first page arrives.
+class _DoneButton extends StatelessWidget {
+ const _DoneButton({
+ required this.session,
+ required this.accent,
+ required this.onTap,
+ });
+
+ final ScanSessionController session;
+ final Color accent;
+ final VoidCallback onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final visible = session.pageCount > 0 || session.isProcessing;
+ return IgnorePointer(
+ ignoring: !visible,
+ child: AnimatedScale(
+ scale: visible ? 1 : 0,
+ duration: Motion.standard,
+ curve: visible ? Curves.easeOutBack : Curves.easeIn,
+ child: Tooltip(
+ message: context.strings.done,
+ child: GestureDetector(
+ onTap: onTap,
+ child: SizedBox(
+ width: 62,
+ height: 62,
+ child: Stack(
+ clipBehavior: Clip.none,
+ children: [
+ Positioned.fill(
+ child: Container(
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ color: accent,
+ boxShadow: [
+ BoxShadow(
+ color: Colors.black.withValues(alpha: 0.35),
+ blurRadius: 10,
+ ),
+ ],
+ ),
+ child: Center(
+ child: session.isProcessing
+ ? SizedBox(
+ width: 24,
+ height: 24,
+ child: CircularProgressIndicator(
+ strokeWidth: 2.5,
+ color: colors.specialWhite,
+ ),
+ )
+ : HugeIcon(
+ icon: HugeIcons.strokeRoundedTick02,
+ color: colors.specialWhite,
+ size: 30,
+ ),
+ ),
+ ),
+ ),
+ if (session.pageCount > 0)
+ Positioned(
+ top: -4,
+ right: -4,
+ child: Container(
+ padding: const EdgeInsets.all(Spacing.xs),
+ constraints: const BoxConstraints(minWidth: 22),
+ decoration: BoxDecoration(
+ shape: BoxShape.circle,
+ color: colors.specialWhite,
+ ),
+ child: Center(
+ child: Text(
+ '${session.pageCount}',
+ style: TextStyles.mini.copyWith(color: accent),
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart
new file mode 100644
index 0000000000..4b96dbbff7
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_crop_page.dart
@@ -0,0 +1,256 @@
+import 'dart:typed_data';
+
+import 'package:ente_components/ente_components.dart';
+import 'package:ente_strings/ente_strings.dart';
+import 'package:flutter/material.dart';
+import 'package:hugeicons/hugeicons.dart';
+import 'package:locker/services/scanner/pdf_writer.dart';
+import 'package:locker/services/scanner/scan_geometry.dart';
+import 'package:locker/services/scanner/scan_session_controller.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+
+/// Manual corner adjustment on the source capture. Saving re-renders the
+/// page; cancelling discards the edit.
+class ScannerCropPage extends StatefulWidget {
+ const ScannerCropPage({
+ super.key,
+ required this.session,
+ required this.pageId,
+ });
+
+ final ScanSessionController session;
+ final String pageId;
+
+ @override
+ State<ScannerCropPage> createState() => _ScannerCropPageState();
+}
+
+class _ScannerCropPageState extends State<ScannerCropPage> {
+ static const double _handleRadius = 22;
+
+ Uint8List? _sourceBytes;
+ double _aspect = 3 / 4;
+ List<Offset> _corners = const [];
+ int _dragging = -1;
+
+ @override
+ void initState() {
+ super.initState();
+ WidgetsBinding.instance.addPostFrameCallback((_) => _load());
+ }
+
+ Future<void> _load() async {
+ final page = _page;
+ if (page == null) {
+ if (mounted) Navigator.of(context).pop();
+ return;
+ }
+ final bytes = await page.sourceJpeg.readAsBytes();
+ if (!mounted) return;
+ double aspect;
+ try {
+ final info = JpegInfo.parse(bytes);
+ aspect = info.width / info.height;
+ } on FormatException {
+ aspect = page.width / page.height;
+ }
+ setState(() {
+ _sourceBytes = bytes;
+ _aspect = aspect;
+ _corners = [
+ for (final c in page.quad.corners)
+ Offset(c.dx / ScanQuad.maskSide, c.dy / ScanQuad.maskSide),
+ ];
+ });
+ }
+
+ ScannedPage? get _page {
+ for (final page in widget.session.pages) {
+ if (page.id == widget.pageId) return page;
+ }
+ return null;
+ }
+
+ Future<void> _save() async {
+ final navigator = Navigator.of(context);
+ final quad = orderClockwise([
+ for (final c in _corners)
+ Offset(c.dx * ScanQuad.maskSide, c.dy * ScanQuad.maskSide),
+ ]);
+ await widget.session.updatePage(widget.pageId, quad: quad);
+ if (mounted) navigator.pop();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final source = _sourceBytes;
+ return Scaffold(
+ backgroundColor: Colors.black,
+ body: SafeArea(
+ child: Column(
+ children: [
+ Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.sm,
+ ),
+ child: Row(
+ children: [
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedCancel01,
+ color: colors.specialWhite,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ onTap: () => Navigator.of(context).pop(),
+ tooltip: context.strings.cancel,
+ ),
+ Expanded(
+ child: Center(
+ child: Text(
+ context.strings.adjustCrop,
+ style: TextStyles.body.copyWith(
+ color: colors.specialWhite,
+ ),
+ ),
+ ),
+ ),
+ const SizedBox.square(dimension: 44),
+ ],
+ ),
+ ),
+ Expanded(
+ child: source == null
+ ? Center(
+ child: CircularProgressIndicator(
+ color: colors.specialWhite,
+ ),
+ )
+ : Padding(
+ padding: const EdgeInsets.all(Spacing.lg),
+ child: LayoutBuilder(
+ builder: (context, constraints) {
+ final container = Size(
+ constraints.maxWidth,
+ constraints.maxHeight,
+ );
+ final rect = fittedRect(container, _aspect);
+
+ Offset toScreen(Offset normalized) => Offset(
+ rect.left + normalized.dx * rect.width,
+ rect.top + normalized.dy * rect.height,
+ );
+
+ Offset toNormalized(Offset screen) => Offset(
+ ((screen.dx - rect.left) / rect.width).clamp(
+ 0.0,
+ 1.0,
+ ),
+ ((screen.dy - rect.top) / rect.height).clamp(
+ 0.0,
+ 1.0,
+ ),
+ );
+
+ return GestureDetector(
+ onPanStart: (details) {
+ var best = -1;
+ var bestDistance = double.infinity;
+ for (var i = 0; i < _corners.length; i++) {
+ final distance =
+ (toScreen(_corners[i]) -
+ details.localPosition)
+ .distance;
+ if (distance < bestDistance) {
+ bestDistance = distance;
+ best = i;
+ }
+ }
+ _dragging = bestDistance <= _handleRadius * 2
+ ? best
+ : -1;
+ },
+ onPanUpdate: (details) {
+ if (_dragging < 0) return;
+ setState(() {
+ _corners = [..._corners]
+ ..[_dragging] = toNormalized(
+ details.localPosition,
+ );
+ });
+ },
+ onPanEnd: (_) => _dragging = -1,
+ child: Stack(
+ fit: StackFit.expand,
+ children: [
+ Positioned.fromRect(
+ rect: rect,
+ child: Image.memory(source, fit: BoxFit.fill),
+ ),
+ CustomPaint(
+ painter: _CropPainter(
+ corners: [
+ for (final c in _corners) toScreen(c),
+ ],
+ color: colors.primary,
+ ),
+ ),
+ ],
+ ),
+ );
+ },
+ ),
+ ),
+ ),
+ Padding(
+ padding: const EdgeInsets.all(Spacing.xl),
+ child: ButtonComponent(
+ label: context.strings.save,
+ onTap: source == null ? null : _save,
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class _CropPainter extends CustomPainter {
+ const _CropPainter({required this.corners, required this.color});
+
+ final List<Offset> corners;
+ final Color color;
+
+ @override
+ void paint(Canvas canvas, Size size) {
+ if (corners.length != 4) return;
+ canvas.drawPath(
+ Path()..addPolygon(corners, true),
+ Paint()
+ ..color = color
+ ..style = PaintingStyle.stroke
+ ..strokeWidth = 3,
+ );
+ for (final corner in corners) {
+ canvas.drawCircle(
+ corner,
+ 12,
+ Paint()..color = color.withValues(alpha: 0.45),
+ );
+ canvas.drawCircle(
+ corner,
+ 12,
+ Paint()
+ ..color = color
+ ..style = PaintingStyle.stroke
+ ..strokeWidth = 3,
+ );
+ }
+ }
+
+ @override
+ bool shouldRepaint(_CropPainter oldDelegate) =>
+ oldDelegate.corners != corners || oldDelegate.color != color;
+}
diff --git a/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
new file mode 100644
index 0000000000..95b6f2a8a7
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/pages/scanner/scanner_review_page.dart
@@ -0,0 +1,412 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:ente_components/ente_components.dart';
+import 'package:ente_strings/ente_strings.dart';
+import 'package:ente_ui/utils/toast_util.dart';
+import 'package:flutter/material.dart';
+import 'package:hugeicons/hugeicons.dart';
+import 'package:intl/intl.dart';
+import 'package:locker/services/scanner/scan_session_controller.dart';
+import 'package:locker/services/scanner/scanner_models.dart';
+import 'package:locker/ui/components/text_input_sheet.dart';
+import 'package:locker/ui/pages/scanner/scanner_crop_page.dart';
+import 'package:share_plus/share_plus.dart';
+
+/// Review of the scanned pages. Pops with `true` after a successful save so
+/// the capture page can close the flow; popping otherwise returns to the
+/// camera for more pages.
+class ScannerReviewPage extends StatefulWidget {
+ const ScannerReviewPage({
+ super.key,
+ required this.session,
+ required this.onUploadFiles,
+ });
+
+ final ScanSessionController session;
+ final Future<bool> Function(List<File> files) onUploadFiles;
+
+ @override
+ State<ScannerReviewPage> createState() => _ScannerReviewPageState();
+}
+
+class _ScannerReviewPageState extends State<ScannerReviewPage> {
+ static const _thumbnailWidth = 40.0;
+ static const _thumbnailHeight = 56.0;
+
+ late final PageController _pageController;
+ int _index = 0;
+
+ @override
+ void initState() {
+ super.initState();
+ widget.session.addListener(_onSessionChanged);
+ _index = widget.session.pageCount - 1;
+ if (_index < 0) _index = 0;
+ _pageController = PageController(initialPage: _index);
+ }
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ final prefix = context.strings.scanFilenamePrefix;
+ final timestamp = DateFormat('d MMM yyyy, HH.mm').format(DateTime.now());
+ widget.session.ensureFileName('$prefix $timestamp.pdf');
+ }
+
+ @override
+ void dispose() {
+ widget.session.removeListener(_onSessionChanged);
+ _pageController.dispose();
+ super.dispose();
+ }
+
+ void _onSessionChanged() {
+ if (!mounted) return;
+ if (widget.session.pageCount == 0 && !widget.session.isProcessing) {
+ Navigator.of(context).pop(false);
+ return;
+ }
+ setState(() {
+ if (_index >= widget.session.pageCount && widget.session.pageCount > 0) {
+ _index = widget.session.pageCount - 1;
+ }
+ });
+ }
+
+ ScannedPage? get _currentPage {
+ final pages = widget.session.pages;
+ if (pages.isEmpty) return null;
+ return pages[_index.clamp(0, pages.length - 1)];
+ }
+
+ Future<File> _buildPdf() async {
+ await widget.session.waitForPending();
+ return widget.session.buildPdf();
+ }
+
+ Future<void> _saveToEnte() async {
+ final pdf = await _buildPdf();
+ if (!mounted) return;
+ final didUpload = await widget.onUploadFiles([pdf]);
+ if (!mounted) return;
+ if (didUpload) {
+ showShortToast(context, context.strings.scanSaved);
+ Navigator.of(context).pop(true);
+ }
+ }
+
+ Future<void> _share() async {
+ final pdf = await _buildPdf();
+ await SharePlus.instance.share(ShareParams(files: [XFile(pdf.path)]));
+ }
+
+ Future<void> _rename() async {
+ final l10n = context.strings;
+ final current = widget.session.fileName ?? '';
+ final base = current.toLowerCase().endsWith('.pdf')
+ ? current.substring(0, current.length - 4)
+ : current;
+ await showTextInputSheet(
+ context,
+ title: l10n.renameFile,
+ initialValue: base,
+ hintText: l10n.enterFileName,
+ submitButtonLabel: l10n.save,
+ onSubmit: (String name) async => widget.session.renameFile(name),
+ );
+ }
+
+ Future<void> _adjustCrop() async {
+ final page = _currentPage;
+ if (page == null) return;
+ await Navigator.of(context).push(
+ MaterialPageRoute(
+ builder: (_) =>
+ ScannerCropPage(session: widget.session, pageId: page.id),
+ ),
+ );
+ }
+
+ Future<void> _rotate() async {
+ final page = _currentPage;
+ if (page == null) return;
+ await widget.session.rotatePageClockwise(page.id);
+ }
+
+ Future<void> _delete() async {
+ final page = _currentPage;
+ if (page == null) return;
+ await widget.session.deletePage(page.id);
+ }
+
+ void _jumpToPage(int index) {
+ _pageController.animateToPage(
+ index,
+ duration: Motion.standard,
+ curve: Curves.easeInOut,
+ );
+ }
+
+ /// Reorders and keeps the currently viewed page in view at its new
+ /// position.
+ void _onReorder(int oldIndex, int newIndex) {
+ final pages = widget.session.pages;
+ if (pages.isEmpty) return;
+ final viewedId = pages[_index.clamp(0, pages.length - 1)].id;
+ widget.session.reorderPage(oldIndex, newIndex);
+ final target = widget.session.pages.indexWhere(
+ (page) => page.id == viewedId,
+ );
+ if (target >= 0 && target != _index) {
+ setState(() => _index = target);
+ _pageController.jumpToPage(target);
+ }
+ }
+
+ static String _elideMiddle(String text, int maxLength) {
+ if (text.length <= maxLength) return text;
+ final half = (maxLength - 1) ~/ 2;
+ return '${text.substring(0, half)}…${text.substring(text.length - half)}';
+ }
+
+ /// Filename styled as an editable field so renaming is discoverable.
+ Widget _buildFileNameField(ColorTokens colors) {
+ final fileName = widget.session.fileName ?? '';
+ return Tooltip(
+ message: context.strings.renameFile,
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: _rename,
+ child: Container(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.md,
+ vertical: Spacing.sm,
+ ),
+ decoration: BoxDecoration(
+ color: colors.fillLight,
+ borderRadius: BorderRadius.circular(Radii.lg),
+ border: Border.all(color: colors.strokeFaint),
+ ),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Flexible(
+ child: Text(
+ _elideMiddle(fileName, 26),
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyles.bodyBold.copyWith(color: colors.textBase),
+ ),
+ ),
+ const SizedBox(width: Spacing.sm),
+ HugeIcon(
+ icon: HugeIcons.strokeRoundedPencilEdit02,
+ color: colors.primary,
+ size: 16,
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildThumbnailStrip(
+ List<ScannedPage> pages,
+ int current,
+ ColorTokens colors,
+ ) {
+ return SizedBox(
+ height: _thumbnailHeight + 2 * Spacing.xs,
+ child: ReorderableListView.builder(
+ scrollDirection: Axis.horizontal,
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.xs,
+ ),
+ itemCount: pages.length,
+ onReorder: _onReorder,
+ itemBuilder: (context, index) {
+ final page = pages[index];
+ final isCurrent = index == current;
+ return Padding(
+ key: ValueKey(page.id),
+ padding: const EdgeInsets.only(right: Spacing.sm),
+ child: GestureDetector(
+ onTap: () => _jumpToPage(index),
+ child: Container(
+ width: _thumbnailWidth,
+ height: _thumbnailHeight,
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(Radii.xs),
+ border: Border.all(
+ color: isCurrent ? colors.primary : colors.strokeDark,
+ width: isCurrent ? 2 : 1,
+ ),
+ ),
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(Radii.xs - 1),
+ child: Image.file(
+ page.processedJpeg,
+ key: ValueKey(page.processedJpeg.path),
+ fit: BoxFit.cover,
+ cacheHeight: 168,
+ ),
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ );
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+ final l10n = context.strings;
+ return Scaffold(
+ backgroundColor: colors.backgroundBase,
+ body: SafeArea(
+ child: ListenableBuilder(
+ listenable: widget.session,
+ builder: (context, _) {
+ final pages = widget.session.pages;
+ final pageCount = pages.length;
+ final current = pageCount == 0 ? 0 : _index.clamp(0, pageCount - 1);
+ return Column(
+ children: [
+ Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.sm,
+ ),
+ child: Row(
+ children: [
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedArrowLeft01,
+ color: colors.textBase,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ onTap: () => Navigator.of(context).pop(false),
+ tooltip: l10n.addPage,
+ ),
+ Expanded(
+ child: Center(child: _buildFileNameField(colors)),
+ ),
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedCameraAdd01,
+ color: colors.textBase,
+ ),
+ variant: IconButtonComponentVariant.unfilled,
+ onTap: () => Navigator.of(context).pop(false),
+ tooltip: l10n.addPage,
+ ),
+ ],
+ ),
+ ),
+ Expanded(
+ child: pageCount == 0
+ ? const Center(child: CircularProgressIndicator())
+ : PageView.builder(
+ controller: _pageController,
+ itemCount: pageCount,
+ onPageChanged: (index) =>
+ setState(() => _index = index),
+ itemBuilder: (context, index) {
+ final page = pages[index];
+ return Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: Spacing.lg,
+ vertical: Spacing.sm,
+ ),
+ child: Center(
+ child: ClipRRect(
+ borderRadius: BorderRadius.circular(Radii.sm),
+ child: Image.file(
+ page.processedJpeg,
+ key: ValueKey(page.processedJpeg.path),
+ fit: BoxFit.contain,
+ ),
+ ),
+ ),
+ );
+ },
+ ),
+ ),
+ if (pageCount > 0)
+ Text(
+ l10n.scanPageOfTotal(
+ current: current + 1,
+ total: pageCount,
+ ),
+ style: TextStyles.mini.copyWith(color: colors.textLighter),
+ ),
+ const SizedBox(height: Spacing.sm),
+ if (pageCount > 1) ...[
+ _buildThumbnailStrip(pages, current, colors),
+ const SizedBox(height: Spacing.sm),
+ ],
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedCrop,
+ color: colors.textBase,
+ ),
+ onTap: pageCount == 0 ? null : _adjustCrop,
+ tooltip: l10n.adjustCrop,
+ ),
+ const SizedBox(width: Spacing.lg),
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedRotateClockwise,
+ color: colors.textBase,
+ ),
+ onTap: pageCount == 0 ? null : _rotate,
+ tooltip: l10n.rotate,
+ ),
+ const SizedBox(width: Spacing.lg),
+ IconButtonComponent(
+ icon: HugeIcon(
+ icon: HugeIcons.strokeRoundedDelete02,
+ color: colors.warning,
+ ),
+ onTap: pageCount == 0 ? null : _delete,
+ tooltip: l10n.delete,
+ ),
+ ],
+ ),
+ Padding(
+ padding: const EdgeInsets.all(Spacing.xl),
+ child: Column(
+ children: [
+ ButtonComponent(
+ label: l10n.saveToEnte,
+ onTap: pageCount == 0 && !widget.session.isProcessing
+ ? null
+ : _saveToEnte,
+ ),
+ const SizedBox(height: Spacing.md),
+ ButtonComponent(
+ label: l10n.share,
+ variant: ButtonComponentVariant.secondary,
+ onTap: pageCount == 0 && !widget.session.isProcessing
+ ? null
+ : _share,
+ ),
+ ],
+ ),
+ ),
+ ],
+ );
+ },
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/locker/pubspec.yaml b/mobile/apps/locker/pubspec.yaml
index 97adb1d136..2fbf8d2454 100644
--- a/mobile/apps/locker/pubspec.yaml
+++ b/mobile/apps/locker/pubspec.yaml
@@ -11,6 +11,8 @@ dependencies:
adaptive_theme: 3.7.2
app_links: 6.4.1
bip39: 1.0.6
+ camera: 0.11.2+1
+ camera_android_camerax: 0.6.30
collection: 1.19.1
convert: 3.1.2
crypto: 3.0.7
diff --git a/mobile/packages/strings/lib/l10n/arb/strings_en.arb b/mobile/packages/strings/lib/l10n/arb/strings_en.arb
index e29b1e048a..045fe39d04 100644
--- a/mobile/packages/strings/lib/l10n/arb/strings_en.arb
+++ b/mobile/packages/strings/lib/l10n/arb/strings_en.arb
@@ -150,6 +150,10 @@
"storageAmount": {}
}
},
+ "addPage": "Add page",
+ "@addPage": {
+ "description": "Button to capture another page in the document scanner"
+ },
"addParticipants": "Add participants",
"addPeopleWidgetPrompt": "Add a people widget to your home screen to customize it here.",
"addPerson": "Add person",
@@ -180,6 +184,7 @@
},
"addYourPhotosNow": "Add your photos now",
"adjust": "Adjust",
+ "adjustCrop": "Adjust crop",
"admin": "Admin",
"admins": "Admins",
"adminsAndCollaboratorsCanAddPhotosDescription": "Admins and collaborators can add photos. Admins can also manage photos & participants.",
@@ -3389,8 +3394,8 @@
"saveCollage": "Save collage",
"saveContact": "Save contact",
"saveCopy": "Save copy",
- "saveDocumentDescription": "Upload important docs.",
- "saveDocumentTitle": "Document",
+ "saveFileDescription": "Upload important files.",
+ "saveFileTitle": "File",
"saveKey": "Save key",
"@saveKey": {
"description": "Save key button label"
@@ -3404,6 +3409,7 @@
"description": "Description text asking user if they want to save to storage or share with other apps"
},
"savePerson": "Save person",
+ "saveToEnte": "Save to Ente",
"saveToLocker": "Save to Locker",
"saveYourRecoveryKeyIfYouHaventAlready": "Save your recovery key if you haven't already",
"saving": "Saving...",
@@ -3413,8 +3419,40 @@
"scanACode": "Scan a code",
"scanAQrCode": "Scan a QR code",
"scanCode": "Scan code",
+ "scanDocumentDescription": "Scan important docs.",
+ "scanDocumentTitle": "Document",
+ "scanFilenamePrefix": "Scan",
+ "@scanFilenamePrefix": {
+ "description": "Prefix of the default filename for a scanned document PDF, e.g. 'Scan 13 Aug 2026, 14.30.pdf'"
+ },
+ "scannerAutoCaptureOff": "Turn off automatic capture",
+ "scannerAutoCaptureOn": "Turn on automatic capture",
+ "scannerCaptureModeAuto": "Auto",
+ "@scannerCaptureModeAuto": {
+ "description": "Label on the scanner capture-mode toggle while automatic capture is on"
+ },
+ "scannerCaptureModeManual": "Manual",
+ "@scannerCaptureModeManual": {
+ "description": "Label on the scanner capture-mode toggle while capture is manual (automatic capture off)"
+ },
+ "scannerNotSupportedOnDevice": "Document scanning is not supported on this device",
"scannerTorchOff": "Turn off torch",
"scannerTorchOn": "Turn on torch",
+ "scanPageOfTotal": "Page {current} of {total}",
+ "@scanPageOfTotal": {
+ "description": "Page position indicator in the document scanner review screen",
+ "placeholders": {
+ "current": {
+ "type": "int",
+ "example": "1"
+ },
+ "total": {
+ "type": "int",
+ "example": "3"
+ }
+ }
+ },
+ "scanSaved": "Scan saved",
"scanThisBarcodeWithnyourAuthenticatorApp": "Scan this barcode with\nyour authenticator app",
"scheduleShare": "Schedule share",
"search": "Search",