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
920 stars 208 forks source link

Question: How to Run Code after picture is taken or video is finished recording #240

Closed chitgoks closed 1 year ago

chitgoks commented 1 year ago

I already have a code in place to run the code what to do with the picture or video taken. But i am not sure where this takes place.

I figured it would be in

saveConfig: SaveConfig.photoAndVideo(
                            photoPathBuilder: () => _path(CaptureMode.photo),
                            videoPathBuilder: () => _path(CaptureMode.video),
                            initialCaptureMode: CaptureMode.photo,
                          ),

but that sample just returns a string. what would the photoPathBuilder look like if i would place code after a picture is taken (or video)?

Thoughts?

apalala-dev commented 1 year ago

The SaveConfig does not provide a callback to call after a media has been saved.

However, you can listen to cameraState.captureState$ to react to different capture states. A new event is provided whenever the status of the capture status changes, including when a media is saved. Here, you probably want a MediaCapture with a MediaCaptureStatus.success before running your code.

chitgoks commented 1 year ago

Thanks! I think that is what I am looking for.

rlee1990 commented 1 year ago

@apalala-dev how do you do this with the awesome builder or would we have to use the custom builder?

The SaveConfig does not provide a callback to call after a media has been saved.

However, you can listen to cameraState.captureState$ to react to different capture states. A new event is provided whenever the status of the capture status changes, including when a media is saved. Here, you probably want a MediaCapture with a MediaCaptureStatus.success before running your code.

jasonroland commented 6 months ago

Here's how I'm implementing this. Is there a better way to get access to state, not in the builder?


return CameraAwesomeBuilder.awesome(
  saveConfig: SaveConfig.photo(),
  onMediaTap: (mediaCapture) {
    print("Tapped");
  },
  topActionsBuilder: (state) {
    // This is placed here due to the presence of state; 
    _captureStateSubscription ??=
        state.captureState$.listen((MediaCapture? mediaCapture) {
      if (mediaCapture != null) {
        capture = mediaCapture;

        switch (mediaCapture.status) {
          case MediaCaptureStatus.success:
            // Image captured, perform additional code (e.g., get the file path)
            print("Image captured: ${filePath(mediaCapture)}");
            break;
          case MediaCaptureStatus.capturing:
            print("Capturing in progress...");
            break;
          case MediaCaptureStatus.failure:
            print("Capture failed!");
            break;
        }
      }
    });
    return Container();
    },

    // Retrieve the file path
    String filePath(MediaCapture mediaCapture) {
      if (mediaCapture.status == MediaCaptureStatus.success &&
          mediaCapture.captureRequest != null) {
        return mediaCapture.captureRequest.when(
          single: (single) => single.file!.path,
          multiple: (multiple) => multiple.fileBySensor.values.first!.path,
        );
      } else {
        return "null found";
      }
    }
  }
);