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
958 stars 245 forks source link

Can I click the picture programmatically using a controller? #313

Closed meet7-sagar23 closed 1 year ago

meet7-sagar23 commented 1 year ago

Proposal

I want to trigger the capture photo event manually from some other place in the code.

Can we have a Controller or a GlobalKey using which I can trigger the capture event and get the captured image path or data?

apalala-dev commented 1 year ago

You can use the CameraState like this:

state.when(
  onPhotoMode: (photoState) => photoState.takePhoto(),
  onVideoMode: (videoState) => videoState.startRecording(),
  onVideoRecordingMode: (videoState) => videoState.stopRecording(),
)

That's what AwesomeCaptureButton uses behind the scene :)

The current CameraState is obtained from the CameraAwesomeBuilder builders:

CameraAwesomeBuilder.awesome(
    previewDecoratorBuilder: (state, previewSize, previewRect) {
          // You could save the current state in a variable, but don't forget to refresh it every time this method is called
          _currentState = state;

          // Example with a custom button used to take pictures
          return ElevatedButton(
            onPressed: () {
              state.when(
                onPhotoMode: (photoState) => photoState.takePhoto(),
                onVideoMode: (videoState) => videoState.startRecording(),
                onVideoRecordingMode: (videoState) =>
                    videoState.stopRecording(),
              );
            },
            child: Text("Example"),
          );
        },
)

custom() constructor has a similar builder property.

For getting the path of the media taken, you can listen to state.captureState$.

I'm closing the issue since it should solve your use case but feel free to keep discussing - and eventually reopen the issue if it does not work for you. 👌

meet7-sagar23 commented 1 year ago

This is definitely a good solution @apalala-dev, but the reason why I asked about a controller or a global key is because I want to trigger it manually when an action is being performed.

apalala-dev commented 1 year ago

We might add a key to let people access the state without having to register it in the builders functions for this kind of use cases.

meet7-sagar23 commented 1 year ago

We might add a key to let people access the state without having to register it in the builders functions for this kind of use cases.

Sounds like a plan. Until then, should I keep this bug open or closed @apalala-dev?

apalala-dev commented 1 year ago

I'm not yet 100% sure that doing it is a good idea: CameraAwesomeBuilder has several properties that should not be accessed as is by users and making sure everything is safe would add more complexity for a small benefit. There's a camera flow that must be respected. Letting users access a controller or something like that implies that they may use the currently internal API without respecting that flow.

Since you wanted to take a picture manually and that it's doable, you can let this issue closed.

I'll open a new issue to discuss the need of a controller/global key, and try to define exactly what is needed. Feel free to share your opinion there!