ANDREAS_LATANOWSKY_CPD_1910877/lib/image_uploader.dart

49 lines
1.4 KiB
Dart

import 'package:flutter/services.dart';
import 'dart:io';
import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart';
import 'package:path_provider/path_provider.dart';
class ImageUploader {
ImageUploader();
Future<Uint8List> buildImageFile(String img) async {
String imageName = img;
ByteData bytes = await rootBundle.load('assets/$imageName');
return bytes.buffer.asUint8List();
}
Future<String> performOcr(
Uint8List imageBytes, String imageName, String lang) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File tempImageFile = File('$tempPath/$imageName');
await tempImageFile.writeAsBytes(imageBytes);
String text = await FlutterTesseractOcr.extractText(tempImageFile.path,
language: lang,
args: {
"psm": "4",
"preserve_interword_spaces": "1",
});
return text;
}
Future<String> performAdvancedOCR(
Uint8List imageBytes, String imageName, String lang) async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File tempImageFile = File('$tempPath/$imageName');
await tempImageFile.writeAsBytes(imageBytes);
String text = await FlutterTesseractOcr.extractHocr(tempImageFile.path,
language: lang,
args: {
"psm": "4",
"preserve_interword_spaces": "1",
});
return text;
}
}