Skip to content

Commit

Permalink
Merge pull request #48 from jachzen/feature/mlkit
Browse files Browse the repository at this point in the history
Exchanged Android implementation with MLKit Document scanner
  • Loading branch information
vicajilau authored Feb 28, 2024
2 parents 5e5ad67 + c21a38e commit 6be0bb6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.0
* Use ML kit on Android
* dropped nocrop support
* image quality dropped

## 1.1.5
* Nmed parameters
* crop default is false
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This would limit the number of pages to one.

### Step 1

- Fork this project's repo :
- Fork this project's repo :

### Step 2

Expand Down
3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.websitebeaver:documentscanner:1.3.5'
// implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1'
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package biz.cunning.cunning_document_scanner

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import androidx.core.app.ActivityCompat
import com.websitebeaver.documentscanner.DocumentScannerActivity
import com.websitebeaver.documentscanner.constants.DocumentScannerExtra
import android.content.IntentSender
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.RESULT_FORMAT_JPEG
import com.google.mlkit.vision.documentscanner.GmsDocumentScannerOptions.SCANNER_MODE_FULL
import com.google.mlkit.vision.documentscanner.GmsDocumentScanning
import com.google.mlkit.vision.documentscanner.GmsDocumentScanningResult
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
Expand All @@ -15,6 +16,7 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry


/** CunningDocumentScannerPlugin */
class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private var delegate: PluginRegistry.ActivityResultListener? = null
Expand All @@ -37,11 +39,9 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA

override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "getPictures") {
val crop = call.argument<Boolean>("crop") ?: true;
val noOfPages = call.argument<Int>("noOfPages") ?: 50;
val imageQuality = call.argument<Int>("imageQuality") ?: 100;
this.pendingResult = result
startScan(crop, noOfPages, imageQuality)
val noOfPages = call.argument<Int>("noOfPages") ?: 50;
this.pendingResult = result
startScan(noOfPages)
} else {
result.notImplemented()
}
Expand Down Expand Up @@ -74,15 +74,11 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}

// get an array with scanned document file paths
val croppedImageResults =
data?.getStringArrayListExtra("croppedImageResults")?.toList()
?: throw Exception("No cropped images returned")
val scanningResult: GmsDocumentScanningResult = data?.extras?.get("extra_scanning_result") as GmsDocumentScanningResult

// return a list of file paths
// removing file uri prefix as Flutter file will have problems with it
val successResponse = croppedImageResults.map {
it.removePrefix("file://")
}.toList()
val successResponse = scanningResult.pages?.map {
it.imageUri.toString().removePrefix("file://")
}?.toList()

// trigger the success event handler with an array of cropped images
this.pendingResult?.success(successResponse)
Expand All @@ -106,42 +102,27 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}


/**
* create intent to launch document scanner and set custom options
*/
private fun createDocumentScanIntent(crop: Boolean, noOfPages: Int, imageQuality: Int): Intent {
val documentScanIntent = Intent(activity, DocumentScannerActivity::class.java)
documentScanIntent.putExtra(
DocumentScannerExtra.EXTRA_LET_USER_ADJUST_CROP,
crop
)
documentScanIntent.putExtra(
DocumentScannerExtra.EXTRA_MAX_NUM_DOCUMENTS,
noOfPages
)
documentScanIntent.putExtra(
DocumentScannerExtra.EXTRA_CROPPED_IMAGE_QUALITY,
if(imageQuality>100) 100 else if(imageQuality<0) 0 else imageQuality
)

return documentScanIntent
}


/**
* add document scanner result handler and launch the document scanner
*/
private fun startScan(crop: Boolean, noOfPages: Int, imageQuality: Int) {
val intent = createDocumentScanIntent(crop, noOfPages, imageQuality)
try {
ActivityCompat.startActivityForResult(
this.activity,
intent,
START_DOCUMENT_ACTIVITY,
null
)
} catch (e: ActivityNotFoundException) {
pendingResult?.error("ERROR", "FAILED TO START ACTIVITY", null)
private fun startScan(noOfPages: Int) {
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(false)
.setPageLimit(noOfPages)
.setResultFormats(RESULT_FORMAT_JPEG)
.setScannerMode(SCANNER_MODE_FULL)
.build()
val scanner = GmsDocumentScanning.getClient(options)
scanner.getStartScanIntent(activity).addOnSuccessListener {
try {
// Use a custom request code for onActivityResult identification
activity.startIntentSenderForResult(it, START_DOCUMENT_ACTIVITY, null, 0, 0, 0)

} catch (e: IntentSender.SendIntentException) {
pendingResult?.error("ERROR", "Failed to start document scanner", null)
}
}.addOnFailureListener {
pendingResult?.error("ERROR", "Failed to start document scanner Intent", null)
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _MyAppState extends State<MyApp> {
void onPressed() async {
List<String> pictures;
try {
pictures = await CunningDocumentScanner.getPictures(crop: true) ?? [];
pictures = await CunningDocumentScanner.getPictures() ?? [];
if (!mounted) return;
setState(() {
_pictures = pictures;
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
version: 1.1.5
version: 1.2.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
4 changes: 2 additions & 2 deletions lib/cunning_document_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CunningDocumentScanner {

/// Call this to start get Picture workflow.
static Future<List<String>?> getPictures(
{bool crop = false, int noOfPages = 100, int imageQuality = 100}) async {
{int noOfPages = 100}) async {
Map<Permission, PermissionStatus> statuses = await [
Permission.camera,
].request();
Expand All @@ -19,7 +19,7 @@ class CunningDocumentScanner {
}

final List<dynamic>? pictures = await _channel.invokeMethod('getPictures',
{'crop': crop, 'noOfPages': noOfPages, 'imageQuality': imageQuality});
{'noOfPages': noOfPages});
return pictures?.map((e) => e as String).toList();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cunning_document_scanner
description: A document scanner plugin for flutter. Scan and crop automatically on iOS and Android.
version: 1.1.5
version: 1.2.0
homepage: https://cunning.biz
repository: https://github.com/jachzen/cunning_document_scanner

Expand Down

0 comments on commit 6be0bb6

Please sign in to comment.