208 lines
7.4 KiB
Dart
208 lines
7.4 KiB
Dart
import 'dart:io';
|
|
import 'package:cpd_app/http_utils.dart';
|
|
import 'package:cpd_app/image_uploader.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'language_utils.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
class OCRPage extends StatefulWidget {
|
|
const OCRPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<OCRPage> createState() => OCRPageState();
|
|
}
|
|
|
|
class OCRPageState extends State<OCRPage> {
|
|
List<String> tessLangCodes = LanguageUtils.tessLanguages.keys.toList();
|
|
String extractedText = '';
|
|
final ImageUploader _imageUploader = ImageUploader();
|
|
final HttpUtils _httpUtils = HttpUtils();
|
|
String _selectedLanguage = 'Auto';
|
|
XFile? selectedImage;
|
|
|
|
Future<void> getText(Uint8List imageBytes, String selectedLanguage) async {
|
|
setState(() {
|
|
extractedText = '';
|
|
});
|
|
|
|
if (!(await _httpUtils.internetConnectivityCheck())) {
|
|
_httpUtils.triggerNoInetToast();
|
|
return;
|
|
}
|
|
String text = '';
|
|
// ignore: use_build_context_synchronously
|
|
_httpUtils.showProgressBar(context);
|
|
try {
|
|
if (Platform.isWindows) {
|
|
if (selectedLanguage == 'Auto') {
|
|
String lang = await _httpUtils.checkLang(imageBytes, 'image.jpg');
|
|
String mappedLang = LanguageUtils.reMapping[lang]!;
|
|
text =
|
|
await _httpUtils.extractTextKnownSource(imageBytes, mappedLang);
|
|
} else {
|
|
String langCode =
|
|
LanguageUtils.translatorLanguages[selectedLanguage]!;
|
|
text = await _httpUtils.extractTextKnownSource(imageBytes, langCode);
|
|
}
|
|
} else {
|
|
if (selectedLanguage == 'Auto') {
|
|
String lang = await _httpUtils.checkLang(imageBytes, 'image.jpg');
|
|
text = await _imageUploader.performOcr(imageBytes, 'image.jpg', lang);
|
|
} else {
|
|
String langCode = LanguageUtils.tessLanguages[selectedLanguage]!;
|
|
text = await _imageUploader.performOcr(
|
|
imageBytes, 'image.jpg', langCode);
|
|
}
|
|
}
|
|
} finally {
|
|
// ignore: use_build_context_synchronously
|
|
_httpUtils.hideProgressBar(context);
|
|
}
|
|
if (text == "") {
|
|
Fluttertoast.showToast(
|
|
msg: "Text could not be extracted",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
timeInSecForIosWeb: 1,
|
|
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
|
|
textColor: Colors.white,
|
|
fontSize: 16.0,
|
|
);
|
|
} else {
|
|
setState(() {
|
|
extractedText = text;
|
|
selectedImage = null;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: const Color.fromARGB(99, 78, 72, 72),
|
|
appBar: AppBar(
|
|
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
|
|
title: const Text('OCR'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.wifi),
|
|
onPressed: () async {
|
|
if (!await _httpUtils.internetConnectivityCheck()) {
|
|
_httpUtils.triggerNoInetToast();
|
|
} else {
|
|
_httpUtils.triggerConnectedToast();
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
DropdownButton<String>(
|
|
value: _selectedLanguage,
|
|
style: const TextStyle(color: Colors.white),
|
|
dropdownColor: const Color(0xFF4E4848),
|
|
onChanged: (newValue) {
|
|
setState(() {
|
|
_selectedLanguage = newValue!;
|
|
});
|
|
},
|
|
items:
|
|
tessLangCodes.map<DropdownMenuItem<String>>((String value) {
|
|
return DropdownMenuItem<String>(
|
|
value: value,
|
|
child: Text(value),
|
|
);
|
|
}).toList(),
|
|
),
|
|
//Image preview hier
|
|
selectedImage != null
|
|
? Container(
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
|
height: MediaQuery.of(context).size.height * 0.4,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: const Color.fromARGB(255, 161, 120, 17),
|
|
width: 1.0,
|
|
),
|
|
),
|
|
child: Image.file(File(selectedImage!.path)),
|
|
)
|
|
: Container(),
|
|
ElevatedButton(
|
|
onPressed: () async {
|
|
final ImagePicker picker = ImagePicker();
|
|
final XFile? image =
|
|
await picker.pickImage(source: ImageSource.gallery);
|
|
if (image != null) {
|
|
setState(() {
|
|
selectedImage = image;
|
|
});
|
|
}
|
|
},
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
|
|
foregroundColor: const Color.fromARGB(255, 0, 0, 0)),
|
|
child: const Text('Select Image'),
|
|
),
|
|
ElevatedButton(
|
|
onPressed: selectedImage != null
|
|
? () async {
|
|
if (selectedImage != null && selectedImage!.path != "") {
|
|
File imageFile = File(selectedImage!.path);
|
|
List<int> imageBytes = imageFile.readAsBytesSync();
|
|
await getText(
|
|
Uint8List.fromList(imageBytes), _selectedLanguage);
|
|
} else if (selectedImage != null &&
|
|
selectedImage!.path == "") {
|
|
Uint8List? list = await selectedImage?.readAsBytes();
|
|
await getText(list!, _selectedLanguage);
|
|
}
|
|
}
|
|
: null,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
|
|
foregroundColor: const Color.fromARGB(255, 0, 0, 0)),
|
|
child: const Text('Extract Text'),
|
|
),
|
|
extractedText.isNotEmpty
|
|
? Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: TextFormField(
|
|
readOnly: true,
|
|
initialValue: extractedText,
|
|
maxLines: null,
|
|
style: const TextStyle(color: Colors.white),
|
|
decoration: const InputDecoration(
|
|
labelText: 'Extracted Text',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
: Container(),
|
|
|
|
extractedText.isNotEmpty
|
|
? TextButton(
|
|
onPressed: () {
|
|
Clipboard.setData(ClipboardData(text: extractedText));
|
|
},
|
|
style: TextButton.styleFrom(
|
|
backgroundColor: const Color.fromARGB(0, 33, 149, 243),
|
|
foregroundColor: const Color.fromARGB(255, 161, 120, 17),
|
|
),
|
|
child: const Text('Copy to Clipboard'),
|
|
)
|
|
: const SizedBox(),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|