This commit is contained in:
Hamza-Ayed
2023-10-23 20:59:36 +03:00
parent e9738296df
commit c21558d125
6 changed files with 217 additions and 147 deletions

View File

@@ -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);
}
}

View File

@@ -13,6 +13,7 @@ import 'package:ride/views/widgets/elevated_btn.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import '../../../controller/functions/location_controller.dart';
import '../../../controller/functions/ocr_controller.dart';
import '../../../controller/home/captin/widget/connect.dart';
class HomeCaptain extends StatelessWidget {
@@ -191,6 +192,14 @@ class HomeCaptain extends StatelessWidget {
"Text FlutterTesseractsOcr",
),
),
TextButton(
onPressed: () {
Get.to(() => TextRecognizerWidget());
},
child: const Text(
"Text FlutterMLGoogle",
),
),
],
),
),

View File

@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_scalable_ocr/flutter_scalable_ocr.dart';
import 'package:get/get.dart';
import 'package:ride/views/widgets/mycircular.dart';
import '../../../controller/functions/ocr_controller.dart';
@@ -19,22 +19,23 @@ class TextScanner extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ScalableOCR(
paintboxCustom: Paint()
..style = PaintingStyle.stroke
..strokeWidth = 4.0
..color = const Color.fromARGB(153, 102, 160, 241),
boxLeftOff: 5,
boxBottomOff: 2.5,
boxRightOff: 5,
boxTopOff: 2.5,
boxHeight: MediaQuery.of(context).size.height / 3,
getRawData: (value) {
// Inspect the raw data here.
},
getScannedText: (value) {
controller.setText(value);
}),
// ScalableOCR(
// paintboxCustom: Paint()
// ..style = PaintingStyle.stroke
// ..strokeWidth = 4.0
// ..color = const Color.fromARGB(153, 102, 160, 241),
// boxLeftOff: 5,
// boxBottomOff: 2.5,
// boxRightOff: 5,
// boxTopOff: 2.5,
// boxHeight: MediaQuery.of(context).size.height / 3,
// getRawData: (value) {
// // Inspect the raw data here.
// },
// getScannedText: (value) {
// controller.setText(value);
// }),
Result(),
Obx(() {
return SnackBar(
@@ -80,7 +81,9 @@ class TextExtractionView extends StatelessWidget {
child: const Text('Pick Image and Extract Text'),
),
const SizedBox(height: 20),
Text(controller.extractedText),
controller.isloading
? const MyCircularProgressIndicator()
: Text(controller.extractedText),
],
),
);
@@ -88,6 +91,62 @@ class TextExtractionView extends StatelessWidget {
);
}
}
class TextRecognizerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
Get.put(TextRecognizerController());
return GetBuilder<TextRecognizerController>(
builder: (controller) => Scaffold(
appBar: AppBar(),
body: Center(
child: Text(controller.scannedText ?? ''),
),
));
}
}
// class TesseractWidget extends StatelessWidget {
// final TesseractController controller = Get.put(TesseractController());
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(
// title: Text('Tesseract Implementation'),
// ),
// body: GetBuilder<TesseractController>(
// builder: (controller) => Container(
// padding: const EdgeInsets.all(16),
// child: ListView(
// children: <Widget>[
// Column(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// children: [
// ElevatedButton(
// child: Text('Select image'),
// onPressed: controller.extractTextFromImage,
// ),
// controller.scanning
// ? const MyCircularProgressIndicator()
// : const Icon(Icons.done),
// SizedBox(height: 16),
// Center(child: SelectableText(controller.text)),
// ],
// ),
// ],
// ),
// ),
// ),
// );
// }
// }
// import 'package:flutter/material.dart';
// import 'package:get/get.dart';
// import 'package:image_picker/image_picker.dart';