Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update example app #481

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class _DetectorViewState extends State<DetectorView> {
)
: GalleryView(
title: widget.title,
text: widget.text,
onImage: widget.onImage,
onDetectorViewModeChanged: _onDetectorViewModeChanged);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ class DigitalInkView extends StatefulWidget {
class _DigitalInkViewState extends State<DigitalInkView> {
final DigitalInkRecognizerModelManager _modelManager =
DigitalInkRecognizerModelManager();
final String _language = 'en-US';
late final DigitalInkRecognizer _digitalInkRecognizer =
DigitalInkRecognizer(languageCode: _language);
var _language = 'en';
// Codes from https://developers.google.com/ml-kit/vision/digital-ink-recognition/base-models?hl=en#text
final _languages = [
'en',
'es',
'fr',
'hi',
'it',
'ja',
'pt',
'ru',
'zh-Hani',
];
var _digitalInkRecognizer = DigitalInkRecognizer(languageCode: 'en');
final Ink _ink = Ink();
List<StrokePoint> _points = [];
String _recognizedText = '';
Expand All @@ -31,6 +42,45 @@ class _DigitalInkViewState extends State<DigitalInkView> {
body: SafeArea(
child: Column(
children: [
SizedBox(height: 8),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_buildDropdown(),
ElevatedButton(
onPressed: _isModelDownloaded,
child: Text('Check Model'),
),
ElevatedButton(
onPressed: _downloadModel,
child: Icon(Icons.download),
),
ElevatedButton(
onPressed: _deleteModel,
child: Icon(Icons.delete),
),
],
),
),
SizedBox(height: 8),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: _recogniseText,
child: Text('Read Text'),
),
ElevatedButton(
onPressed: _clearPad,
child: Text('Clear Pad'),
),
],
),
),
Expanded(
child: GestureDetector(
onPanStart: (DragStartDetails details) {
Expand Down Expand Up @@ -69,50 +119,39 @@ class _DigitalInkViewState extends State<DigitalInkView> {
'Candidates: $_recognizedText',
style: TextStyle(fontSize: 23),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: _recogniseText,
child: Text('Read Text'),
),
ElevatedButton(
onPressed: _clearPad,
child: Text('Clear Pad'),
),
],
),
),
SizedBox(height: 8),
Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: _isModelDownloaded,
child: Text('Check Model'),
),
ElevatedButton(
onPressed: _downloadModel,
child: Text('Download'),
),
ElevatedButton(
onPressed: _deleteModel,
child: Text('Delete'),
),
],
),
),
SizedBox(height: 8),
],
),
),
);
}

Widget _buildDropdown() => DropdownButton<String>(
value: _language,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.blue),
underline: Container(
height: 2,
color: Colors.blue,
),
onChanged: (String? lang) {
if (lang != null) {
setState(() {
_language = lang;
_digitalInkRecognizer.close();
_digitalInkRecognizer =
DigitalInkRecognizer(languageCode: _language);
});
}
},
items: _languages.map<DropdownMenuItem<String>>((lang) {
return DropdownMenuItem<String>(
value: lang,
child: Text(lang),
);
}).toList(),
);

void _clearPad() {
setState(() {
_ink.strokes.clear();
Expand Down
68 changes: 59 additions & 9 deletions packages/example/lib/vision_detector_views/text_detector_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class TextRecognizerView extends StatefulWidget {
}

class _TextRecognizerViewState extends State<TextRecognizerView> {
final TextRecognizer _textRecognizer =
TextRecognizer(script: TextRecognitionScript.latin);
var _script = TextRecognitionScript.latin;
var _textRecognizer = TextRecognizer(script: TextRecognitionScript.latin);
bool _canProcess = true;
bool _isBusy = false;
CustomPaint? _customPaint;
Expand All @@ -28,16 +28,66 @@ class _TextRecognizerViewState extends State<TextRecognizerView> {

@override
Widget build(BuildContext context) {
return DetectorView(
title: 'Text Detector',
customPaint: _customPaint,
text: _text,
onImage: _processImage,
initialCameraLensDirection: _cameraLensDirection,
onCameraLensDirectionChanged: (value) => _cameraLensDirection = value,
return Scaffold(
body: Stack(children: [
DetectorView(
title: 'Text Detector',
customPaint: _customPaint,
text: _text,
onImage: _processImage,
initialCameraLensDirection: _cameraLensDirection,
onCameraLensDirectionChanged: (value) => _cameraLensDirection = value,
),
Positioned(
top: 30,
left: 100,
right: 100,
child: Row(
children: [
Spacer(),
Container(
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: _buildDropdown(),
)),
Spacer(),
],
)),
]),
);
}

Widget _buildDropdown() => DropdownButton<TextRecognitionScript>(
value: _script,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.blue),
underline: Container(
height: 2,
color: Colors.blue,
),
onChanged: (TextRecognitionScript? script) {
if (script != null) {
setState(() {
_script = script;
_textRecognizer.close();
_textRecognizer = TextRecognizer(script: _script);
});
}
},
items: TextRecognitionScript.values
.map<DropdownMenuItem<TextRecognitionScript>>((script) {
return DropdownMenuItem<TextRecognitionScript>(
value: script,
child: Text(script.name),
);
}).toList(),
);

Future<void> _processImage(InputImage inputImage) async {
if (!_canProcess) return;
if (_isBusy) return;
Expand Down