-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
457 lines (420 loc) · 14.5 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import 'package:bubble_bottom_bar/bubble_bottom_bar.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:clipboard/clipboard.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:translator/translator.dart';
import 'package:text_recognition/ControlsWidget.dart';
import 'package:text_recognition/TextAreaWidget.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: MyStatefulWidget(),
debugShowCheckedModeBanner: false,
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int currentIndex;
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
File pickedImage;
String identifiedText = '';
String out = 'Choose a language';
String language = '';
final lang = TextEditingController();
final lang2 = TextEditingController();
TextEditingController textEditingController = TextEditingController();
final FlutterTts flutterTts = FlutterTts();
String dyslexiaText = '';
GoogleTranslator translator = new GoogleTranslator();
///This allows the user to choose an image file
Future pickImage() async {
final file = await ImagePicker().getImage(source: ImageSource.gallery);
setImage(File(file.path));
}
///This scans the text in the picture
Future scanText() async {
showDialog(
context: context,
child: Center(
child: CircularProgressIndicator(),
),
);
final text = await recognizeText(pickedImage);
setText(text);
Navigator.of(context).pop();
}
///This method defines what the copy to clipboard button does
void copyToClipboard() {
if (identifiedText.trim() != '') {
FlutterClipboard.copy(identifiedText);
}
}
void setImage(File newImage) {
setState(() {
pickedImage = newImage;
});
}
void setText(String newText) {
setState(() {
identifiedText = newText;
});
}
///This Method uses the Firebase Vision API and text recognizer to find the words in the image
static Future<String> recognizeText(File imageFile) async {
if (imageFile == null) {
return 'No selected image';
} else {
final visionImage = FirebaseVisionImage.fromFile(imageFile);
final textRecognizer = FirebaseVision.instance.textRecognizer();
try {
final visionText = await textRecognizer.processImage(visionImage);
await textRecognizer.close();
final text = extractText(visionText);
return text.isEmpty ? 'No text found in the image' : text;
} catch (error) {
return error.toString();
}
}
}
///This method formats what is seen on the uploaded file
static extractText(VisionText visionText) {
String text = '';
for (TextBlock block in visionText.blocks) {
for (TextLine line in block.lines) {
for (TextElement word in line.elements) {
text = text + word.text + ' ';
}
text = text + '\n';
}
}
return text;
}
@override
void initState() {
super.initState();
currentIndex = 0;
}
changePage(int index) {
setState(() {
currentIndex = index;
});
}
void transSpanish()
{
translator.translate(lang.text, to: 'es')
.then((output)
{
setState(() {
out = output.text;
});
print(out);
});
}
void transFrench()
{
translator.translate(lang.text, to: 'fr')
.then((output)
{
setState(() {
out = output.text;
});
print(out);
});
}
void transHindi()
{
translator.translate(lang.text, to: 'hi')
.then((output)
{
setState(() {
out = output.text;
});
print(out);
});
}
///Text to Speech Settings
speak(String text) async {
await flutterTts.setLanguage('en-US');
await flutterTts.setPitch(1.0);
await flutterTts.setSpeechRate(1.0);
await flutterTts.setVolume(100000.0);
await flutterTts.speak(text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: (currentIndex == 0)
? AppBar(
title: Text('Home'),
backgroundColor: Colors.pinkAccent[100],
)
: (currentIndex == 1)
? AppBar(
title: Text('Translate'),
backgroundColor: Colors.pinkAccent[100],
)
: (currentIndex == 2)
? AppBar(
title: Text('Text to Speech'),
backgroundColor: Colors.pinkAccent[100],
)
: (currentIndex == 3)
? AppBar(
title: Text('Change the Font'),
backgroundColor: Colors.pinkAccent[100],
): Container ,
endDrawer: new Drawer(
child: Column(
children: [
Expanded(child: Center(child: buildImage())),
const SizedBox(height: 16),
Expanded(child:
ControlsWidget(
onClickedPickImage: pickImage,
onClickedScanText: scanText,
),
),
const SizedBox(height: 16),
Expanded(child:
TextAreaWidget(
text: identifiedText,
onClickedCopy: copyToClipboard,
),
),
],
),
),
bottomNavigationBar: BubbleBottomBar(
opacity: 0.2,
backgroundColor: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
currentIndex: currentIndex,
hasInk: true,
inkColor: Colors.black12,
hasNotch: true,
fabLocation: BubbleBottomBarFabLocation.end,
onTap: changePage,
items: <BubbleBottomBarItem>[
BubbleBottomBarItem(
backgroundColor: Colors.pinkAccent[100],
icon: Icon(Icons.home_outlined, color: Colors.black),
activeIcon: Icon(Icons.home_outlined, color: Colors.pinkAccent[100]),
title: Text('Home'),
),
BubbleBottomBarItem(
backgroundColor: Colors.pinkAccent[100],
icon: Icon(Icons.g_translate_rounded, color: Colors.black),
activeIcon: Icon(Icons.g_translate_rounded, color: Colors.pinkAccent[100]),
title: Text('Translate'),
),
BubbleBottomBarItem(
backgroundColor: Colors.pinkAccent[100],
icon: Icon(Icons.speaker_outlined, color: Colors.black),
activeIcon: Icon(Icons.speaker_outlined, color: Colors.pinkAccent[100]),
title: Text('Speech'),
),
BubbleBottomBarItem(
backgroundColor: Colors.pinkAccent[100],
icon: Icon(Icons.subject_rounded, color: Colors.black),
activeIcon: Icon(Icons.subject_rounded, color: Colors.pinkAccent[100]),
title: Text('Font'),
),
],
),
body: (currentIndex == 0)
? SingleChildScrollView(child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0.0, 30.0, 0.0, 0.0),
child: Text(
'TextSlate',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 35.0,
),
),
),
Center(child: Image(image: AssetImage('images/book.png')),),
Container(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0),
child: Text(
'An app made to make education more accessible.',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 15.0,
),
),
),
Container(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child: Text(
'Click on one of the tabs at the bottom or click on the camera button to start.',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 27.0,
),
),
),
],
))
:(currentIndex == 1)
? SingleChildScrollView(child: Container(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: lang,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Text'
),
),
),
RaisedButton(
color: Colors.pinkAccent[75],
child: Text("Translate"), //on press to translate the language using function
onPressed: ()
{
if(language == 'Spanish'){
transSpanish();
} else if(language == 'French'){
transFrench();
} else if(language == 'Hindi'){
transHindi();
}
},
),
Padding(padding: const EdgeInsets.all(8.0),
child: Text(out.toString(),
style: TextStyle(fontSize: 20.0,
),
),
),
Stack(
children: <Widget>[
Align(
alignment: Alignment.bottomCenter,
child: FloatingActionButton.extended(
label: Text('Français'),
onPressed: () => language = 'French',
backgroundColor: Colors.black26,
),
),
Align(
alignment: Alignment.bottomLeft,
child: FloatingActionButton.extended(
onPressed: () => language = 'Spanish',
label: Text('Español'),
backgroundColor: Colors.black26,
),
),
Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton.extended(
onPressed: () => language = 'Hindi',
label: Text('Hindi'),
backgroundColor: Colors.black26,
),
),
],
)//translated string
],
)
),
))
:(currentIndex == 2)
? SingleChildScrollView(child: Container(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: textEditingController,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Text'
),
),
),
RaisedButton(
color: Colors.pinkAccent[75],
child: Text('Read Aloud'),
onPressed: () => speak(textEditingController.text),
),
RaisedButton(
color: Colors.pinkAccent[75],
child: Text('Stop'),
onPressed: () => flutterTts.stop(),
)
],
),
)
))
:(currentIndex == 3)
? SingleChildScrollView(child: Container(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: lang2,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter Text'
),
),
),
RaisedButton(
color: Colors.pinkAccent[75],
child: Text("Change Font"),
onPressed: (){
setState(() {
dyslexiaText = lang2.text;
});
},
),
Text(dyslexiaText,
style: TextStyle(fontFamily: 'OpenDyslexic',
fontSize: 20.0,
),
)
],
),
))
): Container ,
floatingActionButton: new FloatingActionButton(
onPressed: () {
_scaffoldKey.currentState.openEndDrawer();
},
child: new Icon(Icons.camera_alt_outlined),
backgroundColor: Colors.pinkAccent[100],
),
floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
);
}
Widget buildImage() => Container(
child: pickedImage != null
? Image.file(pickedImage)
: Icon(Icons.photo, size: 80, color: Colors.black),
);
}