Skip to content

Commit

Permalink
feat: DocumentScanner returns DocumentScanningResult
Browse files Browse the repository at this point in the history
  • Loading branch information
fbernaly committed Apr 19, 2024
1 parent 7e8206f commit 68973c8
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 253 deletions.
9 changes: 7 additions & 2 deletions packages/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PODS:
- camera_avfoundation (0.0.1):
- Flutter
- Flutter (1.0.0)
- flutter_pdfview (1.0.2):
- Flutter
- google_mlkit_barcode_scanning (0.10.0):
- Flutter
- google_mlkit_commons
Expand All @@ -15,7 +17,6 @@ PODS:
- GoogleMLKit/DigitalInkRecognition (~> 4.0.0)
- google_mlkit_document_scanner (0.0.1):
- Flutter
- google_mlkit_commons
- google_mlkit_entity_extraction (0.11.0):
- Flutter
- google_mlkit_commons
Expand Down Expand Up @@ -285,6 +286,7 @@ PODS:
DEPENDENCIES:
- camera_avfoundation (from `.symlinks/plugins/camera_avfoundation/ios`)
- Flutter (from `Flutter`)
- flutter_pdfview (from `.symlinks/plugins/flutter_pdfview/ios`)
- google_mlkit_barcode_scanning (from `.symlinks/plugins/google_mlkit_barcode_scanning/ios`)
- google_mlkit_commons (from `.symlinks/plugins/google_mlkit_commons/ios`)
- google_mlkit_digital_ink_recognition (from `.symlinks/plugins/google_mlkit_digital_ink_recognition/ios`)
Expand Down Expand Up @@ -355,6 +357,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/camera_avfoundation/ios"
Flutter:
:path: Flutter
flutter_pdfview:
:path: ".symlinks/plugins/flutter_pdfview/ios"
google_mlkit_barcode_scanning:
:path: ".symlinks/plugins/google_mlkit_barcode_scanning/ios"
google_mlkit_commons:
Expand Down Expand Up @@ -393,10 +397,11 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
camera_avfoundation: 3125e8cd1a4387f6f31c6c63abb8a55892a9eeeb
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_pdfview: 25f53dd6097661e6395b17de506e6060585946bd
google_mlkit_barcode_scanning: d42717e8a28a5523bcb0b8a285c7d19555021c68
google_mlkit_commons: 3857c1e9f23ca02073f8dd34a7a3580feb814cef
google_mlkit_digital_ink_recognition: d389ce65a846a29ca6e5a67d0257284f54cc9788
google_mlkit_document_scanner: 45c54f9f04522d5730660f041234af05bb57d9e3
google_mlkit_document_scanner: b796a4614554b9ac09d21e118d1978ba3bf3b5bc
google_mlkit_entity_extraction: ce59dde3a8342e820121d9283f97e1fb16fd6f28
google_mlkit_face_detection: 52f7e4c15a7c49b98940fa32af4c2b10fd8608fc
google_mlkit_face_mesh_detection: 8d536ecd0323efb7595445be86800bf37afb3e53
Expand Down
133 changes: 96 additions & 37 deletions packages/example/lib/vision_detector_views/document_scanner_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
import 'package:google_mlkit_document_scanner/google_mlkit_document_scanner.dart';

class DocumentScannerView extends StatefulWidget {
Expand All @@ -7,22 +10,12 @@ class DocumentScannerView extends StatefulWidget {
}

class _DocumentScannerViewState extends State<DocumentScannerView> {
DocumentScanner documentScanner = DocumentScanner(
options: DocumentScannerOptions(
mode: ScannerMode.filter, // to control the feature sets in the flow
isGalleryImport: false, // importing from the photo gallery
pageLimit: 1, // setting a limit to the number of pages scanned
),
);
List<String>? documents;
@override
void initState() {
super.initState();
}
DocumentScanner? _documentScanner;
DocumentScanningResult? _result;

@override
void dispose() {
documentScanner.close();
_documentScanner?.close();
super.dispose();
}

Expand All @@ -36,43 +29,109 @@ class _DocumentScannerViewState extends State<DocumentScannerView> {
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
Icons.document_scanner_outlined,
size: 250,
),
SizedBox(
height: 50,
),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.black),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.document_scanner_outlined,
size: 50,
),
SizedBox(width: 8),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
onPressed: () => startScan(DocumentFormat.pdf),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: const Text(
'Scan PDF',
style: TextStyle(color: Colors.white),
),
),
),
SizedBox(width: 8),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.black),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
onPressed: () => startScan(DocumentFormat.jpeg),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: const Text(
'Scan JPEG',
style: TextStyle(color: Colors.white),
),
),
),
],
),
if (_result?.pdf != null) ...[
Padding(
padding: const EdgeInsets.only(
top: 16, bottom: 8, right: 8, left: 8),
child: Align(
alignment: Alignment.centerLeft,
child: Text('PDF Document:')),
),
onPressed: startScan,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: const Text(
'Start Scan',
style: TextStyle(color: Colors.white),
SizedBox(
height: 300,
child: PDFView(
filePath: _result!.pdf!.uri,
enableSwipe: true,
swipeHorizontal: true,
autoSpacing: false,
pageFling: false,
),
),
),
],
if (_result?.images.isNotEmpty == true) ...[
Padding(
padding: const EdgeInsets.only(
top: 16, bottom: 8, right: 8, left: 8),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Images [0]:')),
),
SizedBox(
height: 400, child: Image.file(File(_result!.images.first))),
],
],
),
),
);
}

void startScan() async {
void startScan(DocumentFormat format) async {
try {
documents = await documentScanner.scanDocument();
print('documents: $documents');
_result = null;
setState(() {});
_documentScanner?.close();
_documentScanner = DocumentScanner(
options: DocumentScannerOptions(
documentFormat: format,
mode: ScannerMode.full,
isGalleryImport: false,
pageLimit: 1,
),
);
_result = await _documentScanner?.scanDocument();
print('result: $_result');
setState(() {});
} catch (e) {
print('Error: $e');
}
Expand Down
8 changes: 8 additions & 0 deletions packages/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_pdfview:
dependency: "direct main"
description:
name: flutter_pdfview
sha256: a9055bf920c7095bf08c2781db431ba23577aa5da5a056a7152dc89a18fbec6f
url: "https://pub.dev"
source: hosted
version: "1.3.2"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_pdfview: ^1.3.2
image_picker: ^1.1.0
camera: ^0.10.5+9
path: ^1.9.0
Expand Down
6 changes: 4 additions & 2 deletions packages/google_mlkit_document_scanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ final documentScanner = DocumentScanner(option: documentOptions);

#### Start Scanner

Returns paths of the scanned documents
The scanner returns objects for the scanned document.

```dart
List<String>? documents = await documentScanner.scanDocument();
DocumentScanningResult result = await documentScanner.scanDocument();
final pdf = result.pdf; // A PDF object.
final images = result.images; // A list with the paths to the images.
```

#### Release resources with `close()`
Expand Down
Loading

0 comments on commit 68973c8

Please sign in to comment.