ANDREAS_LATANOWSKY_CPD_1910877/lib/image_translator.dart

225 lines
8.2 KiB
Dart

import 'dart:io';
import 'package:cpd_app/file_utils.dart';
import 'package:cpd_app/http_utils.dart';
import 'package:cpd_app/session_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'language_utils.dart';
import 'package:fluttertoast/fluttertoast.dart';
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;
bool _wasTranslated = false;
String _sourceLanguage = 'English';
String _targetLanguage = 'German';
Uint8List translatedIamge = Uint8List(0);
final HttpUtils _httpUtils = HttpUtils();
Uint8List buildNewImage(
Uint8List imageBytes, String text, String sourceLang, String targetLang) {
return imageBytes;
}
Future<void> uploadImage(
Uint8List imageBytes,
String sourceLang,
String targetLang,
) async {
Uint8List newImageBytes = Uint8List(0);
_httpUtils.showProgressBar(context);
try {
newImageBytes = await _httpUtils.translateKnownSource(
imageBytes, sourceLang, targetLang);
} finally {
// ignore: use_build_context_synchronously
_httpUtils.hideProgressBar(context);
}
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,
);
} else {
DateTime now = DateTime.now();
int milliseconds = now.millisecondsSinceEpoch;
ImageList.addImage(milliseconds.toString(), newImageBytes);
_wasTranslated = true;
setState(() {
translatedIamge = newImageBytes;
});
}
}
List<String> sourceLanguages = LanguageUtils.tessLanguages.keys.toList();
List<String> targetLanguages =
LanguageUtils.translatorLanguages.keys.toList();
@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('Image Translation'),
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>[
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
.where((value) => value != 'Auto')
.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
.where((value) => value != 'Auto')
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
],
),
const SizedBox(height: 20),
TextButton(
onPressed: () async {
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(),
),
);
}
},
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'),
),
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,
style: ElevatedButton.styleFrom(
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
foregroundColor: const Color.fromARGB(255, 0, 0, 0)),
child: const Text('Translate Image'),
),
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'),
),
),
],
),
),
);
}
}