88 lines
2.5 KiB
Dart
88 lines
2.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:ui';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
class CarRegistrationRecognizerController extends GetxController {
|
|
@override
|
|
void onInit() {
|
|
// scanText();
|
|
super.onInit();
|
|
}
|
|
|
|
// The ImagePicker instance
|
|
final ImagePicker _imagePicker = ImagePicker();
|
|
Map extractedData = {};
|
|
|
|
// The GoogleMlKit TextRecognizer instance
|
|
final TextRecognizer _textRecognizer = TextRecognizer();
|
|
|
|
// The scanned text
|
|
String? scannedText;
|
|
String? jsonOutput;
|
|
final List<Map<String, dynamic>> lines = [];
|
|
XFile? image;
|
|
Map decode = {};
|
|
// Picks an image from the camera or gallery and extracts the text
|
|
final List<Map<String, dynamic>> extractedTextWithCoordinates = [];
|
|
Future<void> scanText() async {
|
|
// Pick an image from the camera or gallery
|
|
image = await _imagePicker.pickImage(source: ImageSource.gallery);
|
|
update();
|
|
// 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;
|
|
final Map<String, dynamic> extractedData = {};
|
|
|
|
for (TextBlock block in recognizedText.blocks) {
|
|
for (TextLine line in block.lines) {
|
|
final String lineText = line.text;
|
|
final Rect lineBoundingBox = line.boundingBox!;
|
|
|
|
extractedTextWithCoordinates.add({
|
|
'text': lineText,
|
|
'boundingBox': {
|
|
'left': lineBoundingBox.left,
|
|
'top': lineBoundingBox.top,
|
|
'width': lineBoundingBox.width,
|
|
'height': lineBoundingBox.height,
|
|
},
|
|
});
|
|
}
|
|
update();
|
|
}
|
|
|
|
print(jsonEncode(extractedTextWithCoordinates));
|
|
|
|
// Extract the scanned text line by line
|
|
final List<Map<String, dynamic>> lines = [];
|
|
for (var i = 0; i < recognizedText.blocks.length; i++) {
|
|
lines.add({
|
|
i.toString(): recognizedText.blocks[i].text,
|
|
});
|
|
}
|
|
// print(jsonEncode(lines));
|
|
|
|
// Convert the list of lines to a JSON string
|
|
jsonOutput = jsonEncode(extractedData);
|
|
decode = jsonDecode(jsonOutput!);
|
|
|
|
update();
|
|
print('jsonOutput------------------------------');
|
|
print(decode);
|
|
// print(jsonEncode(lines));
|
|
}
|
|
}
|