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 createState() => _ImageTranslationState(); } class _ImageTranslationState extends State { 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 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 sourceLanguages = LanguageUtils.tessLanguages.keys.toList(); List 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: [ const SizedBox(height: 20), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Container( decoration: BoxDecoration( color: Colors.blue.shade100, borderRadius: BorderRadius.circular(8.0), ), padding: const EdgeInsets.all(8.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('From', style: TextStyle(color: Colors.blue)), DropdownButton( value: _sourceLanguage, onChanged: (newValue) { setState(() { _sourceLanguage = newValue!; }); }, items: sourceLanguages .where((value) => value != 'Auto') .map>((String value) { return DropdownMenuItem( 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: [ const Text('Target', style: TextStyle(color: Colors.red)), DropdownButton( value: _targetLanguage, onChanged: (newValue) { setState(() { _targetLanguage = newValue!; }); }, items: targetLanguages .where((value) => value != 'Auto') .map>((String value) { return DropdownMenuItem( 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 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'), ), ), ], ), ), ); } }