CameraView icon indicating copy to clipboard operation
CameraView copied to clipboard

Callback for preview stream availability

Open razlesh1 opened this issue 3 years ago • 5 comments

I am looking for a way to observe preview stream availability, meaning when is the preview shown, and when it is closed. Reading the documentation of CameraView, I haven't find a way to do so. Does this feature exist in CameraView? or this behavior can be implemented somehow? and if so then how?

Version Used

CameraView 2.7.2

razlesh1 avatar Nov 15 '21 21:11 razlesh1

There's no API for this unfortunately.

natario1 avatar Nov 16 '21 00:11 natario1

Will there be in a future CameraView release? I know that CameraX has that option and it is using Camera2 so I believe this behavior can be achieved somehow. Anyways, thanks for the quick answer!

razlesh1 avatar Nov 24 '21 08:11 razlesh1

There might be, it's not hard to do. If you plan to work on it, I can point you to the right place.

natario1 avatar Nov 24 '21 09:11 natario1

It'd be great if you could point me to the right place🙏

razlesh1 avatar Nov 25 '21 06:11 razlesh1

I would do this:

  1. create a new sealed class for the camera state, something like:

    sealed class CameraState { 
       class Closed : CameraState()
       class Open : CameraState()
       class Preview : CameraState()
    }
    

    Let's use a sealed class instead of enum because in the future we will add more information.

  2. add onCameraStateChanged(@NonNull CameraState state) to CameraListener

  3. in CameraEngine.Callback, add a new method like dispatchCameraState

  4. implement this method in CameraView, you can use the other dispatch* methods as reference https://github.com/natario1/CameraView/blob/main/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java#L2224

  5. we already have another CameraState, com.otaliastudios.cameraview.engine.orchestrator.CameraState. Please rename this to CameraInternalState to avoid confusion.

  6. We need to be able to convert internal state to the new CameraState. It's easy, OFF means Closed(), ENGINE and BIND mean Open(), PREVIEW means Preview().

  7. Add a new function here, like onInternalStateChanged(CameraInternalState). The function should be invoked when the internal state changes, here: https://github.com/natario1/CameraView/blob/main/cameraview/src/main/java/com/otaliastudios/cameraview/engine/orchestrator/CameraStateOrchestrator.java#L74

  8. Last step: whenever internal state changes, dispatch the new state. This should be done here, implement the new method that you added in 8., convert internal state to camera state as we described, and pass to callback defined in 4. mCallback.dispatchCameraState(internalState.toCameraState())

Then run the app and verify that it works :-)

natario1 avatar Nov 25 '21 13:11 natario1