ANDREAS_LATANOWSKY_CPD_1910877/lib/image_translator.dart

225 lines
8.2 KiB
Dart
Raw Normal View History

2024-01-02 13:53:21 +01:00
import 'dart:io';
2024-01-10 23:39:33 +01:00
import 'package:cpd_app/file_utils.dart';
2024-01-02 13:53:21 +01:00
import 'package:cpd_app/http_utils.dart';
2024-01-10 23:39:33 +01:00
import 'package:cpd_app/session_list.dart';
2024-01-02 13:53:21 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'language_utils.dart';
2024-01-10 23:39:33 +01:00
import 'package:fluttertoast/fluttertoast.dart';
2024-01-02 13:53:21 +01:00
import 'package:image_picker/image_picker.dart';
class ImageTranslation extends StatefulWidget {
const ImageTranslation({Key? key}) : super(key: key);
@override
State<ImageTranslation> createState() => _ImageTranslationState();
}
class _ImageTranslationState extends State<ImageTranslation> {
XFile? _selectedImage;
2024-01-10 23:39:33 +01:00
bool _wasTranslated = false;
String _sourceLanguage = 'English';
String _targetLanguage = 'German';
Uint8List translatedIamge = Uint8List(0);
2024-01-02 13:53:21 +01:00
final HttpUtils _httpUtils = HttpUtils();
2024-01-10 23:39:33 +01:00
Uint8List buildNewImage(
Uint8List imageBytes, String text, String sourceLang, String targetLang) {
return imageBytes;
}
2024-01-02 13:53:21 +01:00
Future<void> uploadImage(
Uint8List imageBytes,
String sourceLang,
String targetLang,
) async {
2024-01-10 23:39:33 +01:00
Uint8List newImageBytes = Uint8List(0);
_httpUtils.showProgressBar(context);
try {
newImageBytes = await _httpUtils.translateKnownSource(
imageBytes, sourceLang, targetLang);
} finally {
// ignore: use_build_context_synchronously
_httpUtils.hideProgressBar(context);
2024-01-02 13:53:21 +01:00
}
2024-01-10 23:39:33 +01:00
if (newImageBytes.isEmpty) {
Fluttertoast.showToast(
msg: "Image could not be translated",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0,
2024-01-02 13:53:21 +01:00
);
2024-01-10 23:39:33 +01:00
} else {
DateTime now = DateTime.now();
int milliseconds = now.millisecondsSinceEpoch;
ImageList.addImage(milliseconds.toString(), newImageBytes);
_wasTranslated = true;
setState(() {
translatedIamge = newImageBytes;
});
2024-01-02 13:53:21 +01:00
}
}
List<String> sourceLanguages = LanguageUtils.tessLanguages.keys.toList();
List<String> targetLanguages =
LanguageUtils.translatorLanguages.keys.toList();
@override
Widget build(BuildContext context) {
return Scaffold(
2024-01-10 23:39:33 +01:00
backgroundColor: const Color.fromARGB(99, 78, 72, 72),
2024-01-02 13:53:21 +01:00
appBar: AppBar(
2024-01-10 23:39:33 +01:00
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
2024-01-02 13:53:21 +01:00
title: const Text('Image Translation'),
2024-01-10 23:39:33 +01:00
actions: [
IconButton(
icon: const Icon(Icons.wifi),
onPressed: () async {
if (!await _httpUtils.internetConnectivityCheck()) {
_httpUtils.triggerNoInetToast();
} else {
_httpUtils.triggerConnectedToast();
}
},
),
],
2024-01-02 13:53:21 +01:00
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.blue.shade100,
borderRadius: BorderRadius.circular(8.0),
),
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text('From', style: TextStyle(color: Colors.blue)),
DropdownButton<String>(
value: _sourceLanguage,
onChanged: (newValue) {
setState(() {
_sourceLanguage = newValue!;
});
},
items: sourceLanguages
2024-01-10 23:39:33 +01:00
.where((value) => value != 'Auto')
2024-01-02 13:53:21 +01:00
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
Container(
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(8.0),
),
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text('Target', style: TextStyle(color: Colors.red)),
DropdownButton<String>(
value: _targetLanguage,
onChanged: (newValue) {
setState(() {
_targetLanguage = newValue!;
});
},
items: targetLanguages
2024-01-10 23:39:33 +01:00
.where((value) => value != 'Auto')
2024-01-02 13:53:21 +01:00
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
],
),
const SizedBox(height: 20),
TextButton(
onPressed: () async {
2024-01-10 23:39:33 +01:00
if (!_wasTranslated) {
final ImagePicker picker = ImagePicker();
final XFile? image =
await picker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
_selectedImage = image;
});
}
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
const ImageTranslation(),
),
);
2024-01-02 13:53:21 +01:00
}
},
2024-01-10 23:39:33 +01:00
style: ElevatedButton.styleFrom(
foregroundColor: const Color.fromARGB(255, 0, 0, 0),
backgroundColor: const Color.fromARGB(255, 161, 120, 17)),
child: Text(_wasTranslated ? 'Try Another' : 'Select Image'),
2024-01-02 13:53:21 +01:00
),
ElevatedButton(
onPressed: _selectedImage != null
? () async {
if (_selectedImage != null) {
File imageFile = File(_selectedImage!.path);
List<int> imageBytes = imageFile.readAsBytesSync();
await uploadImage(Uint8List.fromList(imageBytes),
_sourceLanguage, _targetLanguage);
}
}
: null,
2024-01-10 23:39:33 +01:00
style: ElevatedButton.styleFrom(
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
foregroundColor: const Color.fromARGB(255, 0, 0, 0)),
child: const Text('Translate Image'),
2024-01-02 13:53:21 +01:00
),
2024-01-10 23:39:33 +01:00
if (translatedIamge.isEmpty && _selectedImage != null)
Image.file(File(_selectedImage!.path)),
if (translatedIamge.isNotEmpty) Image.memory(translatedIamge),
if (translatedIamge.isNotEmpty)
Visibility(
visible: translatedIamge.isNotEmpty,
child: ElevatedButton(
onPressed: () {
if (translatedIamge.isNotEmpty) {
FileUtils.saveTranslatedImage(translatedIamge);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
foregroundColor: const Color.fromARGB(255, 0, 0, 0)),
child: const Text('Save Translated Image'),
),
),
2024-01-02 13:53:21 +01:00
],
),
),
);
}
}