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

[DocumentScanner] Google Play Service — Something Went Wrong, Try Again Later Error #728

Closed
dimitryzub opened this issue Dec 19, 2024 · 1 comment
Assignees

Comments

@dimitryzub
Copy link

dimitryzub commented Dec 19, 2024

The main issue: document scanner can't get to process image. All of the test was done on a physical phone.

The same issue appears (image below) with:

  1. google_ml_kit example app.
  2. flutter_doc_scanner package example. Most likely because it uses mlkit.
  3. my app using mlkit.

Bar scanner and rest of the sample apps where camera was requried was working as expected without any issues.

Note: I'm new to Flutter/Dart so code is most likely dummy. If you need more context, please let me know.

Physical phone specs:

  • Android 11
  • Phone Google Pixel 2 XL
Full Debug Console Log (1:1 log from my app and google_ml_kit example app)
Launching lib\main.dart on Pixel 2 XL in debug mode...
√ Built build\app\outputs\flutter-apk\app-debug.apk
Connecting to VM Service at ws://127.0.0.1:52075/Hu87wv4mJl0=/ws
Connected to the VM Service.
I/Gralloc4(24346): mapper 4.x is not supported
W/Gralloc3(24346): mapper 3.x is not supported
D/ProfileInstaller(24346): Installing profile for monofinance.app
D/TransportRuntime.JobInfoScheduler(24346): Scheduling upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) with jobId=-1678826653 in 86400000ms(Backend next call timestamp 0). Attempt 1
I/TetheringManager(24346): registerTetheringEventCallback:monofinance.app
D/TransportRuntime.SQLiteEventStore(24346): Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler(24346): Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
D/TransportRuntime.SQLiteEventStore(24346): Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler(24346): Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
E/GmsDocScanDelAct(24346): Failed to handle scanning result

# LOGS BELOW AFTER I CLICKED "CANCEL"
E/GmsDocScanDelAct(24346): java.lang.IllegalStateException: Failed to handle result
E/GmsDocScanDelAct(24346): 	at com.google.mlkit.vision.documentscanner.internal.zzg.run(com.google.android.gms:play-services-mlkit-document-scanner@@16.0.0-beta1:12)
E/GmsDocScanDelAct(24346): 	at android.os.Handler.handleCallback(Handler.java:938)
E/GmsDocScanDelAct(24346): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/GmsDocScanDelAct(24346): 	at android.os.Looper.loop(Looper.java:223)
E/GmsDocScanDelAct(24346): 	at android.os.HandlerThread.run(HandlerThread.java:67)
D/TransportRuntime.SQLiteEventStore(24346): Storing event with priority=VERY_LOW, name=FIREBASE_ML_SDK for destination cct
D/TransportRuntime.JobInfoScheduler(24346): Upload for context TransportContext(cct, VERY_LOW, MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc) is already scheduled. Returning...
I/flutter (24346): Error scanning document: PlatformException(DocumentScanner, Operation cancelled, null, null)
Application finished.

Exited (-1).

Error screen from Play Service I get whenever I try to process image via mlkit document scanner:

Code

document_scanner_service.dart
import 'package:google_mlkit_document_scanner/google_mlkit_document_scanner.dart';
import '../models/scan_result.dart';

class DocumentScannerService {
  late DocumentScanner _documentScanner;

  DocumentScannerService() {
    final documentOptions = DocumentScannerOptions(
      documentFormat: DocumentFormat.jpeg,
      mode: ScannerMode.filter,
      pageLimit: 1,
      isGalleryImport: true,
    );

    _documentScanner = DocumentScanner(options: documentOptions);
  }

  Future<ScanResult?> scanDocument() async {
    try {
      final result = await _documentScanner.scanDocument();

      if (result.images.isEmpty) return null;

      return ScanResult(
        processedImagePath: result.images.first,
      );
    } catch (e) {
      print('Error scanning document: $e');
      return null;
    }
  }

  void dispose() {
    _documentScanner.close();
  }
}
receipt_scanner_screen.dart
import 'package:flutter/material.dart';
import 'dart:io';
import '../services/document_scanner_service.dart';
import '../models/scan_result.dart';

class ReceiptScannerScreen extends StatefulWidget {
  const ReceiptScannerScreen({super.key});

  @override
  State<ReceiptScannerScreen> createState() => _ReceiptScannerScreenState();
}

class _ReceiptScannerScreenState extends State<ReceiptScannerScreen> {
  final DocumentScannerService _scannerService = DocumentScannerService();
  ScanResult? _scanResult;
  bool _isScanning = false;

  @override
  void initState() {
    super.initState();
    _startScanning();
  }

  Future<void> _startScanning() async {
    setState(() => _isScanning = true);

    final result = await _scannerService.scanDocument();

    setState(() {
      _scanResult = result;
      _isScanning = false;
    });
  }

  @override
  void dispose() {
    _scannerService.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Сканування чеку'),
      ),
      body: Column(
        children: [
          if (_isScanning)
            const Center(
              child: CircularProgressIndicator(),
            )
          else if (_scanResult != null)
            Expanded(
              child: Column(
                children: [
                  Expanded(
                    child: Image.file(
                      File(_scanResult!.processedImagePath),
                      fit: BoxFit.contain,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        ElevatedButton(
                          onPressed: _startScanning,
                          child: const Text('Сканувати знову'),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            Navigator.pop(context, _scanResult);
                          },
                          child: const Text('Зберегти'),
                        ),
                        TextButton(
                          onPressed: () => Navigator.pop(context),
                          child: const Text('Скасувати'),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            )
        ],
      ),
    );
  }
}
scan_result.dart
class ScanResult {
  final String processedImagePath;

  ScanResult({
    required this.processedImagePath,
  });

  factory ScanResult.fromMap(Map<String, dynamic> map) {
    return ScanResult(
      processedImagePath: map['processedImagePath'] as String,
    );
  }
}

android/app/build.gradle
plugins {
    id "com.android.application"
    id "kotlin-android"
    id "com.google.gms.google-services"
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "monofinance.app"
    compileSdk = 34
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId = "monofinance.app"
        minSdk = Math.max(21, flutter.minSdkVersion)
        targetSdk = 34
        versionCode = flutter.versionCode
        versionName = flutter.versionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

dependencies {
    implementation 'com.google.android.gms:play-services-mlkit-document-scanner:16.0.0-beta1'
    // implementation platform('com.google.firebase:firebase-bom:33.7.0')
    // implementation 'com.android.support:multidex:1.0.3'
}
android/build.gradle
buildscript {
    ext.kotlin_version = '1.9.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.2'
    }
}

plugins {
    id 'com.google.gms.google-services' version '4.4.2' apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }

    // Test exclusions on gradlew build
    tasks.withType(Test) {
        exclude '**/ImageSaverTests*'
        exclude '**/camera/**'
        exclude '**/webviewflutter/**'  
        exclude '**/ConsoleMessageTest*'
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
android/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:label="monofinance.app"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:largeHeap="true">
        <activity
            android:name="monofinance.app.MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <meta-data
            android:name="com.google.mlkit.vision.DEPENDENCIES"
            android:value="ica,ocr,face,subject_segment" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

Steps to reproduce.

Using Example App:

  1. Open app.
  2. Open document scanner.
  3. Click Scan PDG/JPG.

What is the expected result?

Scanned image result/preview. Cropped, rotated, color adjusted, etc.

Did you try our example app?

Yes

Is it reproducible in the example app?

Yes

Reproducible in which OS?

Android

Flutter/Dart Version?

My goal is Android platform only, so Windows/Chrome errors are there.

[√] Flutter (Channel stable, 3.24.5, on Microsoft Windows [Version 10.0.19045.5247], locale en-US)
    • Flutter version 3.24.5 on channel stable at C:\Workspace\flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision dec2ee5c1f (5 weeks ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\Dmitriy\AppData\Local\Android\sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Program Files\Java\jdk-19\bin\java
    • Java version Java(TM) SE Runtime Environment (build 19.0.2+7-44)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2024.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[√] VS Code (version 1.95.3)
    • VS Code at C:\Users\Dmitriy\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available)
    • Pixel 2 XL (mobile) • adb-712KPQJ1311388-n9MQAS._adb-tls-connect._tcp • android-arm64  • Android 11 (API 30)
    • Windows (desktop)   • windows                                         • windows-x64    • Microsoft Windows [Version 10.0.19045.5247]
    • Edge (web)          • edge                                            • web-javascript • Microsoft Edge 131.0.2903.99

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Plugin Version?

google_mlkit_document_scanner: ^0.3.0

@bensonarafat
Copy link
Collaborator

@dimitryzub Can this issue be reported on mlkit instead since the package relies on the Google Play service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants