Apparence-io / CamerAwesome

📸 Embedding a camera experience within your own app shouldn't be that hard. A flutter plugin to integrate awesome Android / iOS camera experience.
https://ApparenceKit.dev
MIT License
985 stars 260 forks source link

Error when stopping the camera stream with autoStart: false or calling stop on AnalysisController #536

Open Somespi opened 3 days ago

Somespi commented 3 days ago

Steps to Reproduce

I encountered an issue when setting autoStart: false and attempting to stop the camera stream using AnalysisController.stop()

return CameraAwesomeBuilder.analysisOnly(
  onImageForAnalysis: (image) async {
         ...
        },
  imageAnalysisConfig: AnalysisConfig(
          androidOptions: const AndroidAnalysisOptions.jpeg(width: 500),
          autoStart: false,
          maxFramesPerSecond: 5,
        ),
    ...

or when calling stop method for AnalysisController

...
builder: (controller, preview) => Scaffold(
              floatingActionButton: FloatingActionButton(
                      heroTag: "stop camera",
                      onPressed: () async {
                            await controller.analysisController?.stop();
                      }, 
...

Expected results

The camera stream should stop when calling stop() on the AnalysisController, and no error should occur.

Actual results

An error is thrown.

E/flutter ( 5933): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(IllegalArgumentException, java.lang.IllegalArgumentException: UseCase must not be empty., Cause: null, Stacktrace: java.lang.IllegalArgumentException: UseCase must not be empty.
E/flutter ( 5933):      at androidx.core.util.Preconditions.checkArgument(Preconditions.java:51)
E/flutter ( 5933):      at androidx.camera.core.UseCaseGroup$Builder.build(UseCaseGroup.java:181)
E/flutter ( 5933):      at com.apparence.camerawesome.cameraX.CameraXState.updateLifecycle(CameraXState.kt:260)
E/flutter ( 5933):      at com.apparence.camerawesome.cameraX.CameraAwesomeX.stopAnalysis(CameraAwesomeX.kt:279)
E/flutter ( 5933):      at com.apparence.camerawesome.cameraX.CameraInterface$Companion.setUp$lambda$72$lambda$71(Pigeon.kt:1456)
E/flutter ( 5933):      at com.apparence.camerawesome.cameraX.CameraInterface$Companion.$r8$lambda$oUqylGWaZ5hjmRyUjpcRAmikQ_E(Unknown Source:0)
E/flutter ( 5933):      at com.apparence.camerawesome.cameraX.CameraInterface$Companion$$ExternalSyntheticLambda19.onMessage(Unknown Source:2)
E/flutter ( 5933):      at io.flutter.plugin.common.BasicMessageChannel$IncomingMessageHandler.onMessage(BasicMessageChannel.java:261)
E/flutter ( 5933):      at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/flutter ( 5933):      at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter ( 5933):      at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter ( 5933):      at android.os.Handler.handleCallback(Handler.java:958)
E/flutter ( 5933):      at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 5933):      at android.os.Looper.loopOnce(Looper.java:232)
E/flutter ( 5933):      at android.os.Looper.loop(Looper.java:334)
E/flutter ( 5933):      at android.app.ActivityThread.main(ActivityThread.java:8291)
E/flutter ( 5933):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 5933):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:557)
E/flutter ( 5933):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
E/flutter ( 5933): , null)
E/flutter ( 5933): #0      CameraInterface.stopAnalysis (package:camerawesome/pigeon.dart:1589:7)
E/flutter ( 5933): <asynchronous suspension>
E/flutter ( 5933): #1      AnalysisController.stop (package:camerawesome/src/orchestrator/analysis/analysis_controller.dart:92:5)
E/flutter ( 5933): <asynchronous suspension>
E/flutter ( 5933): #2      _MyHomePageState.build.<anonymous closure>.<anonymous closure> (package:baseerah/main.dart:198:29)
E/flutter ( 5933): <asynchronous suspension>
E/flutter ( 5933): 

About your device

Brand Model OS
Nothing Phone 2 Android 14 ( Nothing OS 2.6)

Your flutter version

Run this in your command line flutter --version

Flutter 3.24.3 • channel • https://github.com/flutter/flutter.git Framework • revision archlinuxaur (unknown (arch linux aur package)) • 2038-01-19 03:14:08 Engine • revision 36335019a8ea Tools • Dart 3.5.3 • DevTools 2.37.3

Somespi commented 3 days ago

After some testing, I noticed that when doing await controller.analysisController?.imageSubscription?.cancel(); manually it works fine. I'm betting that the issue comes from await CamerawesomePlugin.stopAnalysis(); in

Future<void> stop() async {
    if (onImageListener == null || imageSubscription == null) {
      return;
    }
    _analysisEnabled = false;
    await CamerawesomePlugin.stopAnalysis();
    imageSubscription?.cancel();
    imageSubscription = null;
  }