improve parsing
This commit is contained in:
+35
-5
@@ -2,6 +2,7 @@
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -102,12 +103,22 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||
});
|
||||
|
||||
final recognizer = TextRecognizer(script: TextRecognitionScript.latin);
|
||||
RecognizedText? recognized;
|
||||
double imageWidth = 0;
|
||||
double imageHeight = 0;
|
||||
try {
|
||||
final bytes = await File(imagePath).readAsBytes();
|
||||
final codec = await ui.instantiateImageCodec(bytes);
|
||||
final frame = await codec.getNextFrame();
|
||||
imageWidth = frame.image.width.toDouble();
|
||||
imageHeight = frame.image.height.toDouble();
|
||||
frame.image.dispose();
|
||||
|
||||
final inputImage = InputImage.fromFilePath(imagePath);
|
||||
final recognized = await recognizer.processImage(inputImage);
|
||||
recognized = await recognizer.processImage(inputImage);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_extractedText = recognized.text.trim();
|
||||
_extractedText = recognized!.text.trim();
|
||||
if (_extractedText.isEmpty) {
|
||||
_error = 'No text found in this image.';
|
||||
}
|
||||
@@ -131,8 +142,14 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted && _extractedText.isNotEmpty) {
|
||||
await _openReview(_extractedText);
|
||||
if (mounted && _extractedText.isNotEmpty && recognized != null) {
|
||||
await _openReview(
|
||||
_extractedText,
|
||||
imagePath: imagePath,
|
||||
recognized: recognized,
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,11 +167,22 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||
String rawText, {
|
||||
List<LineItem>? items,
|
||||
String? imagePath,
|
||||
RecognizedText? recognized,
|
||||
double? imageWidth,
|
||||
double? imageHeight,
|
||||
}) async {
|
||||
final parsedItems = items;
|
||||
ReceiptParseResult? parsed;
|
||||
if (parsedItems == null) {
|
||||
parsed = ReceiptParser.parseReceipt(rawText);
|
||||
if (recognized != null && imageWidth != null && imageHeight != null) {
|
||||
parsed = ReceiptParser.parseRecognizedText(
|
||||
recognized,
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
);
|
||||
} else {
|
||||
parsed = ReceiptParser.parseReceipt(rawText);
|
||||
}
|
||||
}
|
||||
if (!mounted) return;
|
||||
final names =
|
||||
@@ -170,6 +198,8 @@ class _ScanScreenState extends State<ScanScreen> {
|
||||
supermarketNames: names.toList(),
|
||||
needsReview: parsed != null && !parsed.validationPassed,
|
||||
detectedStore: parsed?.store,
|
||||
ocrImageWidth: parsed?.ocrImageWidth,
|
||||
ocrImageHeight: parsed?.ocrImageHeight,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user