38 lines
1.0 KiB
Dart
38 lines
1.0 KiB
Dart
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();
|
|
}
|
|
}
|