Perform first OCR extraction

This commit is contained in:
2025-07-20 17:25:52 +02:00
parent fb7891d913
commit f61e3541fb
6 changed files with 113 additions and 41 deletions

View File

@@ -1,45 +1,23 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:moneymgr_mobile/services/storage/expenses.dart';
import 'package:moneymgr_mobile/utils/ocr_utils.dart';
import 'package:moneymgr_mobile/utils/pdf_utils.dart';
import 'package:moneymgr_mobile/widgets/expense_editor.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:scanbot_sdk/scanbot_sdk.dart';
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart' hide IconButton, EdgeInsets;
part 'scan_screen.g.dart';
/// Scan a document & return generated PDF as byte file
@riverpod
Future<Uint8List?> _scanDocument(Ref ref) async {
var configuration = DocumentScanningFlow(
appearance: DocumentFlowAppearanceConfiguration(
statusBarMode: StatusBarMode.DARK,
),
cleanScanningSession: true,
outputSettings: DocumentScannerOutputSettings(pagesScanLimit: 1),
screens: DocumentScannerScreens(
review: ReviewScreenConfiguration(enabled: false),
),
);
var documentResult = await ScanbotSdkUiV2.startDocumentScanner(configuration);
if (documentResult.status != OperationStatus.OK) {
throw Exception("Scanner failed with status ${documentResult.status}");
}
// Convert result to PDF
var result = await ScanbotSdk.document.createPDFForDocument(
PDFFromDocumentParams(
documentID: documentResult.data!.uuid,
pdfConfiguration: PdfConfiguration(),
),
);
final pdfPath = result.pdfFileUri.replaceFirst("file://", "");
return File(pdfPath).readAsBytes();
Future<(Uint8List?, double?)> _scanDocument(Ref ref) async {
final pdf = await scanDocAsPDF();
final img = await renderPdf(pdfBytes: pdf);
final amount = await extractTotalFromBill(img);
return (pdf, amount);
}
class ScanScreen extends HookConsumerWidget {
@@ -62,12 +40,17 @@ class ScanScreen extends HookConsumerWidget {
return Padding(
padding: const EdgeInsets.all(8.0),
child: switch (scanDocProvider) {
AsyncData(:final value) when value != null => ExpenseEditor(
file: value,
AsyncData(:final value) when value.$1 != null => ExpenseEditor(
file: value.$1!,
initialData: BaseExpenseInfo(
label: null,
cost: value.$2 ?? 0.0,
time: DateTime.now(),
),
onFinished: (expense) async {
await expenses.add(
info: expense,
fileContent: value,
fileContent: value.$1!,
fileMimeType: "application/pdf",
);
restartScan();
@@ -76,7 +59,7 @@ class ScanScreen extends HookConsumerWidget {
),
// No data
AsyncData(:final value) when value == null => ScanErrorScreen(
AsyncData(:final value) when value.$1 == null => ScanErrorScreen(
message: "No document scanned!",
onTryAgain: restartScan,
),