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 2 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
2 changes: 1 addition & 1 deletion packages/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ SPEC CHECKSUMS:
camera_avfoundation: 3125e8cd1a4387f6f31c6c63abb8a55892a9eeeb
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
google_mlkit_barcode_scanning: ec351dc4eeea0e004996607817b75a0b9fd278ba
google_mlkit_commons: 26607e403b3301ec966e578a308c98c6ac3e9477
google_mlkit_commons: 803b9b2fc28852082ae592a2db594c1e467b2f31
google_mlkit_digital_ink_recognition: 07fb2aae2d704592be8f6c2ff63f3adcfd3d768c
google_mlkit_entity_extraction: 480e6029eaacf7fac2305276dc111978bd351bb5
google_mlkit_face_detection: 6930c8cecb10f49921ff7010c165eb1d803e87fd
Expand Down
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