10/23/2
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
class OCRController extends GetxController {
|
||||
@@ -12,11 +16,28 @@ class OCRController extends GetxController {
|
||||
|
||||
class TextExtractionController extends GetxController {
|
||||
String extractedText = '';
|
||||
bool isloading = false;
|
||||
File? _scannedImage;
|
||||
// Convert the extracted text to JSON
|
||||
String getTextAsJSON(String text) {
|
||||
final json = {
|
||||
'text': text,
|
||||
};
|
||||
|
||||
return jsonEncode(json);
|
||||
}
|
||||
|
||||
// Convert the extracted text to blocks by line
|
||||
List<String> getTextBlocks(String text) {
|
||||
return text.split('\n');
|
||||
}
|
||||
|
||||
Future<void> pickAndExtractText() async {
|
||||
final pickedImage =
|
||||
await ImagePicker().pickImage(source: ImageSource.camera);
|
||||
if (pickedImage != null) {
|
||||
isloading = true;
|
||||
update();
|
||||
final imagePath = pickedImage.path;
|
||||
final languages = [
|
||||
'eng',
|
||||
@@ -32,7 +53,17 @@ class TextExtractionController extends GetxController {
|
||||
"preserve_interword_spaces": "1",
|
||||
} // Additional options if needed
|
||||
);
|
||||
extractedText = text;
|
||||
isloading = false;
|
||||
final jsonText = getTextAsJSON(text);
|
||||
final textBlocks = getTextBlocks(text);
|
||||
update();
|
||||
extractedText =
|
||||
textBlocks.toString(); // Convert the extracted text to JSON.
|
||||
|
||||
// Print the JSON to the console.
|
||||
print(textBlocks);
|
||||
update();
|
||||
// print(text);
|
||||
} catch (e) {
|
||||
print('Error during text extraction: $e');
|
||||
extractedText = '';
|
||||
@@ -41,40 +72,82 @@ class TextExtractionController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
// class TesseractController extends GetxController {
|
||||
// String text = '';
|
||||
// bool scanning = false;
|
||||
|
||||
// Future<void> extractTextFromImage() async {
|
||||
// scanning = true;
|
||||
// update();
|
||||
|
||||
// // Get the image from the image picker
|
||||
// final ImagePicker picker = ImagePicker();
|
||||
// final XFile? image = await picker.pickImage(source: ImageSource.gallery);
|
||||
|
||||
// // If an image was selected, extract the text from it
|
||||
// if (image != null) {
|
||||
// var watch = Stopwatch()..start();
|
||||
// text = await TesseractLatest.extractText(image.path);
|
||||
// scanning = false;
|
||||
// update();
|
||||
// print('Scanning took ${watch.elapsedMilliseconds} ms');
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// import 'package:get/get.dart';
|
||||
// import 'package:flutter/material.dart';
|
||||
// import 'package:image_picker/image_picker.dart';
|
||||
// import 'package:google_ml_kit/google_ml_kit.dart';
|
||||
//
|
||||
// class TextRecognizerController extends GetxController {
|
||||
// // The ImagePicker instance
|
||||
// final ImagePicker _imagePicker = ImagePicker();
|
||||
//
|
||||
// // The GoogleMlKit TextRecognizer instance
|
||||
// final TextRecognizer _textRecognizer = TextRecognizer();
|
||||
//
|
||||
// // The scanned text
|
||||
// String? scannedText;
|
||||
//
|
||||
// // Picks an image from the camera or gallery and extracts the text
|
||||
// Future<void> scanText() async {
|
||||
// // Pick an image from the camera or gallery
|
||||
// final XFile? image =
|
||||
// await _imagePicker.pickImage(source: ImageSource.gallery);
|
||||
//
|
||||
// // If no image was picked, return
|
||||
// if (image == null) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // Recognize the text in the image
|
||||
// final RecognizedText recognizedText =
|
||||
// await _textRecognizer.processImage(image as InputImage);
|
||||
//
|
||||
// // Extract the scanned text
|
||||
// scannedText = recognizedText.text;
|
||||
//
|
||||
// // Update the UI
|
||||
// update();
|
||||
// }
|
||||
// }
|
||||
class TextRecognizerController extends GetxController {
|
||||
@override
|
||||
void onInit() {
|
||||
scanText();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
// The ImagePicker instance
|
||||
final ImagePicker _imagePicker = ImagePicker();
|
||||
|
||||
// The GoogleMlKit TextRecognizer instance
|
||||
final TextRecognizer _textRecognizer = TextRecognizer();
|
||||
|
||||
// The scanned text
|
||||
String? scannedText;
|
||||
|
||||
// Picks an image from the camera or gallery and extracts the text
|
||||
|
||||
Future<void> scanText() async {
|
||||
// Pick an image from the camera or gallery
|
||||
final XFile? image =
|
||||
await _imagePicker.pickImage(source: ImageSource.gallery);
|
||||
|
||||
// If no image was picked, return
|
||||
if (image == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert the XFile object to an InputImage object
|
||||
final InputImage inputImage = InputImage.fromFile(File(image.path));
|
||||
|
||||
// Recognize the text in the image
|
||||
final RecognizedText recognizedText =
|
||||
await _textRecognizer.processImage(inputImage);
|
||||
scannedText = recognizedText.text;
|
||||
// Extract the scanned text line by line
|
||||
final List<Map<String, dynamic>> lines = [];
|
||||
for (var i = 0; i < recognizedText.blocks.length; i++) {
|
||||
lines.add({
|
||||
'line_number': i,
|
||||
'text': recognizedText.blocks[i].text,
|
||||
});
|
||||
}
|
||||
|
||||
// Convert the list of lines to a JSON string
|
||||
final String jsonOutput = jsonEncode(lines);
|
||||
update();
|
||||
// Print the JSON output
|
||||
print(jsonOutput);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user