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
911 stars 200 forks source link

Add `OnErrorMode` to `CameraState` #328

Open rubenferreira97 opened 1 year ago

rubenferreira97 commented 1 year ago

Look at the following code:

CameraAwesomeBuilder.previewOnly(
  onImageForAnalysis: (img) => _processImageBarcode(img),
  imageAnalysisConfig: AnalysisConfig(
    androidOptions: const AndroidAnalysisOptions.nv21(
      width: 1024,
    ),
    cupertinoOptions: const CupertinoAnalysisOptions.bgra8888(),
    maxFramesPerSecond: 5,
  ),
  builder: (cameraState, previewSize, previewRect) => const SizedBox.shrink(),
)

Currently if the camera can't load (if there is no camera or a camera can't be resolved for the use case) an exception is given and AFAIK there is no way to catch the error.

The previous code throws the following exception on the Android 12L emulator: java.lang.IllegalArgumentException: Provided camera selector unable to resolve a camera for the given use case.

Proposal

Allow to build a widget from .when on OnErrorMode that catches exceptions.

typedef OnErrorMode = Function(OnErrorState);

class OnErrorState {...}

builder: (cameraState, previewSize, previewRect) => cameraState.when(
  onErrorMode: (error) => Text('An error $error')
)