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

Feature/doc scanner #607

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
72167a5
Update document scanner
bensonarafat Mar 22, 2024
6edf350
fix: enum variables
bensonarafat Mar 27, 2024
8983fa5
Update document scanner activity return nulls
bensonarafat Apr 10, 2024
ddd5453
Update package name in AndroidManifest.xml
ablause Apr 11, 2024
a044ca5
Refactor document scanner view to handle scan errors
ablause Apr 11, 2024
2d223d8
Fix access to activity class in onAttachedToActivity & add result to …
ablause Apr 11, 2024
8fa2ef8
Update minSdkVersion to 21 in google_mlkit_document_scanner build.gradle
ablause Apr 11, 2024
dc10df9
Merge pull request #1 from ablause/bugfix/document-scanner-activity
bensonarafat Apr 11, 2024
d8fc2d7
Update refactor code
bensonarafat Apr 11, 2024
ffaa57c
Update analysis path
bensonarafat Apr 11, 2024
8616563
Update analysis path
bensonarafat Apr 11, 2024
7c9efa8
Update document scanner readme
bensonarafat Apr 11, 2024
558414f
Update add path to mlkit
bensonarafat Apr 11, 2024
6d3e817
Update changelog
bensonarafat Apr 11, 2024
830bf24
update publish.sh
bensonarafat Apr 12, 2024
968e596
Update pubspec.yaml
bensonarafat Apr 12, 2024
19b7bb2
Update review
bensonarafat Apr 15, 2024
a5b9e45
Merge branch 'master' into master
bensonarafat Apr 15, 2024
990e845
Update pubspec document scanner path
bensonarafat Apr 15, 2024
29bba84
Update pubspec remove relative path
bensonarafat Apr 15, 2024
6a113cb
Update pubspec document scanner path
bensonarafat Apr 15, 2024
e8ef626
Update - remove google_comoon because it is not used
bensonarafat Apr 15, 2024
c9750c1
Update flutter action - add pub add for document scanner
bensonarafat Apr 15, 2024
48a6bf6
Update flutter.yml arrange alphabetical
bensonarafat Apr 15, 2024
fc13676
Update review
bensonarafat Apr 16, 2024
d4ea792
Merge branch 'develop' into master
bensonarafat Apr 16, 2024
6414727
Rename GoogleMlkitDocumentScannerPlugin.m to GoogleMlKitDocumentScann…
bensonarafat Apr 16, 2024
d9a51b7
Rename GoogleMlkitDocumentScannerPlugin.h to GoogleMlKitDocumentScann…
bensonarafat Apr 16, 2024
224c9db
Update readme
bensonarafat Apr 16, 2024
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
4 changes: 4 additions & 0 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ jobs:
working-directory: ./packages/google_mlkit_digital_ink_recognition
run: flutter pub get

- name: Install dependencies for google_mlkit_document_scanner
working-directory: ./packages/google_mlkit_document_scanner
run: flutter pub get

- name: Install dependencies for google_mlkit_language_id
working-directory: ./packages/google_mlkit_language_id
run: flutter pub get
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Google's ML Kit for Flutter is a set of [Flutter plugins](https://flutter.io/pla
| [Digital Ink Recognition](https://developers.google.com/ml-kit/vision/digital-ink-recognition) | [google\_mlkit\_digital\_ink\_recognition](https://pub.dev/packages/google_mlkit_digital_ink_recognition) [![Pub Version](https://img.shields.io/pub/v/google_mlkit_digital_ink_recognition)](https://pub.dev/packages/google_mlkit_digital_ink_recognition) | [![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_digital_ink_recognition) | ✅ | ✅ |
| [Pose Detection](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) | ✅ | ✅ |
| [Selfie Segmentation](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) | ✅ | ✅ |
| [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) | ✅ | ❌ |

### Natural Language APIs

Expand Down
2 changes: 2 additions & 0 deletions packages/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'nlp_detector_views/language_translator_view.dart';
import 'nlp_detector_views/smart_reply_view.dart';
import 'vision_detector_views/barcode_scanner_view.dart';
import 'vision_detector_views/digital_ink_recognizer_view.dart';
import 'vision_detector_views/document_scanner_view.dart';
import 'vision_detector_views/face_detector_view.dart';
import 'vision_detector_views/face_mesh_detector_view.dart';
import 'vision_detector_views/label_detector_view.dart';
Expand Down Expand Up @@ -58,6 +59,7 @@ class Home extends StatelessWidget {
CustomCard('Digital Ink Recognition', DigitalInkView()),
CustomCard('Pose Detection', PoseDetectorView()),
CustomCard('Selfie Segmentation', SelfieSegmenterView()),
CustomCard('Document Scanner', DocumentScannerView()),
],
),
SizedBox(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'package:google_mlkit_document_scanner/google_mlkit_document_scanner.dart';

class DocumentScannerView extends StatefulWidget {
@override
State<DocumentScannerView> createState() => _DocumentScannerViewState();
}

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();
}

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Document Scanner'),
centerTitle: true,
elevation: 0,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
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),
),
),
),
onPressed: startScan,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: const Text(
'Start Scan',
style: TextStyle(color: Colors.white),
),
),
),
],
),
),
);
}

void startScan() async {
try {
documents = await documentScanner.scanDocument();
print('documents: $documents');
} catch (e) {
print('Error: $e');
}
}
}
9 changes: 8 additions & 1 deletion packages/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ packages:
relative: true
source: path
version: "0.10.0"
google_mlkit_document_scanner:
dependency: "direct main"
description:
path: "../google_mlkit_document_scanner"
relative: true
source: path
version: "0.0.1"
google_mlkit_entity_extraction:
dependency: "direct main"
description:
Expand Down Expand Up @@ -490,7 +497,7 @@ packages:
sha256: "43798d895c929056255600343db8f049921cbec94d31ec87f1dc5c16c01935dd"
url: "https://pub.dev"
source: hosted
version: "2.1.5"
version: "2.1.8"
quiver:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions packages/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies:
path: ../google_mlkit_selfie_segmentation
google_mlkit_text_recognition:
path: ../google_mlkit_text_recognition
google_mlkit_document_scanner:
path: ../google_mlkit_document_scanner

google_mlkit_entity_extraction:
path: ../google_mlkit_entity_extraction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
private void handleDetection(MethodCall call, final MethodChannel.Result result) {
String tag = call.argument("model");
DigitalInkRecognitionModel model = getModel(tag, result);
if (model == null) return;
if (model == null)
return;
if (!genericModelManager.isModelDownloaded(model)) {
result.error("Model Error", "Model has not been downloaded yet ", null);
return;
Expand All @@ -79,7 +80,7 @@ private void handleDetection(MethodCall call, final MethodChannel.Result result)
if (t0 instanceof Integer) {
t = (int) t0;
} else {
t = (long) t0;
t = (long) t0;
}
Ink.Point strokePoint = Ink.Point.create(x, y, t);
strokeBuilder.addPoint(strokePoint);
Expand Down Expand Up @@ -137,7 +138,8 @@ private void process(RecognitionResult recognitionResult, final MethodChannel.Re
private void closeDetector(MethodCall call) {
String id = call.argument("id");
com.google.mlkit.vision.digitalink.DigitalInkRecognizer recognizer = instances.get(id);
if (recognizer == null) return;
if (recognizer == null)
return;
recognizer.close();
instances.remove(id);
}
Expand Down
29 changes: 29 additions & 0 deletions packages/google_mlkit_document_scanner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
3 changes: 3 additions & 0 deletions packages/google_mlkit_document_scanner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* Initial release.
21 changes: 21 additions & 0 deletions packages/google_mlkit_document_scanner/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Francisco Bernal, Bharat Biradar and Benson Arafat.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
107 changes: 107 additions & 0 deletions packages/google_mlkit_document_scanner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Google's ML Kit Document scanner for Flutter

[![Pub Version](https://img.shields.io/pub/v/google_mlkit_document_scanner)](https://pub.dev/packages/google_mlkit_document_scanner)
[![analysis](https://github.com/flutter-ml/google_ml_kit_flutter/actions/workflows/flutter.yml/badge.svg)](https://github.com/flutter-ml/google_ml_kit_flutter/actions)
[![Star on Github](https://img.shields.io/github/stars/flutter-ml/google_ml_kit_flutter.svg?style=flat&logo=github&colorB=deeppink&label=stars)](https://github.com/flutter-ml/google_ml_kit_flutter)
[![License: MIT](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT)

A Flutter plugin to use [Google's ML Kit Document Scanner](https://developers.google.com/ml-kit/vision/doc-scanner) to digitize physical documents, whicj allows users to convert physical documents into digital formats. ML Kit's document scanner API provides a comprehensive solution with a high-quality, consistent UI flow across Android apps and devices. Once the document scanner flow is triggered from your app, users retain full control over the scanning process. They can optionally crop the scanned documents, apply filters, remove shadows or stains, and easily send the digitized files back to your app.

The UI flow, ML models and other large resources are delivered using Google Play services, which means:

- Low binary size impact (all ML models and large resources are downloaded centrally in Google Play services).
- No camera permission is required - the document scanner leverages the Google Play services' camera permission, and users are in control of which files to share back with your app.

Key capabilities
- High-quality and consistent user interface for digitizing physical documents.
- Automatic capture with document detection.
- Accurate edge detection for optimal crop results.
- Automatic rotation detection to show documents upright.
- No camera permission is needed from your app.
- Low apk binary size impact.

Customization

The document scanner API provides a high-quality fully fledged UI flow that is consistent across Android apps. However, there is also room to customize some aspects of the user experience
- Maximum number of pages
- Gallery import
- Editing functionalities

**NOTE** Since [Google's Document Scanner](https://developers.google.com/ml-kit/vision/doc-scanner) API is still in Beta and only supports Android. Stay tune for updates in their website.

**PLEASE READ THIS** before continuing or posting a [new issue](https://github.com/flutter-ml/google_ml_kit_flutter/issues):

- [Google's ML Kit](https://developers.google.com/ml-kit) was build only for mobile platforms: iOS and Android apps.

- This plugin is not sponsored or maintained by Google. The [authors](https://github.com/flutter-ml/google_ml_kit_flutter/blob/master/AUTHORS) are developers excited about Machine Learning that wanted to expose Google's native APIs to Flutter.

- Google's ML Kit APIs are only developed natively for iOS and Android. This plugin uses Flutter Platform Channels as explained [here](https://docs.flutter.dev/development/platform-integration/platform-channels).

Messages are passed between the client (the app/plugin) and host (platform) using platform channels as illustrated in this diagram:

<p align="center" width="100%">
<img src="https://docs.flutter.dev/assets/images/docs/PlatformChannels.png">
</p>

Messages and responses are passed asynchronously, to ensure the user interface remains responsive. To read more about platform channels go [here](https://docs.flutter.dev/development/platform-integration/platform-channels).

Because this plugin uses platform channels, no Machine Learning processing is done in Flutter/Dart, all the calls are passed to the native platform using `MethodChannel` in Android and `FlutterMethodChannel` in iOS, and executed using Google's native APIs. Think of this plugin as a bridge between your app and Google's native ML Kit APIs. This plugin only passes the call to the native API and the processing is done by Google's API. It is important that you understand this concept when it comes to debugging errors for your ML model and/or app.

- Since the plugin uses platform channels, you may encounter issues with the native API. Before submitting a new issue, identify the source of the issue. You can run both iOS and/or Android native [example apps by Google](https://github.com/googlesamples/mlkit) and make sure that the issue is not reproducible with their native examples. If you can reproduce the issue in their apps then report the issue to Google. The [authors](https://github.com/flutter-ml/google_ml_kit_flutter/blob/master/AUTHORS) do not have access to the source code of their native APIs, so you need to report the issue to them. If you find that their example apps are okay and still you have an issue using this plugin, then look at our [closed and open issues](https://github.com/flutter-ml/google_ml_kit_flutter/issues). If you cannot find anything that can help you then report the issue and provide enough details. Be patient, someone from the community will eventually help you.

## Requirements

### iOS
Not supported

### Android

- minSdkVersion: 21
- targetSdkVersion: 33
- compileSdkVersion: 33

## Usage

### Document Scanner

#### Create an instance of `DocumentScannerOptions`

```dart
DocumentScannerOptions documentOptions = DocumentScannerOptions(
mode: ScannerMode.filter // to control the feature sets in the flow
isGalleryImport: true, // importing from the photo gallery
pageLimit: 1, // setting a limit to the number of pages scanned
);
```

#### Create an instance of `DocumentScanner`

```dart
final documentScanner = DocumentScanner(option: documentOptions);
```

#### Start Scanner

Returns paths of the scanned documents

```dart
List<String>? documents = await documentScanner.scanDocument();

```

#### Release resources with `close()`

```dart
documentScanner.close();
```

## Example app

Find the example app [here](https://github.com/flutter-ml/google_ml_kit_flutter/tree/master/packages/example).

## Contributing

Contributions are welcome.
In case of any problems look at [existing issues](https://github.com/flutter-ml/google_ml_kit_flutter/issues), if you cannot find anything related to your problem then open an issue.
Create an issue before opening a [pull request](https://github.com/flutter-ml/google_ml_kit_flutter/pulls) for non trivial fixes.
In case of trivial fixes open a [pull request](https://github.com/flutter-ml/google_ml_kit_flutter/pulls) directly.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../google_ml_kit/analysis_options.yaml
9 changes: 9 additions & 0 deletions packages/google_mlkit_document_scanner/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx
43 changes: 43 additions & 0 deletions packages/google_mlkit_document_scanner/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
group 'com.google_mlkit_document_scanner'
version '1.0'

buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
}
}

rootProject.allprojects {
repositories {
google()
mavenCentral()
}
}

apply plugin: 'com.android.library'

android {
if (project.android.hasProperty("namespace")) {
namespace 'com.google_mlkit_document_scanner'
}

compileSdkVersion 34

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion 21
}
}

dependencies {
implementation 'com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'google_mlkit_document_scanner'
Loading
Loading