Update: 2026-05-12 01:29:57
This commit is contained in:
@@ -13,51 +13,11 @@ class ImageProcessingService {
|
||||
/// 3. Increases contrast
|
||||
/// 4. Saves as high-quality JPEG
|
||||
static Future<File?> processInvoiceImage(File originalImage) async {
|
||||
try {
|
||||
AppLogger.print('Started processing image: ${originalImage.path}');
|
||||
|
||||
// Step 1: Initial compression using flutter_image_compress (Native, fast)
|
||||
final dir = await getTemporaryDirectory();
|
||||
final compressedPath = path.join(
|
||||
dir.path, 'compressed_${DateTime.now().millisecondsSinceEpoch}.jpg');
|
||||
|
||||
final XFile? compressedXFile =
|
||||
await FlutterImageCompress.compressAndGetFile(
|
||||
originalImage.path,
|
||||
compressedPath,
|
||||
quality: 85,
|
||||
minWidth: 1920,
|
||||
minHeight: 1080,
|
||||
);
|
||||
|
||||
if (compressedXFile == null) {
|
||||
AppLogger.error('Failed to compress image initially', null);
|
||||
return originalImage; // Fallback to original
|
||||
}
|
||||
|
||||
File compressedFile = File(compressedXFile.path);
|
||||
|
||||
// Step 2: Fine-tuning filters (Grayscale/Contrast) via 'image' package
|
||||
// This helps OCR models detect text edges more accurately
|
||||
final bytes = await compressedFile.readAsBytes();
|
||||
final filteredBytes = await compute(_applyFilters, bytes);
|
||||
|
||||
if (filteredBytes != null) {
|
||||
final filteredPath = path.join(
|
||||
dir.path, 'filtered_${DateTime.now().millisecondsSinceEpoch}.jpg');
|
||||
final filteredFile = File(filteredPath);
|
||||
await filteredFile.writeAsBytes(filteredBytes);
|
||||
|
||||
AppLogger.print('Finished processing image with filters: ${filteredFile.path}');
|
||||
return filteredFile;
|
||||
}
|
||||
|
||||
AppLogger.print('Finished processing image (no filters): ${compressedFile.path}');
|
||||
return compressedFile;
|
||||
} catch (e, stack) {
|
||||
AppLogger.error('Error processing image', e, stack);
|
||||
return originalImage; // Fallback
|
||||
}
|
||||
// The web app uploads the original file and OCR works 100%.
|
||||
// Processing/compressing it here strips EXIF/quality and causes OCR to fail.
|
||||
// So we just return the original image intact.
|
||||
AppLogger.print('Skipping image processing to preserve quality for AI: ${originalImage.path}');
|
||||
return originalImage;
|
||||
}
|
||||
|
||||
/// Runs in an isolate to prevent UI freezing
|
||||
|
||||
@@ -17,8 +17,14 @@ class InvoiceDetailController extends GetxController {
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
if (Get.arguments != null) {
|
||||
invoiceId = Get.arguments['id'];
|
||||
final args = Get.arguments;
|
||||
if (args != null) {
|
||||
if (args is Map) {
|
||||
invoiceId = args['id']?.toString();
|
||||
} else if (args is String) {
|
||||
invoiceId = args;
|
||||
}
|
||||
|
||||
if (invoiceId != null) {
|
||||
fetchInvoiceDetails();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user