Skip to content

Commit cda1145

Browse files
authored
chore: Update Document Scanner to use latest dependency (#834)
1 parent 59a9913 commit cda1145

7 files changed

Lines changed: 118 additions & 99 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Google's ML Kit for Flutter is a set of [Flutter plugins](https://flutter.io/pla
1818
| [Pose Detection (Beta)](https://developers.google.com/ml-kit/vision/pose-detection) | [google_mlkit_pose_detection](https://pub.dev/packages/google_mlkit_pose_detection) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_pose_detection)](https://pub.dev/packages/google_mlkit_pose_detection) | [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/google_mlkit_pose_detection) |||
1919
| [Selfie Segmentation (Beta)](https://developers.google.com/ml-kit/vision/selfie-segmentation) | [google_mlkit_selfie_segmentation](https://pub.dev/packages/google_mlkit_selfie_segmentation) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_selfie_segmentation)](https://pub.dev/packages/google_mlkit_selfie_segmentation) | [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/google_mlkit_selfie_segmentation) |||
2020
| [Subject Segmentation (Beta)](https://developers.google.com/ml-kit/vision/subject-segmentation) | [google_mlkit_subject_segmentation](https://pub.dev/packages/google_mlkit_subject_segmentation) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_subject_segmentation)](https://pub.dev/packages/google_mlkit_subject_segmentation) | [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/google_mlkit_subject_segmentation) |||
21-
| [Document Scanner (Beta)](https://developers.google.com/ml-kit/vision/doc-scanner) | [google_mlkit_document_scanner](https://pub.dev/packages/google_mlkit_document_scanner) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_document_scanner)](https://pub.dev/packages/google_mlkit_document_scanner) | [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/google_mlkit_document_scanner) |||
21+
| [Document Scanner](https://developers.google.com/ml-kit/vision/doc-scanner) | [google_mlkit_document_scanner](https://pub.dev/packages/google_mlkit_document_scanner) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_document_scanner)](https://pub.dev/packages/google_mlkit_document_scanner) | [![GitHub](https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white)](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/google_mlkit_document_scanner) || |
2222

2323
### Natural Language APIs
2424

packages/example/lib/vision_detector_views/document_scanner_view.dart

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import 'dart:collection';
12
import 'dart:io';
23

34
import 'package:flutter/material.dart';
45
import 'package:flutter_pdfview/flutter_pdfview.dart';
56
import 'package:google_mlkit_document_scanner/google_mlkit_document_scanner.dart';
67

8+
typedef MenuEntry = DropdownMenuEntry<String>;
9+
const List<String> list = <String>['Select option', 'pdf', 'jpeg', 'pdf-jpeg'];
10+
711
class DocumentScannerView extends StatefulWidget {
812
@override
913
State<DocumentScannerView> createState() => _DocumentScannerViewState();
@@ -12,7 +16,11 @@ class DocumentScannerView extends StatefulWidget {
1216
class _DocumentScannerViewState extends State<DocumentScannerView> {
1317
DocumentScanner? _documentScanner;
1418
DocumentScanningResult? _result;
15-
19+
static final List<MenuEntry> menuEntries = UnmodifiableListView<MenuEntry>(
20+
list.map<MenuEntry>((String name) => MenuEntry(
21+
value: name,
22+
label:
23+
name == 'Select option' ? name : 'Scan ${name.toUpperCase()}')));
1624
@override
1725
void dispose() {
1826
_documentScanner?.close();
@@ -27,103 +35,84 @@ class _DocumentScannerViewState extends State<DocumentScannerView> {
2735
centerTitle: true,
2836
elevation: 0,
2937
),
30-
body: Center(
31-
child: Column(
32-
mainAxisAlignment: MainAxisAlignment.start,
33-
children: [
34-
Row(
35-
mainAxisAlignment: MainAxisAlignment.center,
36-
children: [
37-
Icon(
38-
Icons.document_scanner_outlined,
39-
size: 50,
40-
),
41-
SizedBox(width: 8),
42-
ElevatedButton(
43-
style: ButtonStyle(
44-
backgroundColor:
45-
WidgetStateProperty.all<Color>(Colors.black),
46-
shape: WidgetStateProperty.all(
47-
RoundedRectangleBorder(
48-
borderRadius: BorderRadius.circular(8),
49-
),
50-
),
51-
),
52-
onPressed: () => startScan(DocumentFormat.pdf),
53-
child: Padding(
54-
padding: const EdgeInsets.symmetric(vertical: 4),
55-
child: const Text(
56-
'Scan PDF',
57-
style: TextStyle(color: Colors.white),
58-
),
38+
body: SingleChildScrollView(
39+
child: Center(
40+
child: Column(
41+
mainAxisAlignment: MainAxisAlignment.start,
42+
children: [
43+
Row(
44+
mainAxisAlignment: MainAxisAlignment.center,
45+
children: [
46+
Icon(
47+
Icons.document_scanner_outlined,
48+
size: 50,
5949
),
50+
SizedBox(width: 8),
51+
DropdownMenu<String>(
52+
initialSelection: list.first,
53+
onSelected: (String? value) {
54+
if (value != null) {
55+
if (value == 'pdf') {
56+
startScan({DocumentFormat.pdf});
57+
}
58+
if (value == 'jpeg') {
59+
startScan({DocumentFormat.jpeg});
60+
}
61+
if (value == 'pdf-jpeg') {
62+
startScan(
63+
{DocumentFormat.pdf, DocumentFormat.jpeg});
64+
}
65+
}
66+
},
67+
dropdownMenuEntries: menuEntries),
68+
],
69+
),
70+
if (_result?.pdf != null) ...[
71+
Padding(
72+
padding: const EdgeInsets.only(
73+
top: 16, bottom: 8, right: 8, left: 8),
74+
child: Align(
75+
alignment: Alignment.centerLeft,
76+
child: Text('PDF Document:')),
6077
),
61-
SizedBox(width: 8),
62-
ElevatedButton(
63-
style: ButtonStyle(
64-
backgroundColor:
65-
WidgetStateProperty.all<Color>(Colors.black),
66-
shape: WidgetStateProperty.all(
67-
RoundedRectangleBorder(
68-
borderRadius: BorderRadius.circular(8),
69-
),
70-
),
71-
),
72-
onPressed: () => startScan(DocumentFormat.jpeg),
73-
child: Padding(
74-
padding: const EdgeInsets.symmetric(vertical: 4),
75-
child: const Text(
76-
'Scan JPEG',
77-
style: TextStyle(color: Colors.white),
78-
),
78+
SizedBox(
79+
height: 300,
80+
child: PDFView(
81+
filePath: _result!.pdf!.uri,
82+
enableSwipe: true,
83+
swipeHorizontal: true,
84+
autoSpacing: false,
85+
pageFling: false,
7986
),
8087
),
8188
],
82-
),
83-
if (_result?.pdf != null) ...[
84-
Padding(
85-
padding: const EdgeInsets.only(
86-
top: 16, bottom: 8, right: 8, left: 8),
87-
child: Align(
88-
alignment: Alignment.centerLeft,
89-
child: Text('PDF Document:')),
90-
),
91-
SizedBox(
92-
height: 300,
93-
child: PDFView(
94-
filePath: _result!.pdf!.uri,
95-
enableSwipe: true,
96-
swipeHorizontal: true,
97-
autoSpacing: false,
98-
pageFling: false,
89+
if (_result?.images?.isNotEmpty == true) ...[
90+
Padding(
91+
padding: const EdgeInsets.only(
92+
top: 16, bottom: 8, right: 8, left: 8),
93+
child: Align(
94+
alignment: Alignment.centerLeft,
95+
child: Text('Images [0]:')),
9996
),
100-
),
101-
],
102-
if (_result?.images.isNotEmpty == true) ...[
103-
Padding(
104-
padding: const EdgeInsets.only(
105-
top: 16, bottom: 8, right: 8, left: 8),
106-
child: Align(
107-
alignment: Alignment.centerLeft,
108-
child: Text('Images [0]:')),
109-
),
110-
SizedBox(
111-
height: 400, child: Image.file(File(_result!.images.first))),
97+
SizedBox(
98+
height: 400,
99+
child: Image.file(File(_result!.images!.first))),
100+
],
112101
],
113-
],
102+
),
114103
),
115104
),
116105
);
117106
}
118107

119-
void startScan(DocumentFormat format) async {
108+
void startScan(Set<DocumentFormat> formats) async {
120109
try {
121110
_result = null;
122111
setState(() {});
123112
_documentScanner?.close();
124113
_documentScanner = DocumentScanner(
125114
options: DocumentScannerOptions(
126-
documentFormat: format,
115+
documentFormats: formats,
127116
mode: ScannerMode.full,
128117
isGalleryImport: false,
129118
pageLimit: 1,

packages/google_mlkit_document_scanner/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ The document scanner API provides a high-quality fully fledged UI flow that is c
5555

5656
### iOS
5757

58-
This feature is still in Beta, and it is only available for Android. Stay tune for updates in [Google's website](https://developers.google.com/ml-kit/vision/doc-scanner) and request the feature [here](https://github.com/googlesamples/mlkit/issues).
58+
This feature is only available for Android. Stay tune for updates in [Google's website](https://developers.google.com/ml-kit/vision/doc-scanner) and request the feature [here](https://github.com/googlesamples/mlkit/issues).
5959

6060
### Android
6161

@@ -70,8 +70,13 @@ This feature is still in Beta, and it is only available for Android. Stay tune f
7070
#### Create an instance of `DocumentScannerOptions`
7171

7272
```dart
73+
// set output document formats
74+
const Set<DocumentFormat> documentFormats = {
75+
DocumentFormat.jpeg,
76+
DocumentFormat.pdf
77+
};
7378
DocumentScannerOptions documentOptions = DocumentScannerOptions(
74-
documentFormat: DocumentFormat.jpeg, // set output document format
79+
documentFormats: documentFormats,
7580
mode: ScannerMode.filter, // to control what features are enabled
7681
pageLimit: 1, // setting a limit to the number of pages scanned
7782
isGalleryImport: true, // importing from the photo gallery

packages/google_mlkit_document_scanner/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ android {
3636
}
3737

3838
dependencies {
39-
implementation("com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1")
39+
implementation("com.google.android.gms:play-services-mlkit-document-scanner:16.0.0")
4040
}
4141
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0-milestone-1-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

packages/google_mlkit_document_scanner/android/src/main/java/com/google_mlkit_document_scanner/DocumentScanner.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,21 @@ public void onFailure(@NonNull Exception e) {
9595
private GmsDocumentScannerOptions parseOptions(Map<String, Object> options) {
9696
boolean isGalleryImportAllowed = (boolean) options.get("isGalleryImport");
9797
int pageLimit = (int) options.get("pageLimit");
98-
int format;
99-
switch ((String) Objects.requireNonNull(options.get("format"))) {
100-
case "pdf":
101-
format = GmsDocumentScannerOptions.RESULT_FORMAT_PDF;
102-
break;
103-
case "jpeg":
104-
format = GmsDocumentScannerOptions.RESULT_FORMAT_JPEG;
105-
break;
106-
default:
107-
throw new IllegalArgumentException("Not a format:" + options.get("format"));
98+
List<String> formatStrings = (List<String>) options.get("formats");
99+
List<Integer> formatConstants = new ArrayList<>();
100+
for (String format: formatStrings) {
101+
switch (format) {
102+
case "pdf":
103+
formatConstants.add(GmsDocumentScannerOptions.RESULT_FORMAT_PDF);
104+
break;
105+
case "jpeg":
106+
formatConstants.add(GmsDocumentScannerOptions.RESULT_FORMAT_JPEG);
107+
break;
108+
default:
109+
throw new IllegalArgumentException("Not a format:" + options.get("format"));
110+
}
108111
}
112+
109113
int mode;
110114
switch ((String) options.get("mode")) {
111115
case "base":
@@ -120,7 +124,21 @@ private GmsDocumentScannerOptions parseOptions(Map<String, Object> options) {
120124
default:
121125
throw new IllegalArgumentException("Not a mode:" + options.get("mode"));
122126
}
123-
GmsDocumentScannerOptions.Builder builder = new GmsDocumentScannerOptions.Builder().setGalleryImportAllowed(isGalleryImportAllowed).setPageLimit(pageLimit).setResultFormats(format).setScannerMode(mode);
127+
GmsDocumentScannerOptions.Builder builder = new GmsDocumentScannerOptions
128+
.Builder()
129+
.setGalleryImportAllowed(isGalleryImportAllowed)
130+
.setPageLimit(pageLimit)
131+
.setScannerMode(mode);
132+
133+
// Set formats
134+
if (!formatConstants.isEmpty()) {
135+
if(formatConstants.size() > 1) {
136+
builder.setResultFormats(formatConstants.get(0), formatConstants.get(1));
137+
} else {
138+
builder.setResultFormats(formatConstants.get(0));
139+
}
140+
141+
}
124142
return builder.build();
125143
}
126144

packages/google_mlkit_document_scanner/lib/src/document_scanner.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DocumentScanner {
3535
class DocumentScannerOptions {
3636
/// Constructor for [DocumentScannerOptions].
3737
DocumentScannerOptions({
38-
this.documentFormat = DocumentFormat.jpeg,
38+
this.documentFormats = const {DocumentFormat.jpeg},
3939
this.pageLimit = 1,
4040
this.mode = ScannerMode.full,
4141
this.isGalleryImport = false,
@@ -46,7 +46,7 @@ class DocumentScannerOptions {
4646

4747
/// Sets scanner result formats.
4848
/// Available formats: PDF, JPG and default format is JPG.
49-
final DocumentFormat documentFormat;
49+
final Set<DocumentFormat> documentFormats;
5050

5151
/// Sets the scanner mode which determines what features are enabled. default = ScannerModel.full.
5252
final ScannerMode mode;
@@ -57,7 +57,7 @@ class DocumentScannerOptions {
5757
/// Returns a json representation of an instance of [DocumentScannerOptions].
5858
Map<String, dynamic> toJson() => {
5959
'pageLimit': pageLimit,
60-
'format': documentFormat.name,
60+
'formats': documentFormats.map((f) => f.name).toList(),
6161
'mode': mode.name,
6262
'isGalleryImport': isGalleryImport,
6363
};
@@ -82,7 +82,7 @@ class DocumentScanningResult {
8282
final DocumentScanningResultPdf? pdf;
8383

8484
/// Returns the scanned images or null if `DocumentFormat.jpeg` was not specified when creating the scanner options.
85-
final List<String> images;
85+
final List<String>? images;
8686

8787
/// Constructor to create an instance of [DocumentScanningResult].
8888
DocumentScanningResult({required this.pdf, required this.images});

0 commit comments

Comments
 (0)