Perform first OCR extraction
This commit is contained in:
44
moneymgr_mobile/lib/utils/ocr_utils.dart
Normal file
44
moneymgr_mobile/lib/utils/ocr_utils.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
/// Attempt to extract total amount from invoice image
|
||||
Future<double?> extractTotalFromBill(Uint8List imgBuff) async {
|
||||
final decodedImage = await decodeImageFromList(imgBuff);
|
||||
|
||||
final byteData = await decodedImage.toByteData(
|
||||
format: ui.ImageByteFormat.rawRgba,
|
||||
);
|
||||
|
||||
final image = InputImage.fromBitmap(
|
||||
bitmap: byteData!.buffer.asUint8List(),
|
||||
width: decodedImage.width,
|
||||
height: decodedImage.height,
|
||||
);
|
||||
|
||||
final textRecognizer = TextRecognizer(script: TextRecognitionScript.latin);
|
||||
final extractionResult = await textRecognizer.processImage(image);
|
||||
|
||||
Logger.root.fine("Expense text: ${extractionResult.text}");
|
||||
|
||||
// Check for highest amount on invoice
|
||||
final regexp = RegExp(
|
||||
r'([0-9]+([ ]*(\\.|,)[ ]*[0-9]{1,2}){0,1})[ \\t\\n]*(EUR|eur|€)',
|
||||
multiLine: true,
|
||||
caseSensitive: false,
|
||||
);
|
||||
var highest = 0.0;
|
||||
for (final match in regexp.allMatches(extractionResult.text)) {
|
||||
if (match.groupCount == 0) continue;
|
||||
|
||||
// Process only numeric value
|
||||
final value = (match.group(1) ?? "").replaceAll(",", ".");
|
||||
highest = max(highest, double.tryParse(value) ?? 0.0);
|
||||
}
|
||||
|
||||
return highest == 0.0 ? null : highest;
|
||||
}
|
@@ -6,14 +6,40 @@ import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pdf_image_renderer/pdf_image_renderer.dart';
|
||||
import 'package:scanbot_sdk/scanbot_sdk.dart';
|
||||
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart' hide IconButton, EdgeInsets;
|
||||
|
||||
/// Scan document as PDF
|
||||
Future<Uint8List> scanDocAsPDF() 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();
|
||||
}
|
||||
|
||||
/// Render PDF to image bits
|
||||
Future<Uint8List> renderPdf(
|
||||
{
|
||||
String? path,
|
||||
Uint8List? pdfBytes,
|
||||
}) async {
|
||||
Future<Uint8List> renderPdf({String? path, Uint8List? pdfBytes}) async {
|
||||
assert(path != null || pdfBytes != null);
|
||||
|
||||
// Create temporary file if required
|
||||
@@ -62,4 +88,4 @@ Future<Uint8List> renderPdf(
|
||||
await File(path).delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user